コード例 #1
0
 @Test
 public void keyValuePairsView() {
   Multimap<Integer, String> multimap = this.newMultimapWithKeysValues(1, "1", 2, "2", 3, "3");
   Assert.assertEquals(
       Bags.mutable.of(Tuples.pair(1, "1"), Tuples.pair(2, "2"), Tuples.pair(3, "3")),
       multimap.keyValuePairsView().toBag());
 }
コード例 #2
0
  @Test
  public void splitAtIndex() {
    final String oompaLoompa = "oompaloompa";

    Assert.assertEquals(Tuples.twin("oompa", "loompa"), StringIterate.splitAtIndex(oompaLoompa, 5));
    Assert.assertEquals(Tuples.twin("", oompaLoompa), StringIterate.splitAtIndex(oompaLoompa, 0));
    Assert.assertEquals(
        Tuples.twin(oompaLoompa, ""),
        StringIterate.splitAtIndex(oompaLoompa, oompaLoompa.length()));

    Assert.assertEquals(Tuples.twin("", ""), StringIterate.splitAtIndex("", 0));

    Verify.assertThrows(
        StringIndexOutOfBoundsException.class,
        new Runnable() {
          public void run() {
            StringIterate.splitAtIndex(oompaLoompa, 17);
          }
        });

    Verify.assertThrows(
        StringIndexOutOfBoundsException.class,
        new Runnable() {
          public void run() {
            StringIterate.splitAtIndex(oompaLoompa, -8);
          }
        });
  }
コード例 #3
0
 @Test
 public void withAllKeyValueArguments() {
   MutableMapIterable<String, Integer> map = this.newMapWithKeysValues("A", 1, "B", 2);
   MutableMapIterable<String, Integer> mapWith =
       map.withAllKeyValueArguments(Tuples.pair("B", 22), Tuples.pair("C", 3));
   Assert.assertSame(map, mapWith);
   Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 1, "B", 22, "C", 3), mapWith);
 }
コード例 #4
0
  @Test
  public void add() {
    MutableMapIterable<String, Integer> map = this.newMapWithKeyValue("A", 1);

    Assert.assertEquals(Integer.valueOf(1), map.add(Tuples.pair("A", 3)));
    Assert.assertNull(map.add(Tuples.pair("B", 2)));
    Verify.assertMapsEqual(UnifiedMap.newWithKeysValues("A", 3, "B", 2), map);
  }
コード例 #5
0
 public RichIterable<Pair<K, V>> keyValuesView() {
   return Lists.immutable
       .<Pair<K, V>>of(
           Tuples.pair(this.key1, this.value1),
           Tuples.pair(this.key2, this.value2),
           Tuples.pair(this.key3, this.value3))
       .asLazy();
 }
コード例 #6
0
 @Test
 public void newMultimapFromPairs() {
   Multimap<Integer, String> expected =
       this.newMultimapWithKeysValues(1, "One", 2, "Two", 3, "Three", 4, "Four");
   Multimap<Integer, String> actual =
       this.newMultimap(
           Tuples.pair(1, "One"),
           Tuples.pair(2, "Two"),
           Tuples.pair(3, "Three"),
           Tuples.pair(4, "Four"));
   Assert.assertEquals(expected, actual);
 }
コード例 #7
0
 @Test
 public void forEachKeyMultiValue() {
   MutableSet<Pair<Integer, Iterable<String>>> collection = UnifiedSet.newSet();
   Multimap<Integer, String> multimap =
       this.newMultimapWithKeysValues(2, "2", 2, "1", 3, "3", 3, "3");
   multimap.forEachKeyMultiValues((key, values) -> collection.add(Tuples.pair(key, values)));
   Assert.assertEquals(
       UnifiedSet.newSetWith(
           Tuples.pair(2, this.createCollection("2", "1")),
           Tuples.pair(3, this.createCollection("3", "3"))),
       collection);
 }
コード例 #8
0
 @Override
 public Pair<K, V> detect(Predicate2<? super K, ? super V> predicate) {
   if (predicate.accept(this.key1, this.value1)) {
     return Tuples.pair(this.key1, this.value1);
   }
   if (predicate.accept(this.key2, this.value2)) {
     return Tuples.pair(this.key2, this.value2);
   }
   if (predicate.accept(this.key3, this.value3)) {
     return Tuples.pair(this.key3, this.value3);
   }
   return null;
 }
