コード例 #1
0
 @Override
 public boolean contains(Object obj) {
   return Comparators.nullSafeEquals(obj, this.element1)
       || Comparators.nullSafeEquals(obj, this.element2)
       || Comparators.nullSafeEquals(obj, this.element3)
       || Comparators.nullSafeEquals(obj, this.element4);
 }
コード例 #2
0
 @Test
 public void toSortedSetWithComparator() {
   ImmutableCollection<Integer> integers = this.classUnderTest();
   MutableSortedSet<Integer> set =
       integers.toSortedSet(Comparators.<Integer>reverseNaturalOrder());
   Assert.assertEquals(integers.toSet(), set);
   Assert.assertEquals(
       integers.toSortedList(Comparators.<Integer>reverseNaturalOrder()), set.toList());
 }
コード例 #3
0
 @Test
 public void valuesCollection_toArray_withLargeTarget() {
   MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two");
   String[] target = new String[3];
   target[2] = "yow!";
   String[] values = map.values().toArray(target);
   ArrayIterate.sort(
       values, values.length, Comparators.safeNullsHigh(Comparators.<String>naturalOrder()));
   Assert.assertArrayEquals(new String[] {"One", "Two", null}, values);
 }
コード例 #4
0
 @Test
 public void keySet_ToArray_withLargeTarget() {
   MutableMap<Integer, String> map =
       this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three", 4, "Four");
   Integer[] target = new Integer[6]; // deliberately large to force the extra to be set to null
   target[4] = 42;
   target[5] = 42;
   Integer[] result = map.keySet().toArray(target);
   ArrayIterate.sort(
       result, result.length, Comparators.safeNullsHigh(Comparators.<Integer>naturalOrder()));
   Assert.assertArrayEquals(new Integer[] {1, 2, 3, 4, 42, null}, result);
 }
コード例 #5
0
 public V get(Object key) {
   if (Comparators.nullSafeEquals(this.key3, key)) {
     return this.value3;
   }
   if (Comparators.nullSafeEquals(this.key2, key)) {
     return this.value2;
   }
   if (Comparators.nullSafeEquals(this.key1, key)) {
     return this.value1;
   }
   return null;
 }
コード例 #6
0
 public MutableSet<T> without(T element) {
   if (Comparators.nullSafeEquals(element, this.element1)) {
     return new DoubletonSet<T>(this.element2, this.element3);
   }
   if (Comparators.nullSafeEquals(element, this.element2)) {
     return new DoubletonSet<T>(this.element1, this.element3);
   }
   if (Comparators.nullSafeEquals(element, this.element3)) {
     return new DoubletonSet<T>(this.element1, this.element2);
   }
   return this;
 }
コード例 #7
0
 @Override
 @Test
 public void toSortedMap_with_comparator() {
   MutableSortedMap<String, String> map =
       this.newBag()
           .toSortedMap(
               Comparators.<String>reverseNaturalOrder(),
               Functions.getStringPassThru(),
               Functions.getStringPassThru());
   Verify.assertEmpty(map);
   Verify.assertInstanceOf(TreeSortedMap.class, map);
   Assert.assertEquals(Comparators.<String>reverseNaturalOrder(), map.comparator());
 }
コード例 #8
0
 @Test
 public void toSortedMap_with_comparator() {
   LazyIterable<Integer> integers = this.newWith(1, 2, 3);
   MutableSortedMap<Integer, String> map =
       integers.toSortedMap(
           Comparators.<Integer>reverseNaturalOrder(),
           Functions.getIntegerPassThru(),
           Functions.getToString());
   Verify.assertMapsEqual(
       TreeSortedMap.newMapWith(
           Comparators.<Integer>reverseNaturalOrder(), 1, "1", 2, "2", 3, "3"),
       map);
   Verify.assertListsEqual(FastList.newListWith(3, 2, 1), map.keySet().toList());
 }
