package data;

/**
  * Objectifier to compute the value of a CatalogItem.
  *
  * @see Stock#sumStock
  *
  * @author Steffen Zschaler
  * @version 2.0 18/08/1999
  * @since v1.0
  */
public class CatalogItemValue extends Object {

  /**
    * Return the value of an CatalogItem.
    *
    * <p>By default returns {@link CatalogItem#getValue ci.getValue()}, but you can create subclasses that
    * refer to any attribute you want to use as the CatalogItem's value.</p>
    *
    * @override Always
    *
    * @param ci the CatalogItem whose value is to be computed
    *
    * @return the value of ci.
    *
    * @see Stock#sumStock
    */
  public Value getValue (CatalogItem ci) {
    return ci.getValue();
  }

  /**
    * A CatalogItemValue that will return the {@link QuoteValue#getBid bid} if the
    * {@link CatalogItem#getValue standard value} of a {@link CatalogItem} is a {@link QuoteValue}. Otherwise,
    * it will simply return the standard value of the CatalogItem.
    */
  public static final CatalogItemValue EVALUATE_BID = new CatalogItemValue() {
    public Value getValue (CatalogItem ci) {
      Value vReturn = ci.getValue();

      if (vReturn instanceof QuoteValue) {
        vReturn = ((QuoteValue) vReturn).getBid();
      }

      return vReturn;
    }
  };

  /**
    * A CatalogItemValue that will return the {@link QuoteValue#getOffer offer} if the
    * {@link CatalogItem#getValue standard value} of a {@link CatalogItem} is a {@link QuoteValue}. Otherwise,
    * it will simply return the standard value of the CatalogItem.
    */
  public static final CatalogItemValue EVALUATE_OFFER = new CatalogItemValue() {
    public Value getValue (CatalogItem ci) {
      Value vReturn = ci.getValue();

      if (vReturn instanceof QuoteValue) {
        vReturn = ((QuoteValue) vReturn).getOffer();
      }

      return vReturn;
    }
  };

  /**
    * A CatalogItemValue that will return the {@link QuoteValue#getMid mid} if the
    * {@link CatalogItem#getValue standard value} of a {@link CatalogItem} is a {@link QuoteValue}. Otherwise,
    * it will simply return the standard value of the CatalogItem.
    */
  public static final CatalogItemValue EVALUATE_MID = new CatalogItemValue() {
    public Value getValue (CatalogItem ci) {
      Value vReturn = ci.getValue();

      if (vReturn instanceof QuoteValue) {
        vReturn = ((QuoteValue) vReturn).getMid();
      }

      return vReturn;
    }
  };

  /**
    * A CatalogItemValue that will return the {@link QuoteValue#getSpread spread} if the
    * {@link CatalogItem#getValue standard value} of a {@link CatalogItem} is a {@link QuoteValue}. Otherwise,
    * it will simply return the standard value of the CatalogItem.
    */
  public static final CatalogItemValue EVALUATE_SPREAD = new CatalogItemValue() {
    public Value getValue (CatalogItem ci) {
      Value vReturn = ci.getValue();

      if (vReturn instanceof QuoteValue) {
        vReturn = ((QuoteValue) vReturn).getSpread();
      }

      return vReturn;
    }
  };
}