コード例 #9
0
  @Test
  public void testNewMultimapWith() {
    Pair<Integer, String> pair1 = Tuples.pair(1, "One");
    Pair<Integer, String> pair2 = Tuples.pair(2, "Two");
    Pair<Integer, String> pair3 = Tuples.pair(3, "Three");
    Pair<Integer, String> pair4 = Tuples.pair(4, "Four");
    ListIterable<Pair<Integer, String>> pairs = FastList.newListWith(pair1, pair2, pair3, pair4);

    Multimap<Integer, String> expected = this.newMultimap(pair1, pair2, pair3, pair4);

    Multimap<Integer, String> actual = this.newMultimapFromPairs(pairs);
    Assert.assertEquals(expected, actual);
  }
コード例 #10
0
  @Test
  public void collect() {
    ImmutableMap<String, String> map =
        this.newMapWithKeysValues("1", "One", "2", "Two", "3", "Three", "4", "Four");
    ImmutableMap<Integer, String> result =
        map.collect(
            (Function2<String, String, Pair<Integer, String>>)
                (argument1, argument2) ->
                    Tuples.pair(
                        Integer.valueOf(argument1),
                        argument1 + ':' + new StringBuilder(argument2).reverse()));

    switch (map.size()) {
      case 0:
        Verify.assertEmpty(result);
        break;
      case 1:
        Verify.assertContainsKeyValue(1, "1:enO", result);
        break;
      case 2:
        Verify.assertContainsAllKeyValues(result, 1, "1:enO", 2, "2:owT");
        break;
      case 3:
        Verify.assertContainsAllKeyValues(result, 1, "1:enO", 2, "2:owT", 3, "3:eerhT");
        break;
      case 4:
        Verify.assertContainsAllKeyValues(
            result, 1, "1:enO", 2, "2:owT", 3, "3:eerhT", 4, "4:ruoF");
        break;
      default:
        Assert.fail();
    }
  }
コード例 #11
0
  @Test
  public void collect() {
    MutableMap<String, String> map = this.classUnderTest();
    Function2<String, String, Pair<Integer, String>> function =
        (argument1, argument2) ->
            Tuples.pair(
                Integer.valueOf(argument1),
                argument1 + ':' + new StringBuilder(argument2).reverse());
    MutableMap<Integer, String> result = map.collect(function);

    switch (map.size()) {
      case 0:
        Verify.assertEmpty(result);
        break;
      case 1:
        Verify.assertContainsKeyValue(1, "1:enO", result);
        break;
      case 2:
        Verify.assertContainsAllKeyValues(result, 1, "1:enO", 2, "2:owT");
        break;
      case 3:
        Verify.assertContainsAllKeyValues(result, 1, "1:enO", 2, "2:owT", 3, "3:eerhT");
        break;
      default:
        Assert.fail();
    }
  }
コード例 #12
0
  public void setValue(String concept, String uri) {
    this.uri = uri;
    if (concept.equals(this.concept)) return;

    this.concept = concept;
    setName(Tuples.pair(id, concept));
  }
コード例 #13
0
  @Test
  public void forEachInBoth() {
    final MutableList<Pair<String, String>> list = Lists.mutable.of();
    ArrayList<String> list1 = new ArrayList<String>(mList("1", "2"));
    ArrayList<String> list2 = new ArrayList<String>(mList("a", "b"));
    ArrayListIterate.forEachInBoth(
        list1,
        list2,
        new Procedure2<String, String>() {
          public void value(String argument1, String argument2) {
            list.add(Tuples.twin(argument1, argument2));
          }
        });

    Assert.assertEquals(FastList.newListWith(Tuples.twin("1", "a"), Tuples.twin("2", "b")), list);
  }
