From bd1baed0cf912828c9ff81b6bdb14bcb63bc1fcc Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Fri, 22 Nov 2024 17:41:17 +0300 Subject: [PATCH 1/6] test --- .../xchange/dto/marketdata/OrderBook.java | 121 ++++++++++++------ 1 file changed, 79 insertions(+), 42 deletions(-) diff --git a/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java b/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java index ec8eb77b271..0a74bc587e0 100644 --- a/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java +++ b/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java @@ -16,29 +16,43 @@ import org.knowm.xchange.dto.Order.OrderType; import org.knowm.xchange.dto.trade.LimitOrder; import org.knowm.xchange.instrument.Instrument; +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 asks; + /** + * the asks + */ + @Getter + private final List asks; - /** the bids */ - @Getter private final List bids; + /** + * the bids + */ + @Getter + private final List 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( @@ -53,10 +67,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 asks, List bids, boolean sort) { @@ -76,9 +90,9 @@ public OrderBook(Date timeStamp, List asks, List 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 asks, Stream bids) { @@ -89,10 +103,10 @@ public OrderBook(Date timeStamp, Stream asks, Stream 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 asks, Stream bids, boolean sort) { @@ -142,10 +156,14 @@ private void update(List limitOrders, LimitOrder limitOrder) { long writeStamp = lock.tryConvertToWriteLock(stamp); if (writeStamp != 0L) { stamp = writeStamp; - if (idx >= 0) limitOrders.remove(idx); - else idx = -idx - 1; - if (limitOrder.getRemainingAmount().compareTo(BigDecimal.ZERO) != 0) + if (idx >= 0) { + limitOrders.remove(idx); + } else { + idx = -idx - 1; + } + if (limitOrder.getRemainingAmount().compareTo(BigDecimal.ZERO) != 0) { limitOrders.add(idx, limitOrder); + } updateDate(limitOrder.getTimestamp()); break; } else { @@ -153,8 +171,9 @@ private void update(List limitOrders, LimitOrder limitOrder) { stamp = lock.writeLock(); // here wee need to recheck idx, because it is possible that orderBook changed between // unlockRead and lockWrite - if (recheckIdx(limitOrders, limitOrder, idx)) + if (recheckIdx(limitOrders, limitOrder, idx)) { idx = Collections.binarySearch(limitOrders, limitOrder); + } } } } finally { @@ -179,8 +198,11 @@ public void update(OrderBookUpdate orderBookUpdate) { long writeStamp = lock.tryConvertToWriteLock(stamp); if (writeStamp != 0L) { stamp = writeStamp; - if (idx >= 0) limitOrders.remove(idx); - else idx = -idx - 1; + if (idx >= 0) { + limitOrders.remove(idx); + } else { + idx = -idx - 1; + } if (orderBookUpdate.getTotalVolume().compareTo(BigDecimal.ZERO) != 0) { LimitOrder updatedOrder = withAmount(limitOrder, orderBookUpdate.getTotalVolume()); limitOrders.add(idx, updatedOrder); @@ -192,8 +214,9 @@ public void update(OrderBookUpdate orderBookUpdate) { stamp = lock.writeLock(); // here wee need to recheck idx, because it is possible that orderBook changed between // unlockRead and lockWrite - if (recheckIdx(limitOrders, limitOrder, idx)) + if (recheckIdx(limitOrders, limitOrder, idx)) { idx = Collections.binarySearch(limitOrders, limitOrder); + } } } } finally { @@ -201,23 +224,37 @@ public void update(OrderBookUpdate orderBookUpdate) { } } + + private final Logger LOG = LoggerFactory.getLogger(OrderBook.class); + private boolean recheckIdx(List limitOrders, LimitOrder limitOrder, int idx) { - if (idx >= 0) { - // if positive, null check or compare - return limitOrders.get(idx) == null || limitOrders.get(idx).compareTo(limitOrder) != 0; - } else { - // on end of array, null check or one check - if (limitOrders.size() == -idx - 1) { - return limitOrders.get(-idx - 2) == null - || limitOrders.get(-idx - 2).compareTo(limitOrder) >= 0; - } else + try { + if (idx >= 0) { + // if positive, null check or compare + return limitOrders.get(idx) == null || limitOrders.get(idx).compareTo(limitOrder) != 0; + } else { + // on end of array, null check or one check + if (limitOrders.size() == -idx - 1) { + return limitOrders.get(-idx - 2) == null + || limitOrders.get(-idx - 2).compareTo(limitOrder) >= 0; + } else // if negative, check that of limitOrders.get(reversed idx) limitOrders.get(reversed idx-1) // and is lower and bigger than limitOrder - return (limitOrders.get(-idx - 1) == null - || limitOrders.get(-idx - 1).compareTo(limitOrder) <= 0) - && (limitOrders.get(-idx - 2) == null - || limitOrders.get(-idx - 2).compareTo(limitOrder) >= 0); + { + return (limitOrders.get(-idx - 1) == null + || limitOrders.get(-idx - 1).compareTo(limitOrder) <= 0) + && (limitOrders.get(-idx - 2) == null + || limitOrders.get(-idx - 2).compareTo(limitOrder) >= 0); + } + } + } catch (IndexOutOfBoundsException e) { + for(LimitOrder lo:limitOrders) { + LOG.error("limitOrders {}", lo); + } + LOG.error("limitOrder {}", limitOrder); + LOG.error("idx {}", idx); } + return false; } // Replace timeStamp if the provided date is non-null and in the future From a4175352744d435acbb8d31a4af238ca3074412a Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Fri, 22 Nov 2024 21:36:46 +0300 Subject: [PATCH 2/6] In the previous implementation there was a bug if the size of the order array was zero. I did not go through all the check options. And limited myself to only the two most common options. I do not want to make a mistake somewhere again. --- .../xchange/dto/marketdata/OrderBook.java | 43 +++++++++++-------- .../dto/marketdata/ConcurrencyTest.java | 18 ++++---- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java b/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java index 0a74bc587e0..7b9d25e557c 100644 --- a/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java +++ b/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java @@ -170,7 +170,7 @@ private void update(List limitOrders, LimitOrder limitOrder) { lock.unlockRead(stamp); stamp = lock.writeLock(); // here wee need to recheck idx, because it is possible that orderBook changed between - // unlockRead and lockWrite + // unlockRead and writeLock if (recheckIdx(limitOrders, limitOrder, idx)) { idx = Collections.binarySearch(limitOrders, limitOrder); } @@ -227,34 +227,39 @@ 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 limitOrders, LimitOrder limitOrder, int idx) { try { - if (idx >= 0) { - // if positive, null check or compare - return limitOrders.get(idx) == null || limitOrders.get(idx).compareTo(limitOrder) != 0; - } else { - // on end of array, null check or one check - if (limitOrders.size() == -idx - 1) { - return limitOrders.get(-idx - 2) == null - || limitOrders.get(-idx - 2).compareTo(limitOrder) >= 0; - } else - // if negative, check that of limitOrders.get(reversed idx) limitOrders.get(reversed idx-1) - // and is lower and bigger than limitOrder - { - return (limitOrders.get(-idx - 1) == null - || limitOrders.get(-idx - 1).compareTo(limitOrder) <= 0) - && (limitOrders.get(-idx - 2) == null - || limitOrders.get(-idx - 2).compareTo(limitOrder) >= 0); + switch (idx) { + case 0: { + if (!limitOrders.isEmpty()) { + //if not equals, need to recheck + return limitOrders.get(0).compareTo(limitOrder) != 0; + } else { + return true; + } + } + case -1: { + if (limitOrders.isEmpty()) { + return false; + } else { + return limitOrders.get(0).compareTo(limitOrder) <= 0; + } } + default: + return true; } } catch (IndexOutOfBoundsException e) { - for(LimitOrder lo:limitOrders) { + for (LimitOrder lo : limitOrders) { LOG.error("limitOrders {}", lo); } LOG.error("limitOrder {}", limitOrder); LOG.error("idx {}", idx); + throw e; } - return false; } // Replace timeStamp if the provided date is non-null and in the future diff --git a/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/ConcurrencyTest.java b/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/ConcurrencyTest.java index a00d8abcabe..f8312e42b74 100644 --- a/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/ConcurrencyTest.java +++ b/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/ConcurrencyTest.java @@ -20,11 +20,11 @@ public class ConcurrencyTest { public static void main(String[] args) throws InterruptedException, ExecutionException { OrderBook orderBook1 = - new OrderBook(new Date(), initOrderBookAsks(), initOrderBookBids(), true); + new OrderBook(new Date(), initOrderBookAsks(), new ArrayList<>(), true); OrderBook orderBook2 = - new OrderBook(new Date(), initOrderBookAsks(), initOrderBookBids(), true); + new OrderBook(new Date(), initOrderBookAsks(), new ArrayList<>(), true); OrderBookOld orderBookOld = - new OrderBookOld(new Date(), initOrderBookAsks(), initOrderBookBids(), true); + new OrderBookOld(new Date(), initOrderBookAsks(), new ArrayList<>(), true); ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(50); newWay(orderBook1, executor); executor.awaitTermination(100L, TimeUnit.SECONDS); @@ -114,7 +114,7 @@ private static void oldOB(OrderBookOld orderBookOld, ThreadPoolExecutor executor public static void updateOrderBook1(OrderBook orderBook, boolean oldWay) { Random rand = new Random(123); - for (int i = 0; i < 100000; i++) { + for (int i = 0; i < 10000; i++) { OrderBookUpdate orderBookUpdateAsk = new OrderBookUpdate( OrderType.ASK, @@ -145,7 +145,7 @@ public static void updateOrderBook1(OrderBook orderBook, boolean oldWay) { public static void updateOrderBookOld1(OrderBookOld orderBookOld) { Random rand = new Random(123); - for (int i = 0; i < 100000; i++) { + for (int i = 0; i < 10000; i++) { OrderBookUpdate orderBookUpdateAsk = new OrderBookUpdate( OrderType.ASK, @@ -171,7 +171,7 @@ public static void updateOrderBookOld1(OrderBookOld orderBookOld) { private static void updateOrderBook2(OrderBook orderBook, boolean oldWay) { Random rand = new Random(123); - for (int i = 0; i < 100000; i++) { + for (int i = 0; i < 10000; i++) { LimitOrder bookUpdateAsk = new LimitOrder( OrderType.ASK, @@ -202,7 +202,7 @@ private static void updateOrderBook2(OrderBook orderBook, boolean oldWay) { private static void updateOrderBookOld2(OrderBookOld orderBookOld) { Random rand = new Random(123); - for (int i = 0; i < 100000; i++) { + for (int i = 0; i < 10000; i++) { LimitOrder bookUpdateAsk = new LimitOrder( OrderType.ASK, @@ -227,7 +227,7 @@ private static void updateOrderBookOld2(OrderBookOld orderBookOld) { } private static void readOrderBook(OrderBook orderBook, boolean oldWay) { - for (int i = 0; i < 1200000; i++) { + for (int i = 0; i < 10000; i++) { int temp = 0; if (oldWay) { synchronized (orderBook) { @@ -252,7 +252,7 @@ private static void readOrderBook(OrderBook orderBook, boolean oldWay) { } private static void readOrderBookOld(OrderBookOld orderBookOld) { - for (int i = 0; i < 1200000; i++) { + for (int i = 0; i < 120000; i++) { int temp = 0; synchronized (orderBookOld) { for (LimitOrder ask : orderBookOld.getAsks()) { From 885567b5c2a9c74c36cd62bd8243eaf1aa26097a Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Sat, 23 Nov 2024 19:30:13 +0300 Subject: [PATCH 3/6] add tests --- .../xchange/dto/marketdata/OrderBook.java | 15 +------ .../xchange/dto/marketdata/OrderBookTest.java | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java b/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java index 7b9d25e557c..efdd2a8af98 100644 --- a/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java +++ b/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java @@ -232,34 +232,23 @@ public void update(OrderBookUpdate orderBookUpdate) { * @return true, if wee need to run binarySearch again */ private boolean recheckIdx(List limitOrders, LimitOrder limitOrder, int idx) { - try { switch (idx) { case 0: { if (!limitOrders.isEmpty()) { //if not equals, need to recheck return limitOrders.get(0).compareTo(limitOrder) != 0; - } else { + } else return true; - } } case -1: { if (limitOrders.isEmpty()) { return false; - } else { + } else return limitOrders.get(0).compareTo(limitOrder) <= 0; - } } default: return true; } - } catch (IndexOutOfBoundsException e) { - for (LimitOrder lo : limitOrders) { - LOG.error("limitOrders {}", lo); - } - LOG.error("limitOrder {}", limitOrder); - LOG.error("idx {}", idx); - throw e; - } } // Replace timeStamp if the provided date is non-null and in the future diff --git a/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/OrderBookTest.java b/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/OrderBookTest.java index dffe01b43e2..bb0c7da2bd1 100644 --- a/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/OrderBookTest.java +++ b/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/OrderBookTest.java @@ -3,10 +3,13 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.math.BigDecimal; 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; @@ -174,4 +177,41 @@ public void testOrderSorting() { assertThat(book.getAsks().get(0).getLimitPrice()).isEqualTo(new BigDecimal("10")); assertThat(book.getBids().get(0).getLimitPrice()).isEqualTo(new BigDecimal("3")); } + + @Test + public void testRecheckIdx() + throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Class[] cArg = new Class[3]; + cArg[0] = List.class; + cArg[1] = LimitOrder.class; + cArg[2] = int.class; + Method method = OrderBook.class.getDeclaredMethod("recheckIdx", cArg); + method.setAccessible(true); + LimitOrder sameBidOrder = + new LimitOrder( + OrderType.BID, BigDecimal.ONE, CurrencyPair.BTC_USD, "", null, BigDecimal.TEN); + LimitOrder smallerBidOrder = + new LimitOrder( + OrderType.BID, BigDecimal.ONE, CurrencyPair.BTC_USD, "", null, BigDecimal.ONE); + 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(),sameBidOrder, 1)).isEqualTo(true); + //idx=0, empty List + assertThat(method.invoke(orderBook, new ArrayList(),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(),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); + } + } From 9f8ea147f78616c6f4345317569abc54b571e51f Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Tue, 26 Nov 2024 09:31:41 +0300 Subject: [PATCH 4/6] format fix --- .../xchange/dto/marketdata/OrderBook.java | 80 ++++++++----------- .../dto/marketdata/ConcurrencyTest.java | 6 +- .../xchange/dto/marketdata/OrderBookTest.java | 37 ++++----- 3 files changed, 54 insertions(+), 69 deletions(-) diff --git a/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java b/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java index efdd2a8af98..2de7cfb5c91 100644 --- a/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java +++ b/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java @@ -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 asks; + /** the asks */ + @Getter private final List asks; - /** - * the bids - */ - @Getter - private final List bids; + /** the bids */ + @Getter private final List 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( @@ -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 asks, List bids, boolean sort) { @@ -90,9 +78,9 @@ public OrderBook(Date timeStamp, List asks, List 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 asks, Stream bids) { @@ -103,10 +91,10 @@ public OrderBook(Date timeStamp, Stream asks, Stream 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 asks, Stream bids, boolean sort) { @@ -224,31 +212,29 @@ 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 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 diff --git a/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/ConcurrencyTest.java b/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/ConcurrencyTest.java index f8312e42b74..3473d2f3208 100644 --- a/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/ConcurrencyTest.java +++ b/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/ConcurrencyTest.java @@ -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); diff --git a/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/OrderBookTest.java b/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/OrderBookTest.java index bb0c7da2bd1..20906f72d48 100644 --- a/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/OrderBookTest.java +++ b/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/OrderBookTest.java @@ -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; @@ -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(),sameBidOrder, 1)).isEqualTo(true); - //idx=0, empty List - assertThat(method.invoke(orderBook, new ArrayList(),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(),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(), sameBidOrder, 1)) + .isEqualTo(true); + // idx=0, empty List + assertThat(method.invoke(orderBook, new ArrayList(), 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(), 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); } - } From 5abcca8e00b05d8fad088e369fed93ab5c29f18c Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Tue, 26 Nov 2024 09:31:41 +0300 Subject: [PATCH 5/6] format fix --- .../xchange/dto/marketdata/OrderBook.java | 82 ++++++++----------- .../dto/marketdata/ConcurrencyTest.java | 6 +- .../xchange/dto/marketdata/OrderBookTest.java | 37 +++++---- 3 files changed, 54 insertions(+), 71 deletions(-) diff --git a/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java b/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java index efdd2a8af98..8f89d160d65 100644 --- a/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java +++ b/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java @@ -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 asks; + /** the asks */ + @Getter private final List asks; - /** - * the bids - */ - @Getter - private final List bids; + /** the bids */ + @Getter private final List 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( @@ -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 asks, List bids, boolean sort) { @@ -90,9 +78,9 @@ public OrderBook(Date timeStamp, List asks, List 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 asks, Stream bids) { @@ -103,10 +91,10 @@ public OrderBook(Date timeStamp, Stream asks, Stream 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 asks, Stream bids, boolean sort) { @@ -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 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 diff --git a/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/ConcurrencyTest.java b/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/ConcurrencyTest.java index f8312e42b74..3473d2f3208 100644 --- a/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/ConcurrencyTest.java +++ b/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/ConcurrencyTest.java @@ -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); diff --git a/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/OrderBookTest.java b/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/OrderBookTest.java index bb0c7da2bd1..20906f72d48 100644 --- a/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/OrderBookTest.java +++ b/xchange-core/src/test/java/org/knowm/xchange/dto/marketdata/OrderBookTest.java @@ -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; @@ -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(),sameBidOrder, 1)).isEqualTo(true); - //idx=0, empty List - assertThat(method.invoke(orderBook, new ArrayList(),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(),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(), sameBidOrder, 1)) + .isEqualTo(true); + // idx=0, empty List + assertThat(method.invoke(orderBook, new ArrayList(), 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(), 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); } - } From a1bd6c00f5db057fbf77526ca5999844ed2d71bf Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Tue, 26 Nov 2024 09:35:31 +0300 Subject: [PATCH 6/6] fix --- .../main/java/org/knowm/xchange/dto/marketdata/OrderBook.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java b/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java index 8f89d160d65..e7c8adabd3f 100644 --- a/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java +++ b/xchange-core/src/main/java/org/knowm/xchange/dto/marketdata/OrderBook.java @@ -16,8 +16,6 @@ import org.knowm.xchange.dto.Order.OrderType; import org.knowm.xchange.dto.trade.LimitOrder; import org.knowm.xchange.instrument.Instrument; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** DTO representing the exchange order book */ public final class OrderBook implements Serializable {