<commit_before>/**
 * Copyright (C) 2013 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.strata.collect.result;

import java.io.Serializable;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.Collectors;

import org.joda.beans.Bean;
import org.joda.beans.BeanBuilder;
import org.joda.beans.BeanDefinition;
import org.joda.beans.ImmutableBean;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaProperty;
import org.joda.beans.Property;
import org.joda.beans.PropertyDefinition;
import org.joda.beans.impl.direct.DirectFieldsBeanBuilder;
import org.joda.beans.impl.direct.DirectMetaBean;
import org.joda.beans.impl.direct.DirectMetaProperty;
import org.joda.beans.impl.direct.DirectMetaPropertyMap;

import com.google.common.collect.ImmutableSet;
import com.opengamma.strata.collect.ArgChecker;

/**
 * Description of a failed result.
 * <p>
 * If calculation of a result fails this class provides details of the failure.
 * There is a single reason and message and a set of detailed failure items.
 * Each {@link FailureItem} has details of the actual cause.
 * <p>
 * Instances of {@code Failure} are public classes created via {@link Result}.
 */
@BeanDefinition(builderScope = "private")
public final class Failure
    implements ImmutableBean, Serializable {

  /**
   * The reason associated with the failure.
   */
  @PropertyDefinition(validate = "notNull")
  private final FailureReason reason;
  /**
   * The error message associated with the failure.
   */
  @PropertyDefinition(validate = "notEmpty")
  private final String message;
  /**
   * The set of failure items.
   * There will be at least one failure item.
   */
  @PropertyDefinition(validate = "notEmpty")
  private final ImmutableSet<FailureItem> items;

  //-------------------------------------------------------------------------
  /**
   * Obtains a failure from a reason, message and exception.
   * 
   * @param reason  the reason
   * @param message  the failure message
   * @param cause  the cause
   * @return the failure
   */
  static Failure of(FailureReason reason, Exception cause, String message, Object... messageArgs) {
    return Failure.of(FailureItem.of(reason, cause, message, messageArgs));
  }

  /**
   * Obtains a failure from a reason and message.
   * 
   * @param reason  the reason
   * @param message  the failure message, not empty
   * @return the failure
   */
  static Failure of(FailureReason reason, String message) {
    return Failure.of(FailureItem.of(reason, message, 2));
  }

  /**
   * Obtains a failure from a reason and exception.
   * 
   * @param reason  the reason
   * @param cause  the cause
   * @return the failure
   */
  static Failure of(FailureReason reason, Exception cause) {
    return Failure.of(FailureItem.of(reason, cause));
  }

  /**
   * Obtains a failure for a single failure item.
   * 
   * @param item  the failure item
   * @return the failure
   */
  private static Failure of(FailureItem item) {
    return new Failure(item.getReason(), item.getMessage(), ImmutableSet.of(item));
  }

  /**
   * Obtains a failure for a non-empty set of failure items.
   * 
   * @param items  the failures, not empty
   * @return the failure
   */
  static Failure of(ImmutableSet<FailureItem> items) {
    ArgChecker.notEmpty(items, "items");
    String message = items.stream()
        .map(FailureItem::getMessage)
        .collect(Collectors.joining(", "));
    FailureReason reason = items.stream()
        .map(FailureItem::getReason)
        .reduce((s1, s2) -> s1 == s2 ? s1 : FailureReason.MULTIPLE).get();
    return new Failure(reason, message, items);
  }

  //------------------------- AUTOGENERATED START -------------------------
  ///CLOVER:OFF
  /**
   * The meta-bean for {@code Failure}.
   * @return the meta-bean, not null
   */
  public static Failure.Meta meta() {
    return Failure.Meta.INSTANCE;
  }

  static {
    JodaBeanUtils.registerMetaBean(Failure.Meta.INSTANCE);
  }

  /**
   * The serialization version id.
   */
  private static final long serialVersionUID = 1L;

  private Failure(
      FailureReason reason,<commit_msg>Make Failure.of methods public (#1305)<commit_after>/**
 * Copyright (C) 2013 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.strata.collect.result;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.Collectors;

import org.joda.beans.Bean;
import org.joda.beans.BeanBuilder;
import org.joda.beans.BeanDefinition;
import org.joda.beans.ImmutableBean;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaProperty;
import org.joda.beans.Property;
import org.joda.beans.PropertyDefinition;
import org.joda.beans.impl.direct.DirectFieldsBeanBuilder;
import org.joda.beans.impl.direct.DirectMetaBean;
import org.joda.beans.impl.direct.DirectMetaProperty;
import org.joda.beans.impl.direct.DirectMetaPropertyMap;

import com.google.common.collect.ImmutableSet;
import com.opengamma.strata.collect.ArgChecker;
import com.opengamma.strata.collect.Messages;

/**
 * Description of a failed result.
 * <p>
 * If calculation of a result fails this class provides details of the failure.
 * There is a single reason and message and a set of detailed failure items.
 * Each {@link FailureItem} has details of the actual cause.
 * <p>
 * Instances of {@code Failure} are public classes created via {@link Result}.
 */
@BeanDefinition(builderScope = "private")
public final class Failure
    implements ImmutableBean, Serializable {

  /**
   * The reason associated with the failure.
   */
  @PropertyDefinition(validate = "notNull")
  private final FailureReason reason;
  /**
   * The error message associated with the failure.
   */
  @PropertyDefinition(validate = "notEmpty")
  private final String message;
  /**
   * The set of failure items.
   * There will be at least one failure item.
   */
  @PropertyDefinition(validate = "notEmpty")
  private final ImmutableSet<FailureItem> items;

  //-------------------------------------------------------------------------
  /**
   * Obtains a failure from a reason, message and exception.
   * 
   * @param reason  the reason
   * @param cause  the cause
   * @param message  the failure message, possibly containing placeholders, formatted using {@link Messages#format}
   * @param messageArgs  arguments used to create the failure message
   * @return the failure
   */
  public static Failure of(FailureReason reason, Exception cause, String message, Object... messageArgs) {
    return Failure.of(FailureItem.of(reason, cause, message, messageArgs));
  }

  /**
   * Obtains a failure from a reason and message.
   * 
   * @param reason  the reason
   * @param message  the failure message, not empty
   * @return the failure
   */
  public static Failure of(FailureReason reason, String message) {
    return Failure.of(FailureItem.of(reason, message, 2));
  }

  /**
   * Obtains a failure from a reason and exception.
   * 
   * @param reason  the reason
   * @param cause  the cause
   * @return the failure
   */
  public static Failure of(FailureReason reason, Exception cause) {
    return Failure.of(FailureItem.of(reason, cause));
  }

  /**
   * Obtains a failure for a single failure item.
   * 
   * @param item  the failure item
   * @return the failure
   */
  public static Failure of(FailureItem item) {
    return new Failure(item.getReason(), item.getMessage(), ImmutableSet.of(item));
  }

  /**
   * Obtains a failure for multiple failure items.
   *
   * @param item  the first failure item
   * @param additionalItems  additional failure items
   * @return the failure
   */
  public static Failure of(FailureItem item, FailureItem... additionalItems) {
    return of(ImmutableSet.<FailureItem>builder().add(item).add(additionalItems).build());
  }

  /**
   * Obtains a failure for a non-empty collection of failure items.
   * 
   * @param items  the failures, not empty
   * @return the failure
   */
  public static Failure of(Collection<FailureItem> items) {
    ArgChecker.notEmpty(items, "items");
    Set<FailureItem> itemSet = ImmutableSet.copyOf(items);
    String message = itemSet.stream()
        .map(FailureItem::getMessage)
        .collect(Collectors.joining(", "));
    FailureReason reason = itemSet.stream()
        .map(FailureItem::getReason)
        .reduce((s1, s2) -> s1 == s2 ? s1 : FailureReason.MULTIPLE).get();
    return new Failure(reason, message, itemSet);
  }

  //------------------------- AUTOGENERATED START -------------------------
  ///CLOVER:OFF
  /**
   * The meta-bean for {@code Failure}.
   * @return the meta-bean, not null
   */
  public static Failure.Meta meta() {
    return Failure.Meta.INSTANCE;
  }

  static {
    JodaBeanUtils.registerMetaBean(Failure.Meta.INSTANCE);
  }

  /**
   * The serialization version id.
   */
  private static final long serialVersionUID = 1L;

  private Failure(
      FailureReason reason,
{'fraction_non_alphanumeric': 0.12892705728883574, 'fraction_numerical': 0.0028263941732797043, 'mean_word_length': 3.1144901610017888, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5566262', 'n_tokens_mistral': 2689, 'n_tokens_neox': 2606, 'n_words': 804}