コード例 #14
0
  @Test
  public void groupBy() {
    ImmutableMap<String, Integer> map = this.newMapWithKeysValues("1", 1, "2", 2, "3", 3, "4", 4);

    Function<Integer, Boolean> isOddFunction = object -> IntegerPredicates.isOdd().accept(object);

    Multimap<Boolean, Integer> expected;

    switch (map.size()) {
      case 1:
        expected = FastListMultimap.newMultimap(Tuples.pair(Boolean.TRUE, 1));
        break;
      case 2:
        expected =
            FastListMultimap.newMultimap(
                Tuples.pair(Boolean.TRUE, 1), Tuples.pair(Boolean.FALSE, 2));
        break;
      case 3:
        expected =
            FastListMultimap.newMultimap(
                Tuples.pair(Boolean.TRUE, 1),
                Tuples.pair(Boolean.TRUE, 3),
                Tuples.pair(Boolean.FALSE, 2));
        break;
      case 4:
        expected =
            FastListMultimap.newMultimap(
                Tuples.pair(Boolean.TRUE, 1),
                Tuples.pair(Boolean.TRUE, 3),
                Tuples.pair(Boolean.FALSE, 2),
                Tuples.pair(Boolean.FALSE, 4));
        break;
      default:
        expected = FastListMultimap.newMultimap();
        break;
    }

    Multimap<Boolean, Integer> actual = map.groupBy(isOddFunction);
    Assert.assertEquals(HashBagMultimap.newMultimap(expected), HashBagMultimap.newMultimap(actual));

    Multimap<Boolean, Integer> actualFromTarget =
        map.groupBy(isOddFunction, FastListMultimap.<Boolean, Integer>newMultimap());
    Assert.assertEquals(
        HashBagMultimap.newMultimap(expected), HashBagMultimap.newMultimap(actualFromTarget));
  }
コード例 #15
0
 @Test
 public void collectKeysValues() {
   Multimap<String, Integer> multimap =
       this.newMultimapWithKeysValues("1", 1, "1", 12, "2", 2, "3", 3);
   Multimap<Integer, String> collectedMultimap =
       multimap.collectKeysValues(
           (key, value) -> Tuples.pair(Integer.valueOf(key), value + "Value"));
   Multimap<Integer, String> expectedMultimap =
       this.newMultimapWithKeysValues(1, "1Value", 1, "12Value", 2, "2Value", 3, "3Value");
   Assert.assertEquals(expectedMultimap, collectedMultimap);
 }
コード例 #16
0
 @Test
 public void forEachWithOccurrences() {
   final MutableList<Pair<Object, Integer>> list = Lists.mutable.of();
   this.getCollection()
       .forEachWithOccurrences(
           new ObjectIntProcedure<Object>() {
             public void value(Object each, int index) {
               list.add(Tuples.pair(each, index));
             }
           });
   Assert.assertEquals(FastList.newListWith(Tuples.pair("", 1)), list);
 }
コード例 #17
0
ファイル: Trainer.java プロジェクト: irwenqiang/ml-1
 private static <T> Pair<List<T>, List<T>> splitData(List<T> original, double splitForSecond) {
   List<T> first = new ArrayList<>();
   List<T> second = new ArrayList<>();
   if (splitForSecond > 0.0) {
     Collections.shuffle(original, new Random(0L));
     int numFirst = (int) ((1.0 - splitForSecond) * original.size());
     first.addAll(original.subList(0, numFirst));
     second.addAll(original.subList(numFirst, original.size()));
   } else {
     first.addAll(original);
     // second stays empty
   }
   return Tuples.pair(first, second);
 }
コード例 #18
0
  @Test
  public void nonUniqueWith() {
    Twin<String> twin1 = Tuples.twin("1", "1");
    Twin<String> twin2 = Tuples.twin("2", "2");
    Twin<String> twin3 = Tuples.twin("3", "3");
    Twin<String> twin4 = Tuples.twin("4", "4");
    QuadrupletonSet<Twin<String>> set = new QuadrupletonSet<>(twin1, twin2, twin3, twin4);

    set.with(Tuples.twin("1", "1"));
    set.with(Tuples.twin("2", "2"));
    set.with(Tuples.twin("3", "3"));
    set.with(Tuples.twin("4", "4"));

    Assert.assertSame(set.getFirst(), twin1);
    Assert.assertSame(set.getSecond(), twin2);
    Assert.assertSame(set.getThird(), twin3);
    Assert.assertSame(set.getLast(), twin4);
  }