@Test
  public void retainAllFromEntrySet() {
    MutableMapIterable<String, Integer> map =
        this.newMapWithKeysValues("One", 1, "Two", 2, "Three", 3);
    Assert.assertFalse(
        map.entrySet()
            .retainAll(
                FastList.newListWith(
                    ImmutableEntry.of("One", 1),
                    ImmutableEntry.of("Two", 2),
                    ImmutableEntry.of("Three", 3),
                    ImmutableEntry.of("Four", 4))));

    Assert.assertTrue(
        map.entrySet()
            .retainAll(
                FastList.newListWith(
                    ImmutableEntry.of("One", 1),
                    ImmutableEntry.of("Three", 3),
                    ImmutableEntry.of("Four", 4))));
    Assert.assertEquals(UnifiedMap.newWithKeysValues("One", 1, "Three", 3), map);

    MutableMapIterable<Integer, Integer> integers = this.newMapWithKeysValues(1, 1, 2, 2, 3, 3);
    Integer copy = new Integer(1);
    Assert.assertTrue(integers.entrySet().retainAll(mList(ImmutableEntry.of(copy, copy))));
    Assert.assertEquals(iMap(copy, copy), integers);
    Assert.assertNotSame(copy, Iterate.getOnly(integers.entrySet()).getKey());
    Assert.assertNotSame(copy, Iterate.getOnly(integers.entrySet()).getValue());
  }
  @Test
  public void entry_hashCodeForNullKeyAndValue() {
    MutableMap<Integer, String> map = this.newMapWithKeyValue(null, null);
    Map.Entry<Integer, String> entry = Iterate.getFirst(map.entrySet());

    Assert.assertEquals(0, entry.hashCode());
  }
  public <T> ImmutableBag<T> withAll(Iterable<? extends T> items) {
    if (items instanceof ImmutableBag<?>) {
      return (ImmutableBag<T>) items;
    }

    return this.of((T[]) Iterate.toArray(items));
  }
  @Test
  public void entry_equalsWithNonEntry() {
    MutableMap<Integer, String> map = this.newMapWithKeyValue(null, null);
    Map.Entry<Integer, String> entry = Iterate.getFirst(map.entrySet());

    Verify.assertNotEquals(entry, new Object());
  }
 @Test
 public void updateValue() {
   MutableMapIterable<Integer, Integer> map = this.newMap();
   Iterate.forEach(
       Interval.oneTo(1000), each -> map.updateValue(each % 10, () -> 0, integer -> integer + 1));
   Assert.assertEquals(Interval.zeroTo(9).toSet(), map.keySet());
   Assert.assertEquals(
       FastList.newList(Collections.nCopies(10, 100)), FastList.newList(map.values()));
 }
 @Test
 public void rejectKeysMultiValues() {
   Multimap<String, Integer> multimap =
       this.newMultimapWithKeysValues("One", 1, "One", 12, "Two", 2, "Two", 3);
   Multimap<String, Integer> rejectedMultimap =
       multimap.rejectKeysMultiValues(
           (key, values) -> "Two".equals(key) && Iterate.contains(values, 2));
   Assert.assertEquals(this.newMultimapWithKeysValues("One", 1, "One", 12), rejectedMultimap);
 }
 @Test
 public void updateValue_collisions() {
   MutableMapIterable<Integer, Integer> map = this.newMap();
   MutableList<Integer> list = Interval.oneTo(2000).toList();
   Collections.shuffle(list);
   Iterate.forEach(list, each -> map.updateValue(each % 1000, () -> 0, integer -> integer + 1));
   Assert.assertEquals(Interval.zeroTo(999).toSet(), map.keySet());
   Assert.assertEquals(
       HashBag.newBag(map.values()).toStringOfItemToCount(),
       FastList.newList(Collections.nCopies(1000, 2)),
       FastList.newList(map.values()));
 }
  @Test
  public void entry_setValue() {
    MutableMap<Integer, String> map = this.newMapWithKeyValue(1, "One");
    Map.Entry<Integer, String> entry = Iterate.getFirst(map.entrySet());
    String value = "Ninety-Nine";
    Assert.assertEquals("One", entry.setValue(value));
    Assert.assertEquals(value, entry.getValue());
    Verify.assertContainsKeyValue(1, value, map);

    map.remove(1);
    Verify.assertEmpty(map);
    Assert.assertNull(entry.setValue("Ignored"));
  }
 @Test
 public void updateValueWith() {
   MutableMapIterable<Integer, Integer> map = this.newMap();
   Iterate.forEach(
       Interval.oneTo(1000),
       each ->
           map.updateValueWith(
               each % 10,
               () -> 0,
               (integer, parameter) -> {
                 Assert.assertEquals("test", parameter);
                 return integer + 1;
               },
               "test"));
   Assert.assertEquals(Interval.zeroTo(9).toSet(), map.keySet());
   Assert.assertEquals(
       FastList.newList(Collections.nCopies(10, 100)), FastList.newList(map.values()));
 }
 /** @see ConnectionFactory#refreshMetadata(Collection) */
 @Override
 public void refreshMetadata(Collection<String> topics) {
   try {
     this.lock.writeLock().lock();
     String brokerAddressesAsString =
         ListIterate.collect(this.configuration.getBrokerAddresses(), Functions.getToString())
             .makeString(",");
     Seq<Broker> brokers = null;
     try {
       brokers = ClientUtils$.MODULE$.parseBrokerList(brokerAddressesAsString);
     } catch (Exception e) {
       throw new IllegalStateException(
           "Can not parse Kafka Brokers for: [" + brokerAddressesAsString + "]", e);
     }
     TopicMetadataResponse topicMetadataResponse =
         new TopicMetadataResponse(
             ClientUtils$.MODULE$.fetchTopicMetadata(
                 JavaConversions.asScalaSet(new HashSet<>(topics)),
                 brokers,
                 this.configuration.getClientId(),
                 this.configuration.getFetchMetadataTimeout(),
                 0));
     PartitionIterable<TopicMetadata> selectWithoutErrors =
         Iterate.partition(
             topicMetadataResponse.topicsMetadata(), errorlessTopicMetadataPredicate);
     this.metadataCacheHolder.set(
         this.metadataCacheHolder.get().merge(selectWithoutErrors.getSelected()));
     if (log.isInfoEnabled()) {
       for (TopicMetadata topicMetadata : selectWithoutErrors.getRejected()) {
         log.info(
             String.format("No metadata could be retrieved for '%s'", topicMetadata.topic()),
             ErrorMapping.exceptionFor(topicMetadata.errorCode()));
       }
     }
   } finally {
     this.lock.writeLock().unlock();
   }
 }
 /** @see ConnectionFactory#getLeaders(Iterable) */
 @Override
 public Map<Partition, BrokerAddress> getLeaders(Iterable<Partition> partitions) {
   return Iterate.toMap(
       partitions, Functions.<Partition>getPassThru(), getBrokersByPartitionFunction);
 }
 public boolean containsAllIterable(Iterable<?> source) {
   return Iterate.allSatisfyWith(source, Predicates2.in(), this);
 }
 public boolean addAllIterable(Iterable<? extends T> iterable) {
   return Iterate.addAllIterable(iterable, this);
 }
 public <V extends Comparable<? super V>> T maxBy(Function<? super T, ? extends V> function) {
   return Iterate.max(this, Comparators.byFunction(function));
 }
 public T max() {
   return Iterate.max(this);
 }
 public T min() {
   return Iterate.min(this);
 }
 public T max(Comparator<? super T> comparator) {
   return Iterate.max(this, comparator);
 }
Beispiel #18
0
 public static <E> HashBag<E> newBag(Iterable<? extends E> source) {
   return Iterate.addAllTo(source, HashBag.<E>newBag());
 }
 @Override
 public boolean containsAll(Collection<?> collection) {
   return Iterate.allSatisfy(collection, Predicates.in(this.items));
 }
 public static <E> ImmutableArrayList<E> newList(Iterable<? extends E> iterable) {
   return new ImmutableArrayList<E>((E[]) Iterate.toArray(iterable));
 }