001    package data.swing;
002    
003    import data.*;
004    
005    /**
006     * DataBasketEntryGrouper that groups entries that have the same main and secondary key as well as the same
007             * source and destination and where both values contain an {@link Integer} object. These are DataBasketEntries
008     * that describe operations on CountingStock items.
009     *
010     * @author Steffen Zschaler
011     * @version 2.0 23/08/1999
012     * @since v2.0
013     */
014    public class CountingStockDBEGrouper extends Object implements DataBasketEntryGrouper {
015    
016        /**
017         * Create a new CountingStockDBEGrouper.
018         */
019        public CountingStockDBEGrouper() {
020            super();
021        }
022    
023        /**
024         * Return true if <code>dbe1</code> and <code>dbe2</code> have the same main and secondary key as well as
025         * the same source and destination and both values contain an {@link Integer} object.
026         *
027         * @override Never
028         */
029        public boolean canGroup(DataBasketEntry dbe1, DataBasketEntry dbe2) {
030            return ((dbe1.getMainKey().equals(dbe2.getMainKey())) &&
031                    (dbe1.getSecondaryKey().equals(dbe2.getSecondaryKey())) &&
032                    (dbe1.getSource() == dbe2.getSource()) && (dbe1.getDestination() == dbe2.getDestination()) &&
033                    (dbe1.getValue()instanceof Integer) && (dbe2.getValue()instanceof Integer));
034        }
035    
036        /**
037         * Return a new DataBasketEntry with the same main and secondary key, source and destination as
038         * <code>dbe1</code> and a value of <code>dbe1.getValue() + dbe2.getValue()</code>.
039         *
040         * <p>The returned DataBasketEntry cannot be used for commiting or rolling back operations.</p>
041         *
042         * @override Never
043         */
044        public DataBasketEntry group(DataBasketEntry dbe1, DataBasketEntry dbe2) {
045            class DBE extends Object implements DataBasketEntry {
046                private String m_sMainKey;
047                private String m_sSecondaryKey;
048                private DataBasketEntrySource m_dbes;
049                private DataBasketEntryDestination m_dbed;
050                private Object m_oValue;
051                private DataBasket m_dbOwner = null;
052    
053                public DBE(String sMainKey, String sSecondaryKey, DataBasketEntrySource dbes,
054                        DataBasketEntryDestination dbed, Object oValue) {
055                    super();
056    
057                    m_sMainKey = sMainKey;
058                    m_sSecondaryKey = sSecondaryKey;
059                    m_dbes = dbes;
060                    m_dbed = dbed;
061                    m_oValue = oValue;
062                }
063    
064                public void rollback() {}
065    
066                public void commit() {}
067    
068                public void setOwner(DataBasket dbOwner) {
069                    m_dbOwner = dbOwner;
070                }
071    
072                public DataBasket getOwner() {
073                    return m_dbOwner;
074                }
075    
076                public DataBasketEntrySource getSource() {
077                    return m_dbes;
078                }
079    
080                public DataBasketEntryDestination getDestination() {
081                    return m_dbed;
082                }
083    
084                public Object getValue() {
085                    return m_oValue;
086                }
087    
088                public String getMainKey() {
089                    return m_sMainKey;
090                }
091    
092                public String getSecondaryKey() {
093                    return m_sSecondaryKey;
094                }
095    
096                public boolean isHandled() {
097                    return true;
098                }
099            }
100    
101            return new DBE(dbe1.getMainKey(), dbe1.getSecondaryKey(), dbe1.getSource(), dbe1.getDestination(),
102                    new Integer(((Integer)dbe1.getValue()).intValue() + ((Integer)dbe2.getValue()).intValue()));
103        }
104    }