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 book price level is the processed output of a bookServer.
031     * The bookServer maintains the depth of book for ECN data, 
032     * keeping track of each order.
033     * In the bookServer orders of the same price are consolidated 
034     * into price levels, and a Book Price Level message is 
035     * issued whenever the size of a price level changes. Even
036     * though we maintain the book on all orders only the top 
037     * book levels are sent as Book
038     * Price Level events. 
039     * Typically the top six levels are sent for both the 
040     * buy and sell sides.
041     */
042    public final class OTBookPriceLevel implements Comparable<OTBookPriceLevel>, Serializable {
043            
044            private static final long serialVersionUID = 5859804510929854117L;
045    
046            private Date timestamp;
047        private double price;
048        private int size;
049        private TradeSideEnum side;
050        private String levelID;
051    
052        /**
053         * Default constructor.
054         */
055        public OTBookPriceLevel() { }
056    
057        /**
058         * Constructor.
059         * @param timestamp Time when the event occurred.
060         * @param price Price.
061         * @param size Number of shares.
062         * @param side      Side: B = Bid, S = Sell.
063         * @param levelID Unique level identifier.
064         */
065        public OTBookPriceLevel(Date timestamp, double price, 
066                    int size, TradeSideEnum side, String levelID) {
067            this.timestamp = timestamp;
068            this.price = price;
069            this.size = size;
070            this.side = side;
071            this.levelID = levelID;
072        }
073    
074        /**
075         * 
076         * @return Time when the event occurred.
077         */
078        public Date getTimestamp() {
079            return timestamp;
080        }
081    
082        /**
083         * Sets time of the event.
084         * @param timestamp Time when the event occurred.
085         */
086        public void setTimestamp(Date timestamp) {
087            this.timestamp = timestamp;
088        }
089    
090        /**
091         * 
092         * @return Price.
093         */
094        public double getPrice() {
095            return price;
096        }
097    
098        /**
099         * Sets price.
100         * @param price Price.
101         */
102        public void setPrice(double price) {
103            this.price = price;
104        }
105    
106        /**
107         * 
108         * @return Number of shares.
109         */
110        public int getSize() {
111            return size;
112        }
113    
114        /**
115         * Sets number of shares.
116         * @param size Number of shares.
117         */
118        public void setSize(int size) {
119            this.size = size;
120        }
121    
122        /**
123         * 
124         * @return Side (BUYER/SELLER).
125         */
126        public TradeSideEnum getSide() {
127            return side;
128        }
129    
130        /**
131         * Sets side: BUYER(Bid) or SELLER(Ask). 
132         * param side side.
133         */
134        public void setSide(TradeSideEnum side) {
135            this.side = side;
136        }
137    
138        /**
139         * 
140         * @return Unique level identifier.
141         */
142        public String getLevelId() {
143            return levelID;
144        }
145    
146        /**
147         * Sets unique level identifier. 
148         * @param levelID Unique level identifier.
149         */
150        public void setLevelId(String levelID) {
151            this.levelID = levelID;
152        }
153    
154        @Override
155            public String toString() {
156            return "OTBookPriceLevel: timestamp=" + timestamp + ", size=" + size + ", sideindicator=" + side + ", levelid=" + levelID + ", price=" + price;
157        }
158    
159        @Override
160            public int hashCode() {
161             return safeHashCode(timestamp) 
162                    + 3 * safeHashCode(price) 
163                    + 5 * safeHashCode(size) 
164                            + 7 * safeHashCode(side) 
165                    + 11 * safeHashCode(levelID);
166        }
167            
168        @Override
169            public boolean equals(Object o) {
170            return equalsTo(this, o);
171        }
172        
173            public int compareTo(OTBookPriceLevel other) {
174                    int rc;
175    
176                    if((rc = safeCompare(timestamp, other.timestamp)) != 0) {
177                            return rc;
178                    }
179                    
180                    if((rc = safeCompare(price, other.price)) != 0) {
181                            return rc;
182                    }
183    
184                    if((rc = safeCompare(size, other.size)) != 0) {
185                            return rc;
186                    }
187                    
188                    if((rc = safeCompare(side, other.side)) != 0) {
189                            return rc;
190                    }
191    
192                    if((rc = safeCompare(levelID, other.levelID)) != 0) {
193                            return rc;
194                    }
195    
196                    return 0;
197            }
198    }