示例#1
0
 public ListTag(Type elementType) {
   checkNotNull(elementType);
   this.elementType = elementType;
   List<Tag> baseList = Lists.newArrayList();
   Constraint<Tag> constraint = new TypeConstraint();
   value = Constraints.constrainedList(baseList, constraint);
 }
示例#2
0
 public ListTag(Collection<? extends Tag> collection) {
   checkArgument(!collection.isEmpty());
   List<Tag> baseList = Lists.newArrayList(collection);
   elementType = baseList.get(0).getTagType();
   Constraint<Tag> constraint = new TypeConstraint();
   for (Tag tag : baseList) {
     constraint.checkElement(tag);
   }
   value = Constraints.constrainedList(baseList, constraint);
 }
示例#3
0
  public void testRemoveIf_randomAccess_notPermittingDuplicates() {
    // https://github.com/google/guava/issues/1596
    final List<String> delegate = newArrayList("a", "b", "c", "d", "e");
    List<String> uniqueList =
        Constraints.constrainedList(
            delegate,
            new Constraint<String>() {
              @Override
              public String checkElement(String element) {
                checkArgument(
                    !delegate.contains(element), "this list does not permit duplicate elements");
                return element;
              }
            });

    assertTrue(uniqueList instanceof RandomAccess);
    assertTrue(
        Iterables.removeIf(
            uniqueList,
            new Predicate<String>() {
              @Override
              public boolean apply(String s) {
                return s.equals("b") || s.equals("d") || s.equals("f");
              }
            }));
    assertEquals(newArrayList("a", "c", "e"), uniqueList);
    assertFalse(
        Iterables.removeIf(
            uniqueList,
            new Predicate<String>() {
              @Override
              public boolean apply(String s) {
                return s.equals("x") || s.equals("y") || s.equals("z");
              }
            }));
    assertEquals(newArrayList("a", "c", "e"), uniqueList);
  }
 public static <T> List<T> newNotNullLinkedList() {
   return Constraints.constrainedList(new LinkedList<T>(), Constraints.notNull());
 }