Skip to content

Commit

Permalink
format fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rizer1980 committed Nov 26, 2024
1 parent 885567b commit 5abcca8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,28 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* DTO representing the exchange order book
*/
/** DTO representing the exchange order book */
public final class OrderBook implements Serializable {

private static final long serialVersionUID = -7788306758114464314L;
@JsonIgnore
public final StampedLock lock = new StampedLock();
@JsonIgnore public final StampedLock lock = new StampedLock();

/**
* the asks
*/
@Getter
private final List<LimitOrder> asks;
/** the asks */
@Getter private final List<LimitOrder> asks;

/**
* the bids
*/
@Getter
private final List<LimitOrder> bids;
/** the bids */
@Getter private final List<LimitOrder> bids;

/**
* the timestamp of the orderbook according to the exchange's server, null if not provided
*/
@Getter
private Date timeStamp;
/** the timestamp of the orderbook according to the exchange's server, null if not provided */
@Getter private Date timeStamp;

/**
* Constructor
*
* @param timeStamp - the timestamp of the orderbook according to the exchange's server, null if
* not provided
* @param asks The ASK orders
* @param bids The BID orders
* not provided
* @param asks The ASK orders
* @param bids The BID orders
*/
@JsonCreator
public OrderBook(
Expand All @@ -67,10 +55,10 @@ public OrderBook(
* Constructor
*
* @param timeStamp - the timestamp of the orderbook according to the exchange's server, null if
* not provided
* @param asks The ASK orders
* @param bids The BID orders
* @param sort True if the asks and bids need to be sorted
* not provided
* @param asks The ASK orders
* @param bids The BID orders
* @param sort True if the asks and bids need to be sorted
*/
public OrderBook(Date timeStamp, List<LimitOrder> asks, List<LimitOrder> bids, boolean sort) {

Expand All @@ -90,9 +78,9 @@ public OrderBook(Date timeStamp, List<LimitOrder> asks, List<LimitOrder> bids, b
* Constructor
*
* @param timeStamp - the timestamp of the orderbook according to the exchange's server, null if
* not provided
* @param asks The ASK orders
* @param bids The BID orders
* not provided
* @param asks The ASK orders
* @param bids The BID orders
*/
public OrderBook(Date timeStamp, Stream<LimitOrder> asks, Stream<LimitOrder> bids) {

Expand All @@ -103,10 +91,10 @@ public OrderBook(Date timeStamp, Stream<LimitOrder> asks, Stream<LimitOrder> bid
* Constructor
*
* @param timeStamp - the timestamp of the orderbook according to the exchange's server, null if
* not provided
* @param asks The ASK orders
* @param bids The BID orders
* @param sort True if the asks and bids need to be sorted
* not provided
* @param asks The ASK orders
* @param bids The BID orders
* @param sort True if the asks and bids need to be sorted
*/
public OrderBook(Date timeStamp, Stream<LimitOrder> asks, Stream<LimitOrder> bids, boolean sort) {

Expand Down Expand Up @@ -224,31 +212,27 @@ public void update(OrderBookUpdate orderBookUpdate) {
}
}


private final Logger LOG = LoggerFactory.getLogger(OrderBook.class);


/**
* @return true, if wee need to run binarySearch again
*/
private boolean recheckIdx(List<LimitOrder> limitOrders, LimitOrder limitOrder, int idx) {
switch (idx) {
case 0: {
switch (idx) {
case 0:
{
if (!limitOrders.isEmpty()) {
//if not equals, need to recheck
// if not equals, need to recheck
return limitOrders.get(0).compareTo(limitOrder) != 0;
} else
return true;
} else return true;
}
case -1: {
case -1:
{
if (limitOrders.isEmpty()) {
return false;
} else
return limitOrders.get(0).compareTo(limitOrder) <= 0;
} else return limitOrders.get(0).compareTo(limitOrder) <= 0;
}
default:
return true;
}
default:
return true;
}
}

// Replace timeStamp if the provided date is non-null and in the future
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public class ConcurrencyTest {
static Instrument inst = new CurrencyPair("BTC/USDT");

public static void main(String[] args) throws InterruptedException, ExecutionException {
OrderBook orderBook1 =
new OrderBook(new Date(), initOrderBookAsks(), new ArrayList<>(), true);
OrderBook orderBook2 =
new OrderBook(new Date(), initOrderBookAsks(), new ArrayList<>(), true);
OrderBook orderBook1 = new OrderBook(new Date(), initOrderBookAsks(), new ArrayList<>(), true);
OrderBook orderBook2 = new OrderBook(new Date(), initOrderBookAsks(), new ArrayList<>(), true);
OrderBookOld orderBookOld =
new OrderBookOld(new Date(), initOrderBookAsks(), new ArrayList<>(), true);
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(50);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.junit.Before;
Expand Down Expand Up @@ -196,22 +195,24 @@ public void testRecheckIdx()
LimitOrder higherBidOrder =
new LimitOrder(
OrderType.BID, BigDecimal.ONE, CurrencyPair.BTC_USD, "", null, new BigDecimal("10.5"));
//idx!= -1,0
assertThat(method.invoke(orderBook, new ArrayList<LimitOrder>(),sameBidOrder, 1)).isEqualTo(true);
//idx=0, empty List
assertThat(method.invoke(orderBook, new ArrayList<LimitOrder>(),sameBidOrder, 0)).isEqualTo(true);
//idx=0, order equals
assertThat(method.invoke(orderBook, orderBook.getBids(),sameBidOrder, 0)).isEqualTo(false);
//idx=0, smallerBidOrder
assertThat(method.invoke(orderBook, orderBook.getBids(),smallerBidOrder, 0)).isEqualTo(true);
//idx=-1, empty List
assertThat(method.invoke(orderBook, new ArrayList<LimitOrder>(),sameBidOrder, -1)).isEqualTo(false);
//idx=-1, order equals
assertThat(method.invoke(orderBook, orderBook.getBids(),sameBidOrder, -1)).isEqualTo(true);
//idx=-1, smaller order
assertThat(method.invoke(orderBook, orderBook.getBids(),smallerBidOrder, -1)).isEqualTo(true);
//idx=-1, higher order
assertThat(method.invoke(orderBook, orderBook.getBids(),higherBidOrder, -1)).isEqualTo(false);
// idx!= -1,0
assertThat(method.invoke(orderBook, new ArrayList<LimitOrder>(), sameBidOrder, 1))
.isEqualTo(true);
// idx=0, empty List
assertThat(method.invoke(orderBook, new ArrayList<LimitOrder>(), sameBidOrder, 0))
.isEqualTo(true);
// idx=0, order equals
assertThat(method.invoke(orderBook, orderBook.getBids(), sameBidOrder, 0)).isEqualTo(false);
// idx=0, smallerBidOrder
assertThat(method.invoke(orderBook, orderBook.getBids(), smallerBidOrder, 0)).isEqualTo(true);
// idx=-1, empty List
assertThat(method.invoke(orderBook, new ArrayList<LimitOrder>(), sameBidOrder, -1))
.isEqualTo(false);
// idx=-1, order equals
assertThat(method.invoke(orderBook, orderBook.getBids(), sameBidOrder, -1)).isEqualTo(true);
// idx=-1, smaller order
assertThat(method.invoke(orderBook, orderBook.getBids(), smallerBidOrder, -1)).isEqualTo(true);
// idx=-1, higher order
assertThat(method.invoke(orderBook, orderBook.getBids(), higherBidOrder, -1)).isEqualTo(false);
}

}

0 comments on commit 5abcca8

Please sign in to comment.