Java
127 строк · 4.1 Кб
1/*
2* Copyright (C) 2008 The Guava Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17package com.google.common.collect.testing.features;
18
19import com.google.common.annotations.GwtCompatible;
20import com.google.common.collect.testing.Helpers;
21import java.lang.annotation.Inherited;
22import java.lang.annotation.Retention;
23import java.lang.annotation.RetentionPolicy;
24import java.util.Collection;
25import java.util.LinkedHashSet;
26import java.util.Set;
27import java.util.SortedSet;
28
29/**
30* Optional features of classes derived from {@code Collection}.
31*
32* @author George van den Driessche
33*/
34@SuppressWarnings("rawtypes") // maybe avoidable if we rework the whole package?
35@GwtCompatible
36public enum CollectionFeature implements Feature<Collection> {
37/**
38* The collection must not throw {@code NullPointerException} on calls such as {@code
39* contains(null)} or {@code remove(null)}, but instead must return a simple {@code false}.
40*/
41ALLOWS_NULL_QUERIES,
42ALLOWS_NULL_VALUES(ALLOWS_NULL_QUERIES),
43
44/**
45* Indicates that a collection disallows certain elements (other than {@code null}, whose validity
46* as an element is indicated by the presence or absence of {@link #ALLOWS_NULL_VALUES}). From the
47* documentation for {@link Collection}:
48*
49* <blockquote>
50*
51* "Some collection implementations have restrictions on the elements that they may contain. For
52* example, some implementations prohibit null elements, and some have restrictions on the types
53* of their elements."
54*
55* </blockquote>
56*/
57RESTRICTS_ELEMENTS,
58
59/**
60* Indicates that a collection has a well-defined ordering of its elements. The ordering may
61* depend on the element values, such as a {@link SortedSet}, or on the insertion ordering, such
62* as a {@link LinkedHashSet}. All list tests and sorted-collection tests automatically specify
63* this feature.
64*/
65KNOWN_ORDER,
66
67/**
68* Indicates that a collection has a different {@link Object#toString} representation than most
69* collections. If not specified, the collection tests will examine the value returned by {@link
70* Object#toString}.
71*/
72NON_STANDARD_TOSTRING,
73
74/**
75* Indicates that the constructor or factory method of a collection, usually an immutable set,
76* throws an {@link IllegalArgumentException} when presented with duplicate elements instead of
77* collapsing them to a single element or including duplicate instances in the collection.
78*/
79REJECTS_DUPLICATES_AT_CREATION,
80
81SUPPORTS_ADD,
82SUPPORTS_REMOVE,
83SUPPORTS_ITERATOR_REMOVE,
84FAILS_FAST_ON_CONCURRENT_MODIFICATION,
85
86/**
87* Features supported by general-purpose collections - everything but {@link #RESTRICTS_ELEMENTS}.
88*
89* @see java.util.Collection the definition of general-purpose collections.
90*/
91GENERAL_PURPOSE(SUPPORTS_ADD, SUPPORTS_REMOVE, SUPPORTS_ITERATOR_REMOVE),
92
93/** Features supported by collections where only removal is allowed. */
94REMOVE_OPERATIONS(SUPPORTS_REMOVE, SUPPORTS_ITERATOR_REMOVE),
95
96SERIALIZABLE,
97SERIALIZABLE_INCLUDING_VIEWS(SERIALIZABLE),
98
99SUBSET_VIEW,
100DESCENDING_VIEW,
101
102/**
103* For documenting collections that support no optional features, such as {@link
104* java.util.Collections#emptySet}
105*/
106NONE;
107
108private final Set<Feature<? super Collection>> implied;
109
110CollectionFeature(Feature<? super Collection>... implied) {
111this.implied = Helpers.copyToSet(implied);
112}
113
114@Override
115public Set<Feature<? super Collection>> getImpliedFeatures() {
116return implied;
117}
118
119@Retention(RetentionPolicy.RUNTIME)
120@Inherited
121@TesterAnnotation
122public @interface Require {
123CollectionFeature[] value() default {};
124
125CollectionFeature[] absent() default {};
126}
127}
128