コード例 #9
0
  @Test
  public void toSortedBag_empty() {
    ImmutableBag<String> immutableBag = Bags.immutable.of();

    MutableSortedBag<String> sortedBag =
        immutableBag.toSortedBag(Comparators.reverseNaturalOrder());
    sortedBag.addOccurrences("apple", 3);
    sortedBag.addOccurrences("orange", 2);

    Verify.assertSortedBagsEqual(
        TreeBag.newBagWith(
            Comparators.reverseNaturalOrder(), "orange", "orange", "apple", "apple", "apple"),
        sortedBag);
  }
コード例 #10
0
 public static <
         T,
         V1 extends Comparable<? super V1>,
         V2 extends Comparable<? super V2>,
         V3 extends Comparable<? super V3>>
     SerializableComparator<T> fromFunctions(
         Function<? super T, ? extends V1> one,
         Function<? super T, ? extends V2> two,
         Function<? super T, ? extends V3> three) {
   return Comparators.chain(
       Comparators.<T, V1>byFunction(one),
       Comparators.<T, V2>byFunction(two),
       Comparators.<T, V3>byFunction(three));
 }
コード例 #11
0
  public <K, V> ImmutableMap<K, V> with(K key1, V value1, K key2, V value2, K key3, V value3) {
    if (Comparators.nullSafeEquals(key1, key2) && Comparators.nullSafeEquals(key2, key3)) {
      return this.of(key1, value3);
    }
    if (Comparators.nullSafeEquals(key1, key2)) {
      return this.of(key1, value2, key3, value3);
    }
    if (Comparators.nullSafeEquals(key1, key3)) {
      return this.of(key2, value2, key1, value3);
    }
    if (Comparators.nullSafeEquals(key2, key3)) {
      return this.of(key1, value1, key2, value3);
    }

    return new ImmutableTripletonMap<K, V>(key1, value1, key2, value2, key3, value3);
  }
コード例 #12
0
 @Test
 public void sortingWitoutAccessToInternalArray() {
   ThisIsNotAnArrayList<Integer> arrayListThatIsnt =
       new ThisIsNotAnArrayList<Integer>(FastList.newListWith(5, 3, 4, 1, 2));
   Verify.assertStartsWith(
       ArrayListIterate.sortThis(arrayListThatIsnt, Comparators.naturalOrder()), 1, 2, 3, 4, 5);
 }
コード例 #13
0
 @Override
 public ImmutableSet<T> newWithout(T element) {
   if (this.contains(element)) {
     if (Comparators.nullSafeEquals(element, this.element1)) {
       return Sets.immutable.of(this.element2, this.element3, this.element4);
     }
     if (Comparators.nullSafeEquals(element, this.element2)) {
       return Sets.immutable.of(this.element1, this.element3, this.element4);
     }
     if (Comparators.nullSafeEquals(element, this.element3)) {
       return Sets.immutable.of(this.element1, this.element2, this.element4);
     }
     return Sets.immutable.of(this.element1, this.element2, this.element3);
   }
   return this;
 }
コード例 #14
0
 @Test
 public void containsKey() {
   ImmutableSortedMap<Integer, String> map = this.classUnderTest();
   ImmutableSortedMap<Integer, String> revMap =
       this.classUnderTest(Comparators.<Integer>reverseNaturalOrder());
   Assert.assertFalse(map.containsKey(0));
   Assert.assertFalse(revMap.containsKey(1));
 }
コード例 #15
0
  @Override
  @Test
  public void toSortedBagBy() {
    ImmutableBag<String> immutableBag = this.newBag();
    MutableSortedBag<String> sortedBag = immutableBag.toSortedBagBy(String::valueOf);
    TreeBag<Object> expectedBag = TreeBag.newBag(Comparators.byFunction(String::valueOf));

    Verify.assertSortedBagsEqual(expectedBag, sortedBag);
  }
