MarketDepthLevel implements Comparable<MarketDepthLevel> { public int compareTo(MarketDepthLevel otherLevel ) { if( side == MarketDepthBook.Side.ASK ) { return price.compareTo( otherLevel.getPrice() ); } else { return otherLevel.getPrice().compareTo( price ); } } MarketDepthLevel(MarketDepthBook.Side side, BigDecimal price, int size );  MarketDepthLevel(MarketDepthBook.Side side, BigDecimal price, BigDecimal size ); BigDecimal getPrice(); void setPrice(BigDecimal price); BigDecimal getSize(); void setSize(BigDecimal size); MarketDepthBook.Side getSide(); void setSide(MarketDepthBook.Side side); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); int compareTo(MarketDepthLevel otherLevel );  }
@Test public void testCompareBidLevels() { MarketDepthLevel bidLevel1 = new MarketDepthLevel(MarketDepthBook.Side.BID, BigDecimal.ONE, BigDecimal.ZERO); MarketDepthLevel bidLevel2 = new MarketDepthLevel(MarketDepthBook.Side.BID, BigDecimal.ZERO, BigDecimal.ZERO); assertEquals(-1, bidLevel1.compareTo(bidLevel2)); }
@Test public void testCompareBidLevels_secondHigher() { MarketDepthLevel bidLevel1 = new MarketDepthLevel(MarketDepthBook.Side.BID, BigDecimal.ZERO, BigDecimal.ZERO); MarketDepthLevel bidLevel2 = new MarketDepthLevel(MarketDepthBook.Side.BID, BigDecimal.ONE, BigDecimal.ZERO); assertEquals(1, bidLevel1.compareTo(bidLevel2)); }
@Test public void testCompareAskLevels() { MarketDepthLevel askLevel1 = new MarketDepthLevel(MarketDepthBook.Side.ASK, BigDecimal.ONE, BigDecimal.ZERO); MarketDepthLevel askLevel2 = new MarketDepthLevel(MarketDepthBook.Side.ASK, BigDecimal.ZERO, BigDecimal.ZERO); assertEquals(1, askLevel1.compareTo(askLevel2)); }
@Test public void testCompareAskLevels_secondLower() { MarketDepthLevel askLevel1 = new MarketDepthLevel(MarketDepthBook.Side.ASK, BigDecimal.ZERO, BigDecimal.ZERO); MarketDepthLevel askLevel2 = new MarketDepthLevel(MarketDepthBook.Side.ASK, BigDecimal.ONE, BigDecimal.ZERO); assertEquals(-1, askLevel1.compareTo(askLevel2)); }