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 * The best bid or offer - this is half of a quote.
031 * These get sent when only one side is changing.
032 */
033 public final class OTBBO implements Comparable<OTBBO>, Serializable {
034
035 private static final long serialVersionUID = -7239387622141172391L;
036
037 private Date timestamp;
038 private double price;
039 private int size;
040 private TradeSideEnum side;
041 private String exchange;
042 private String symbol;
043
044 /**
045 * Default constructor.
046 */
047 public OTBBO() { }
048
049 /**
050 * Constructor.
051 *
052 * @param timestamp time of the event.
053 * @param price price.
054 * @param size size.
055 * @param side side.
056 * @param exchange exchange code.
057 * @param symbol symbol code.
058 */
059 public OTBBO(Date timestamp, double price, int size,
060 TradeSideEnum side, String exchange, String symbol) {
061 this.timestamp = timestamp;
062 this.price = price;
063 this.size = size;
064 this.side = side;
065 this.exchange = exchange;
066 this.symbol = symbol;
067 }
068
069 /**
070 * @return timestamp when the event occurred.
071 */
072 public Date getTimestamp() {
073 return timestamp;
074 }
075
076 /**
077 * Sets time of the event.
078 *
079 * @param timestamp Time when the event occurred.
080 */
081 public void setTimestamp(Date timestamp) {
082 this.timestamp = timestamp;
083 }
084
085 /**
086 * @return Price.
087 */
088 public double getPrice() {
089 return price;
090 }
091
092 /**
093 * Sets price.
094 *
095 * @param price Price.
096 */
097 public void setPrice(double price) {
098 this.price = price;
099 }
100
101 /**
102 * @return Size.
103 */
104 public int getSize() {
105 return size;
106 }
107
108 /**
109 * Sets size.
110 *
111 * @param size Size.
112 */
113 public void setSize(int size) {
114 this.size = size;
115 }
116
117 /**
118 * @return Side: B = Bid (buyer side). A = Ask (seller side).
119 */
120 public TradeSideEnum getSide() {
121 return side;
122 }
123
124 /**
125 * Sets side.
126 *
127 * @param side Side: B = Bid (buyer side). A = Ask (seller side).
128 */
129 public void setSide(TradeSideEnum side) {
130 this.side = side;
131 }
132
133 /**
134 * @return Exchange code.
135 */
136 public String getExchange() {
137 return exchange;
138 }
139
140 /**
141 * Sets exchange code.
142 *
143 * @param exchange Exchange code.
144 */
145 public void setExchange(String exchange) {
146 this.exchange = exchange;
147 }
148
149 /**
150 * Sets symbol code.
151 *
152 * @param symbol Symbol code.
153 */
154 public void setSymbol(String symbol) {
155 this.symbol = symbol;
156 }
157
158 /**
159 * @return Symbol code; this field is provided for option chains only, because there can be more than one symbol appropriate to the requested underlyer and expiry date.
160 */
161 public String getSymbol() {
162 return this.symbol;
163 }
164
165 @Override
166 public String toString() {
167 return "OTBBO: timestamp=" + timestamp + ", size="
168 + size + ", side=" + side + ", price=" + price + ", exchange="
169 + exchange + ", symbol=" + symbol;
170
171 }
172
173 @Override
174 public int hashCode() {
175 return safeHashCode(timestamp)
176 + 3 * safeHashCode(size)
177 + 5 * safeHashCode(price)
178 + 7 * safeHashCode(side)
179 + 29 * safeHashCode(exchange)
180 + 31 * safeHashCode(symbol);
181 }
182
183 @Override
184 public boolean equals(Object o) {
185 return equalsTo(this, o);
186 }
187
188 public int compareTo(OTBBO other) {
189 int rc;
190
191 if((rc = safeCompare(timestamp, other.timestamp)) != 0) {
192 return rc;
193 }
194
195 if((rc = safeCompare(size, other.size)) != 0) {
196 return rc;
197 }
198
199 if((rc = safeCompare(price, other.price)) != 0) {
200 return rc;
201 }
202
203 if((rc = safeCompare(side, other.side)) != 0) {
204 return rc;
205 }
206
207 if((rc = safeCompare(exchange, other.exchange)) != 0) {
208 return rc;
209 }
210
211 if((rc = safeCompare(symbol, other.symbol)) != 0) {
212 return rc;
213 }
214
215 return 0;
216 }
217 }