コード例 #16
0
  @Test
  public void keySet_copyKeys() {
    // a map with a null key
    MutableMap<Integer, Integer> map1 = this.newMapWithKeyValue(null, 0);
    Assert.assertArrayEquals(new Object[] {null}, map1.keySet().toArray());

    // a map with a chain containing empty slots
    MutableMap<Integer, Integer> map2 = this.mapWithCollisionsOfSize(5);
    Assert.assertArrayEquals(new Object[] {0, 17, 34, 51, 68}, map2.keySet().toArray());

    // a map with a chain containing empty slots and null key
    MutableMap<Integer, Integer> map3 = this.mapWithCollisionsOfSize(5);
    map3.put(null, 42);
    Integer[] array = map3.keySet().toArray(new Integer[map3.size()]);
    ArrayIterate.sort(
        array, array.length, Comparators.safeNullsHigh(Comparators.<Integer>naturalOrder()));
    Assert.assertArrayEquals(new Object[] {0, 17, 34, 51, 68, null}, array);
  }
コード例 #17
0
 @Override
 public void equalsAndHashCode() {
   super.equalsAndHashCode();
   Verify.assertInstanceOf(
       UnmodifiableSortedBag.class,
       SerializeTestHelper.serializeDeserialize(
           this.newWith(Comparators.reverseNaturalOrder(), 1, 2, 3)));
   Verify.assertInstanceOf(
       UnmodifiableSortedBag.class,
       SerializeTestHelper.serializeDeserialize(this.newWith(1, 2, 3)));
 }
コード例 #18
0
 @Benchmark
 public void parallel_eager_forkjoin_gsc() {
   MutableMultimap<Alphagram, String> groupBy = FJIterate.groupBy(this.gscWords, Alphagram::new);
   groupBy
       .multiValuesView()
       .select(iterable -> iterable.size() >= SIZE_THRESHOLD)
       .toSortedList(Comparators.<RichIterable<String>>byIntFunction(RichIterable::size))
       .asReversed()
       .collect(iterable -> iterable.size() + ": " + iterable)
       .forEach(Procedures.cast(e -> Assert.assertFalse(e.isEmpty())));
 }
コード例 #19
0
  @Test
  public void toSortedBagBy_empty() {
    ImmutableBag<Integer> immutableBag = Bags.immutable.of();

    Function<Integer, Integer> function = object -> object * -1;
    MutableSortedBag<Integer> sortedBag = immutableBag.toSortedBagBy(function);
    sortedBag.addOccurrences(1, 3);
    sortedBag.addOccurrences(10, 2);

    Verify.assertSortedBagsEqual(
        TreeBag.newBagWith(Comparators.byFunction(function), 10, 10, 1, 1, 1), sortedBag);
  }
コード例 #20
0
 public static <T, V extends Comparable<? super V>> SerializableComparator<T> byFunction(
     Function<? super T, ? extends V> function) {
   if (function instanceof IntFunction) {
     return Functions.toIntComparator((IntFunction<T>) function);
   }
   if (function instanceof DoubleFunction) {
     return Functions.toDoubleComparator((DoubleFunction<T>) function);
   }
   if (function instanceof LongFunction) {
     return Functions.toLongComparator((LongFunction<T>) function);
   }
   return Comparators.byFunction(function, naturalOrder());
 }
コード例 #21
0
  @Test
  public void toSortedMap_with_comparator() {
    MutableSortedMap<Integer, String> map =
        this.newBag()
            .toSortedMap(
                Comparators.<Integer>reverseNaturalOrder(),
                Integer::valueOf,
                Functions.<String>getPassThru());

    Verify.assertMapsEqual(
        this.newBag().toMap(Integer::valueOf, Functions.<String>getPassThru()), map);
    Verify.assertListsEqual(Interval.fromTo(this.numKeys(), 1), map.keySet().toList());
  }
コード例 #22
0
 @Benchmark
 public void parallel_lazy_gsc() {
   ParallelUnsortedBag<String> parallelUnsortedBag =
       this.gscWords.asParallel(this.executorService, BATCH_SIZE);
   UnsortedBagMultimap<Alphagram, String> groupBy = parallelUnsortedBag.groupBy(Alphagram::new);
   groupBy
       .multiValuesView()
       .select(iterable -> iterable.size() >= SIZE_THRESHOLD)
       .toSortedList(Comparators.<RichIterable<String>>byIntFunction(RichIterable::size))
       .asReversed()
       .collect(iterable -> iterable.size() + ": " + iterable)
       .forEach(Procedures.cast(e -> Assert.assertFalse(e.isEmpty())));
 }
