package data.swing;

import data.*;

/**
  * DataBasketEntryGrouper that groups entries that have the same main and secondary key as well as the same
  * source and destination and where both values contain an {@link Integer} object. These are DataBasketEntries
  * that describe operations on CountingStock items.
  *
  * @author Steffen Zschaler
  * @version 2.0 23/08/1999
  * @since v2.0
  */
public class CountingStockDBEGrouper extends Object implements DataBasketEntryGrouper {

  /**
    * Create a new CountingStockDBEGrouper.
    */
  public CountingStockDBEGrouper() {
    super();
  }

  /**
    * Return true if <code>dbe1</code> and <code>dbe2</code> have the same main and secondary key as well as
    * the same source and destination and both values contain an {@link Integer} object.
    *
    * @override Never
    */
  public boolean canGroup (DataBasketEntry dbe1, DataBasketEntry dbe2) {
    return ((dbe1.getMainKey().equals (dbe2.getMainKey())) &&
            (dbe1.getSecondaryKey().equals (dbe2.getSecondaryKey())) &&
            (dbe1.getSource() == dbe2.getSource()) &&
            (dbe1.getDestination() == dbe2.getDestination()) &&
            (dbe1.getValue() instanceof Integer) &&
            (dbe2.getValue() instanceof Integer));
  }

  /**
    * Return a new DataBasketEntry with the same main and secondary key, source and destination as
    * <code>dbe1</code> and a value of <code>dbe1.getValue() + dbe2.getValue()</code>.
    *
    * <p>The returned DataBasketEntry cannot be used for commiting or rolling back operations.</p>
    *
    * @override Never
    */
  public DataBasketEntry group (DataBasketEntry dbe1, DataBasketEntry dbe2) {
    class DBE extends Object implements DataBasketEntry {
      private String m_sMainKey;
      private String m_sSecondaryKey;
      private DataBasketEntrySource m_dbes;
      private DataBasketEntryDestination m_dbed;
      private Object m_oValue;
      private DataBasket m_dbOwner = null;

      public DBE (String sMainKey,
                  String sSecondaryKey,
                  DataBasketEntrySource dbes,
                  DataBasketEntryDestination dbed,
                  Object oValue) {
        super();

        m_sMainKey = sMainKey;
        m_sSecondaryKey = sSecondaryKey;
        m_dbes = dbes;
        m_dbed = dbed;
        m_oValue = oValue;
      }

      public void rollback() {}

      public void commit() {}
      public void setOwner (DataBasket dbOwner) {
        m_dbOwner = dbOwner;
      }

      public DataBasket getOwner() {
        return m_dbOwner;
      }

      public DataBasketEntrySource getSource() {
        return m_dbes;
      }

      public DataBasketEntryDestination getDestination() {
        return m_dbed;
      }

      public Object getValue() {
        return m_oValue;
      }

      public String getMainKey() {
        return m_sMainKey;
      }

      public String getSecondaryKey() {
        return m_sSecondaryKey;
      }

      public boolean isHandled() {
        return true;
      }
    }

    return new DBE (dbe1.getMainKey(),
                    dbe1.getSecondaryKey(),
                    dbe1.getSource(),
                    dbe1.getDestination(),
                    new Integer (((Integer) dbe1.getValue()).intValue() +
                                 ((Integer) dbe2.getValue()).intValue()));
  }
}