View Javadoc
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  
17  package com.google.common.collect.testing.features;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.collect.testing.Helpers;
21  import java.lang.annotation.Inherited;
22  import java.lang.annotation.Retention;
23  import java.lang.annotation.RetentionPolicy;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Set;
27  
28  /**
29   * When describing the features of the collection produced by a given generator
30   * (i.e. in a call to {@link
31   * com.google.common.collect.testing.FeatureSpecificTestSuiteBuilder#withFeatures(Feature...)}),
32   * this annotation specifies each of the different sizes for which a test suite
33   * should be built. (In a typical case, the features should include {@link
34   * CollectionSize#ANY}.) These semantics are thus a little different
35   * from those of other Collection-related features such as {@link
36   * CollectionFeature} or {@link SetFeature}.
37   * <p>
38   * However, when {@link CollectionSize.Require} is used to annotate a test it
39   * behaves normally (i.e. it requires the collection instance under test to be
40   * a certain size for the test to run). Note that this means a test should not
41   * require more than one CollectionSize, since a particular collection instance
42   * can only be one size at once.
43   *
44   * @author George van den Driessche
45   */
46  // Enum values use constructors with generic varargs.
47  @SuppressWarnings("unchecked")
48  @GwtCompatible
49  public enum CollectionSize implements Feature<Collection>, Comparable<CollectionSize> {
50    /** Test an empty collection. */
51    ZERO(0),
52    /** Test a one-element collection. */
53    ONE(1),
54    /** Test a three-element collection. */
55    SEVERAL(3),
56    /*
57     * TODO: add VERY_LARGE, noting that we currently assume that the fourth
58     * sample element is not in any collection
59     */
60  
61    ANY(ZERO, ONE, SEVERAL);
62  
63    private final Set<Feature<? super Collection>> implied;
64    private final Integer numElements;
65  
66    CollectionSize(int numElements) {
67      this.implied = Collections.emptySet();
68      this.numElements = numElements;
69    }
70  
71    CollectionSize(Feature<? super Collection>... implied) {
72      // Keep the order here, so that PerCollectionSizeTestSuiteBuilder
73      // gives a predictable order of test suites.
74      this.implied = Helpers.copyToSet(implied);
75      this.numElements = null;
76    }
77  
78    @Override
79    public Set<Feature<? super Collection>> getImpliedFeatures() {
80      return implied;
81    }
82  
83    public int getNumElements() {
84      if (numElements == null) {
85        throw new IllegalStateException(
86            "A compound CollectionSize doesn't specify a number of elements.");
87      }
88      return numElements;
89    }
90  
91    @Retention(RetentionPolicy.RUNTIME)
92    @Inherited
93    @TesterAnnotation
94    public @interface Require {
95      CollectionSize[] value() default {};
96  
97      CollectionSize[] absent() default {};
98    }
99  }