コード例 #23
0
  @Override
  @Test
  public void toArray() {
    super.toArray();

    int size = COLLISIONS.size();
    for (int i = 1; i < size; i++) {
      MutableSet<Integer> set =
          UnifiedSetWithHashingStrategy.newSet(INTEGER_HASHING_STRATEGY, SIZE)
              .withAll(COLLISIONS.subList(0, i));
      Object[] objects = set.toArray();
      Assert.assertEquals(set, UnifiedSet.newSetWith(objects));
    }

    MutableSet<Integer> deepChain =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY,
            COLLISION_1,
            COLLISION_2,
            COLLISION_3,
            COLLISION_4,
            COLLISION_5,
            COLLISION_6);
    Assert.assertArrayEquals(
        new Integer[] {
          COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6
        },
        deepChain.toArray());

    MutableSet<Integer> minimumChain =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY, COLLISION_1, COLLISION_2);
    minimumChain.remove(COLLISION_2);
    Assert.assertArrayEquals(new Integer[] {COLLISION_1}, minimumChain.toArray());

    MutableSet<Integer> set =
        UnifiedSetWithHashingStrategy.newSetWith(
            INTEGER_HASHING_STRATEGY, COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
    Integer[] target = {
      Integer.valueOf(1),
      Integer.valueOf(1),
      Integer.valueOf(1),
      Integer.valueOf(1),
      Integer.valueOf(1),
      Integer.valueOf(1)
    };
    Integer[] actual = set.toArray(target);
    ArrayIterate.sort(actual, actual.length, Comparators.safeNullsHigh(Integer::compareTo));
    Assert.assertArrayEquals(
        new Integer[] {COLLISION_1, 1, COLLISION_2, COLLISION_3, COLLISION_4, null}, actual);
  }
コード例 #24
0
  @Override
  @Test
  public void iterator() {
    MutableSortedBag<Integer> bag = this.newWith(-1, 0, 1, 1, 2);
    Iterator<Integer> iterator = bag.iterator();
    Assert.assertTrue(iterator.hasNext());
    Assert.assertEquals(Integer.valueOf(-1), iterator.next());
    Assert.assertTrue(iterator.hasNext());
    Assert.assertEquals(Integer.valueOf(0), iterator.next());
    Assert.assertTrue(iterator.hasNext());
    Assert.assertEquals(Integer.valueOf(1), iterator.next());
    Assert.assertTrue(iterator.hasNext());
    Assert.assertEquals(Integer.valueOf(1), iterator.next());
    Assert.assertTrue(iterator.hasNext());
    Assert.assertEquals(Integer.valueOf(2), iterator.next());
    Assert.assertFalse(iterator.hasNext());

    MutableSortedBag<Integer> revBag =
        this.newWith(Comparators.reverseNaturalOrder(), -1, 0, 1, 1, 2);
    Iterator<Integer> revIterator = revBag.iterator();
    Assert.assertTrue(revIterator.hasNext());
    Assert.assertEquals(Integer.valueOf(2), revIterator.next());
    Assert.assertTrue(revIterator.hasNext());
    Assert.assertEquals(Integer.valueOf(1), revIterator.next());
    Assert.assertTrue(revIterator.hasNext());
    Assert.assertEquals(Integer.valueOf(1), revIterator.next());
    Assert.assertTrue(revIterator.hasNext());
    Assert.assertEquals(Integer.valueOf(0), revIterator.next());
    Assert.assertTrue(revIterator.hasNext());
    Assert.assertEquals(Integer.valueOf(-1), revIterator.next());
    Assert.assertFalse(revIterator.hasNext());

    Iterator<Integer> iterator3 =
        this.newWith(Comparators.reverseNaturalOrder(), 2, 1, 1, 0, -1).iterator();
    Verify.assertThrows(UnsupportedOperationException.class, iterator3::remove);
    Assert.assertEquals(Integer.valueOf(2), iterator3.next());
    Verify.assertThrows(UnsupportedOperationException.class, iterator3::remove);
  }
