001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     *   
017     *   Derived from code developed by Opentick Corporation, http://www.opentick.com.
018     */
019    
020    package org.otfeed.event;
021    
022    import static org.otfeed.event.IdentityUtil.equalsTo;
023    import static org.otfeed.event.IdentityUtil.safeCompare;
024    import static org.otfeed.event.IdentityUtil.safeHashCode;
025    
026    import java.io.Serializable;
027    import java.util.Date;
028    
029    /**
030     * This is a change to an existing order, and implies that 
031     * a trade occurred. Look up the existing order using the 
032     * order reference, and subtract the size from the order.
033     * If the order now has no shares, remove the order. 
034     */
035    public final class OTBookExecute implements Comparable<OTBookExecute>, Serializable {
036            
037            private static final long serialVersionUID = -1247678289972113218L;
038    
039            private Date timestamp;
040        private String reference;
041        private int size;
042        private int matchNumber;
043    
044        /**
045         * Default constructor.
046         */
047        public OTBookExecute() { }
048    
049        /**
050         * Constructor.
051         * @param timestamp Time when the event occurred.
052         * @param reference Order reference.
053         * @param size Number of shares.
054         * @param matchNumber ISLD specific unique match identifier.
055         */
056        public OTBookExecute(Date timestamp, 
057                    String reference, 
058                    int size, 
059                    int matchNumber) {
060            this.timestamp = timestamp;
061            this.reference = reference;
062            this.size = size;
063            this.matchNumber = matchNumber;
064        }
065    
066        /**
067         *
068         * @return Time when the event occurred.
069         */
070        public Date getTimestamp() {
071            return timestamp;
072        }
073    
074        /**
075         * Sets time of the event. 
076         * @param timestamp Time when the event occurred.
077         */
078        public void setTimestamp(Date timestamp) {
079            this.timestamp = timestamp;
080        }
081    
082        /**
083         * 
084         * @return Order reference.
085         */
086        public String getOrderRef() {
087            return reference;
088        }
089    
090        /**
091         * Sets order reference. 
092         * @param reference Order reference.
093         */
094        public void setOrderRef(String reference) {
095            this.reference = reference;
096        }
097    
098        /**
099         * 
100         * @return Number of shares.
101         */
102        public int getSize() {
103            return size;
104        }
105    
106        /**
107         * Sets number of shares.
108         * @param size Number of shares. 
109         */
110        public void setSize(int size) {
111            this.size = size;
112        }
113    
114        /**
115         * 
116         * @return ISLD specific unique match identifier.
117         */
118        public int getMatchNumber() {
119            return matchNumber;
120        }
121    
122        /**
123         * Sets time of the event.
124         * @param matchNumber Time when the event occurred.
125         */
126        public void setMatchNumber(int matchNumber) {
127            this.matchNumber = matchNumber;
128        }
129    
130        @Override
131            public String toString() {
132            return "OTBookExecute: timestamp=" + timestamp + ", orderref=" + reference + ", size=" + size + ", matchnumber=" + matchNumber;
133        }
134    
135        @Override
136            public int hashCode() {
137            return safeHashCode(timestamp) 
138                    + 3 * safeHashCode(reference) 
139                    + 5 * safeHashCode(size) 
140                            + 7 * safeHashCode(matchNumber); 
141        }
142            
143        @Override
144            public boolean equals(Object o) {
145            return equalsTo(this, o);
146        }
147        
148            public int compareTo(OTBookExecute other) {
149                    int rc;
150    
151                    if((rc = safeCompare(timestamp, other.timestamp)) != 0) {
152                            return rc;
153                    }
154                    
155                    if((rc = safeCompare(reference, other.reference)) != 0) {
156                            return rc;
157                    }
158    
159                    if((rc = safeCompare(size, other.size)) != 0) {
160                            return rc;
161                    }
162                    
163                    if((rc = safeCompare(matchNumber, other.matchNumber)) != 0) {
164                            return rc;
165                    }
166    
167                    return 0;
168            }
169    }