コード例 #1
0
  @Test
  public void setKeyPreservation() {
    Key key = new Key("key");

    Key duplicateKey1 = new Key("key");
    ImmutableSet<Key> set1 = Sets.immutable.of(key, duplicateKey1);
    Verify.assertSize(1, set1);
    Verify.assertContains(key, set1);
    Assert.assertSame(key, set1.getFirst());

    Key duplicateKey2 = new Key("key");
    ImmutableSet<Key> set2 = Sets.immutable.of(key, duplicateKey1, duplicateKey2);
    Verify.assertSize(1, set2);
    Verify.assertContains(key, set2);
    Assert.assertSame(key, set2.getFirst());

    Key duplicateKey3 = new Key("key");
    ImmutableSet<Key> set3 = Sets.immutable.of(key, new Key("not a dupe"), duplicateKey3);
    Verify.assertSize(2, set3);
    Verify.assertContainsAll("immutable set", set3, key, new Key("not a dupe"));
    Assert.assertSame(key, set3.detect(key::equals));

    Key duplicateKey4 = new Key("key");
    ImmutableSet<Key> set4 =
        Sets.immutable.of(key, new Key("not a dupe"), duplicateKey3, duplicateKey4);
    Verify.assertSize(2, set4);
    Verify.assertContainsAll("immutable set", set4, key, new Key("not a dupe"));
    Assert.assertSame(key, set4.detect(key::equals));

    ImmutableSet<Key> set5 =
        Sets.immutable.of(key, new Key("not a dupe"), new Key("me neither"), duplicateKey4);
    Verify.assertSize(3, set5);
    Verify.assertContainsAll(
        "immutable set", set5, key, new Key("not a dupe"), new Key("me neither"));
    Assert.assertSame(key, set5.detect(key::equals));

    ImmutableSet<Key> set6 = Sets.immutable.of(key, duplicateKey2, duplicateKey3, duplicateKey4);
    Verify.assertSize(1, set6);
    Verify.assertContains(key, set6);
    Assert.assertSame(key, set6.detect(key::equals));
  }