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     * A new order.
031     */
032    public final class OTBookOrder implements Comparable<OTBookOrder>, Serializable {
033    
034            private static final long serialVersionUID = -6395276753169887976L;
035            
036            private Date timestamp;
037        private String reference;
038        private double price;
039        private int size;
040        private TradeSideEnum side;
041        private boolean display;
042    
043        /**
044         * Default constructor.
045         */
046        public OTBookOrder() { }
047    
048        /**
049         * Constructor.
050         * @param timestamp Time when the event occurred.
051         * @param reference Order reference.
052         * @param price Price.
053         * @param size Order size in number of shares.
054         * @param side Side: B = Buy, S = Sell.
055         * @param display Display.
056         */
057        public OTBookOrder(Date timestamp, String reference, 
058                    double price, int size, 
059                    TradeSideEnum side, 
060                    boolean display) {
061            this.timestamp = timestamp;
062            this.reference = reference;
063            this.price = price;
064            this.size = size;
065            this.side = side;
066            this.display = display;
067        }
068    
069        /**
070         * 
071         * @return Time when the event occurred.
072         */
073        public Date getTimestamp() {
074            return timestamp;
075        }
076    
077        /**
078         * Sets time of the event.
079         * @param timestamp Time when the event occurred.
080         */
081        public void setTimestamp(Date timestamp) {
082            this.timestamp = timestamp;
083        }
084    
085        /**
086         * 
087         * @return Order reference; identifies the order for subsequent events.
088         */
089        public String getOrderRef() {
090            return this.reference;
091        }
092    
093        /**
094         * Sets order reference.
095         * @param reference Order reference.
096         */
097        public void setOrderRef(String reference) {
098            this.reference = reference;
099        }
100    
101        /**
102         * 
103         * @return Price.
104         */
105        public double getPrice() {
106            return price;
107        }
108    
109        /**
110         * Sets price.
111         * @param price Price.
112         */
113        public void setPrice(double price) {
114            this.price = price;
115        }
116    
117        /**
118         * 
119         * @return Size of the order.
120         */
121        public int getSize() {
122            return size;
123        }
124    
125        /**
126         * Sets order size in number of shares.
127         * @param size Order size in number of shares.
128         */
129        public void setSize(int size) {
130            this.size = size;
131        }
132    
133        /**
134         * 
135         * @return Trade side (BUYER/SELLER).
136         */
137        public TradeSideEnum getSide() {
138            return side;
139        }
140    
141        /**
142         * Sets Trade side (BUYER/SELLER).
143         * @param side side.
144         */
145        public void setSide(TradeSideEnum side) {
146            this.side = side;
147        }
148    
149        /**
150         * 
151         * @return Display.
152         */
153        public boolean getDisplay() {
154            return display;
155        }
156    
157        /**
158         * Sets display.
159         * @param display Display.
160         */
161        public void setDisplay(boolean display) {
162            this.display = display;
163        }
164    
165    
166        @Override
167            public String toString() {
168            return "OTBookOrder: timestamp=" + timestamp + ", orderref=" + reference + ", size=" + size + ", sideindicator=" + side + ", display=" + display + ", price=" + price;
169    
170        }
171    
172        @Override
173            public int hashCode() {
174            return safeHashCode(timestamp) 
175                    + 3 * safeHashCode(reference) 
176                    + 5 * safeHashCode(price) 
177                            + 7 * safeHashCode(size) 
178                    + 11 * safeHashCode(side) 
179                    + 13 * safeHashCode(display); 
180        }
181            
182        @Override
183            public boolean equals(Object o) {
184            return equalsTo(this, o);
185        }
186        
187            public int compareTo(OTBookOrder other) {
188                    int rc;
189    
190                    if((rc = safeCompare(timestamp, other.timestamp)) != 0) {
191                            return rc;
192                    }
193                    
194                    if((rc = safeCompare(reference, other.reference)) != 0) {
195                            return rc;
196                    }
197    
198                    if((rc = safeCompare(price, other.price)) != 0) {
199                            return rc;
200                    }
201    
202                    if((rc = safeCompare(size, other.size)) != 0) {
203                            return rc;
204                    }
205                    
206                    if((rc = safeCompare(side, other.side)) != 0) {
207                            return rc;
208                    }
209    
210                    if((rc = safeCompare(display, other.display)) != 0) {
211                            return rc;
212                    }
213    
214                    return 0;
215            }
216    }