<K, V> void testNavigationAgainstExpected(
     NavigableMap<K, V> expected, NavigableMap<K, V> navigableMap, Iterable<K> keysToTest) {
   for (K key : keysToTest) {
     assertEquals(expected.lowerEntry(key), navigableMap.lowerEntry(key));
     assertEquals(expected.floorEntry(key), navigableMap.floorEntry(key));
     assertEquals(expected.ceilingEntry(key), navigableMap.ceilingEntry(key));
     assertEquals(expected.higherEntry(key), navigableMap.higherEntry(key));
     for (boolean inclusive : new boolean[] {false, true}) {
       ASSERT
           .that(navigableMap.headMap(key, inclusive).entrySet())
           .has()
           .allFrom(expected.headMap(key, inclusive).entrySet())
           .inOrder();
       ASSERT
           .that(navigableMap.tailMap(key, inclusive).entrySet())
           .has()
           .allFrom(expected.tailMap(key, inclusive).entrySet())
           .inOrder();
       ASSERT
           .that(navigableMap.headMap(key, inclusive).descendingMap().entrySet())
           .has()
           .allFrom(expected.headMap(key, inclusive).descendingMap().entrySet())
           .inOrder();
       ASSERT
           .that(navigableMap.tailMap(key, inclusive).descendingMap().entrySet())
           .has()
           .allFrom(expected.tailMap(key, inclusive).descendingMap().entrySet())
           .inOrder();
     }
   }
 }
示例#2
0
文件: OCPAgent.java 项目: ccbon/ocp
  public Queue<Contact> makeContactQueue(Id key) throws Exception {
    Queue<Contact> contactQueue = new LinkedList<Contact>();
    NavigableMap<Id, OCPContact> nodeMap = new TreeMap<Id, OCPContact>(this.nodeMap);
    if (nodeMap.containsKey(key)) {
      contactQueue.offer(nodeMap.get(key));
    }

    NavigableMap<Id, OCPContact> s = nodeMap.headMap(key, false);
    Iterator<Id> it = s.navigableKeySet().descendingIterator();
    while (it.hasNext()) {
      Id nodeId = it.next();
      OCPContact contact = s.get(nodeId);
      if (!contactQueue.contains(contact)) {
        contactQueue.offer(contact);
      }
    }
    s = nodeMap.tailMap(key, false);
    it = s.navigableKeySet().descendingIterator();
    while (it.hasNext()) {
      Id nodeId = it.next();
      OCPContact contact = s.get(nodeId);
      if (!contactQueue.contains(contact)) {
        contactQueue.offer(contact);
      }
    }
    return contactQueue;
  }
 @Override
 NavigableMap<K, V> createSubMap(SortedMap<K, V> sortedMap, K firstExclusive, K lastExclusive) {
   NavigableMap<K, V> map = (NavigableMap<K, V>) sortedMap;
   if (from == Bound.NO_BOUND && to == Bound.INCLUSIVE) {
     return map.headMap(lastInclusive, true);
   } else if (from == Bound.EXCLUSIVE && to == Bound.NO_BOUND) {
     return map.tailMap(firstExclusive, false);
   } else if (from == Bound.EXCLUSIVE && to == Bound.EXCLUSIVE) {
     return map.subMap(firstExclusive, false, lastExclusive, false);
   } else if (from == Bound.EXCLUSIVE && to == Bound.INCLUSIVE) {
     return map.subMap(firstExclusive, false, lastInclusive, true);
   } else if (from == Bound.INCLUSIVE && to == Bound.INCLUSIVE) {
     return map.subMap(firstInclusive, true, lastInclusive, true);
   } else {
     return (NavigableMap<K, V>) super.createSubMap(map, firstExclusive, lastExclusive);
   }
 }
示例#4
0
  /**
   * lowInclusive and highInclusive are ignored if the respective bound in null (unbounded).
   *
   * @param low
   * @param lowInclusive
   * @param high
   * @param highInclusive
   * @return
   */
  public RangeApacheLogEntryIterator getIterator(
      Date low, Date high, boolean lowInclusive, boolean highInclusive) {
    NavigableMap<Date, File> subMap = dateToFile;

    Date adjustedLow = low == null ? null : dateToFile.floorKey(low);
    Date adjustedHigh = high == null ? null : dateToFile.ceilingKey(high);

    // lower bound
    if (adjustedLow != null) {
      subMap = subMap.tailMap(adjustedLow, true);
    }

    // upperbound
    if (adjustedHigh != null) {
      subMap = subMap.headMap(adjustedHigh, true);
    }

    logger.debug(
        "Adjust: Creating an iterator from "
            + adjustedLow
            + " until "
            + adjustedHigh
            + "; spanning "
            + subMap.size()
            + " files.");
    logger.debug(
        "Creating an iterator from "
            + low
            + " until "
            + high
            + "; spanning "
            + subMap.size()
            + " files.");

    return new RangeApacheLogEntryIterator(
        subMap.entrySet().iterator(), low, lowInclusive, high, highInclusive);
  }
 public void testHeadMap_K_B() {
   NavigableMap<String, Integer> map = create();
   NavigableMap<String, Integer> headMap = map.headMap("a", true);
   assertTrue(headMap instanceof SynchronizedNavigableMap);
   assertSame(mutex, ((SynchronizedNavigableMap<String, Integer>) headMap).mutex);
 }