@Test public void addOverlappedRange() throws Exception { builder.add(Range.create(5, true, 10, true), 1); builder.add(Range.create(0, true, 20, true), 2); RangeLookupTable<Integer, Integer> table = builder.build(); LookupItem<Integer, Integer> match; for (int val = 0; val <= 20; val++) { int expected = val < 5 || val > 10 ? 2 : 1; match = table.getLookupItem(val); assertNotNull(match); assertEquals(expected, match.getValue().intValue()); } }
public LookupItem<T, U> getLookupItem(T srcValue) { if (items.isEmpty()) { return null; } else { int lo = 0; int hi = items.size() - 1; while (hi >= lo) { int mid = lo + (hi - lo) / 2; LookupItem<T, U> item = items.get(mid); Range<T> r = item.getRange(); if (r.contains(srcValue)) { return item; } else if (!r.isMinNegInf() && NumberOperations.compare(srcValue, r.getMin()) <= 0) { hi = mid - 1; } else { lo = mid + 1; } } return null; } }
RangeLookupTable { public LookupItem<T, U> getLookupItem(T srcValue) { if (items.isEmpty()) { return null; } else { int lo = 0; int hi = items.size() - 1; while (hi >= lo) { int mid = lo + (hi - lo) / 2; LookupItem<T, U> item = items.get(mid); Range<T> r = item.getRange(); if (r.contains(srcValue)) { return item; } else if (!r.isMinNegInf() && NumberOperations.compare(srcValue, r.getMin()) <= 0) { hi = mid - 1; } else { lo = mid + 1; } } return null; } } }
RangeLookupTable { public LookupItem<T, U> getLookupItem(T srcValue) { if (items.isEmpty()) { return null; } else { int lo = 0; int hi = items.size() - 1; while (hi >= lo) { int mid = lo + (hi - lo) / 2; LookupItem<T, U> item = items.get(mid); Range<T> r = item.getRange(); if (r.contains(srcValue)) { return item; } else if (!r.isMinNegInf() && NumberOperations.compare(srcValue, r.getMin()) <= 0) { hi = mid - 1; } else { lo = mid + 1; } } return null; } } private  RangeLookupTable(Builder builder); }
RangeLookupTable { public LookupItem<T, U> getLookupItem(T srcValue) { if (items.isEmpty()) { return null; } else { int lo = 0; int hi = items.size() - 1; while (hi >= lo) { int mid = lo + (hi - lo) / 2; LookupItem<T, U> item = items.get(mid); Range<T> r = item.getRange(); if (r.contains(srcValue)) { return item; } else if (!r.isMinNegInf() && NumberOperations.compare(srcValue, r.getMin()) <= 0) { hi = mid - 1; } else { lo = mid + 1; } } return null; } } private  RangeLookupTable(Builder builder); LookupItem<T, U> getLookupItem(T srcValue); @Override String toString(); }
RangeLookupTable { public LookupItem<T, U> getLookupItem(T srcValue) { if (items.isEmpty()) { return null; } else { int lo = 0; int hi = items.size() - 1; while (hi >= lo) { int mid = lo + (hi - lo) / 2; LookupItem<T, U> item = items.get(mid); Range<T> r = item.getRange(); if (r.contains(srcValue)) { return item; } else if (!r.isMinNegInf() && NumberOperations.compare(srcValue, r.getMin()) <= 0) { hi = mid - 1; } else { lo = mid + 1; } } return null; } } private  RangeLookupTable(Builder builder); LookupItem<T, U> getLookupItem(T srcValue); @Override String toString();  }