コード例 #25
0
 @Warmup(iterations = 20)
 @Measurement(iterations = 10)
 @Benchmark
 public void serial_eager_gsc() {
   MutableListMultimap<Alphagram, String> groupBy =
       this.gscWords.groupBy(Alphagram::new, FastListMultimap.newMultimap());
   groupBy
       .multiValuesView()
       .select(iterable -> iterable.size() >= SIZE_THRESHOLD)
       .toSortedList(Comparators.<RichIterable<String>>byIntFunction(RichIterable::size))
       .asReversed()
       .collect(iterable -> iterable.size() + ": " + iterable)
       .forEach(Procedures.cast(e -> Assert.assertFalse(e.isEmpty())));
 }
コード例 #26
0
  @Override
  @Test
  public void rejectMap() {
    ImmutableSortedMap<Integer, String> map = this.classUnderTest();
    ImmutableSortedMap<Integer, String> actual = map.reject(Predicates2.alwaysFalse());
    Verify.assertInstanceOf(ImmutableEmptySortedMap.class, actual);
    Assert.assertSame(ImmutableEmptySortedMap.INSTANCE, actual);

    ImmutableSortedMap<Integer, String> revMap =
        this.classUnderTest(Comparators.<Integer>reverseNaturalOrder());
    ImmutableSortedMap<Integer, String> revActual = revMap.reject(Predicates2.alwaysTrue());
    Verify.assertInstanceOf(ImmutableEmptySortedMap.class, revActual);
    Assert.assertSame(revMap.comparator(), revActual.comparator());
  }
コード例 #27
0
  @Override
  @Test
  public void collectMap() {
    ImmutableSortedMap<Integer, String> map = this.classUnderTest();
    ImmutableSortedMap<Integer, String> revMap =
        this.classUnderTest(Comparators.<Integer>reverseNaturalOrder());

    Function2<Integer, String, Pair<Integer, String>> alwaysTrueFunction = Tuples::pair;
    ImmutableMap<Integer, String> collect = map.collect(alwaysTrueFunction);
    ImmutableMap<Integer, String> revCollect = revMap.collect(alwaysTrueFunction);

    Verify.assertEmpty(collect);
    Assert.assertSame(collect, revCollect);
  }
コード例 #28
0
 @Override
 protected boolean randomAccessListEquals(List<?> otherList) {
   if (this.size() != otherList.size()) {
     return false;
   }
   for (int i = 0; i < this.size(); i++) {
     T one = this.items[i];
     Object two = otherList.get(i);
     if (!Comparators.nullSafeEquals(one, two)) {
       return false;
     }
   }
   return true;
 }
コード例 #29
0
  @Test
  public void serialization() {
    ImmutableSortedMap<Integer, String> map = this.classUnderTest();
    ImmutableSortedMap<Integer, String> deserialized =
        SerializeTestHelper.serializeDeserialize(map);
    Assert.assertSame(ImmutableEmptySortedMap.INSTANCE, map);
    Assert.assertSame(map, deserialized);

    ImmutableSortedMap<Integer, String> revMap =
        this.classUnderTest(Comparators.<Integer>reverseNaturalOrder());
    ImmutableSortedMap<Integer, String> revDeserialized =
        SerializeTestHelper.serializeDeserialize(revMap);
    Verify.assertInstanceOf(ImmutableSortedMap.class, revDeserialized);
    Assert.assertNotNull(revDeserialized.comparator());
  }
コード例 #30
0
 @Override
 protected boolean regularListEquals(List<?> otherList) {
   Iterator<?> iterator = otherList.iterator();
   for (int i = 0; i < this.size(); i++) {
     T one = this.items[i];
     if (!iterator.hasNext()) {
       return false;
     }
     Object two = iterator.next();
     if (!Comparators.nullSafeEquals(one, two)) {
       return false;
     }
   }
   return !iterator.hasNext();
 }