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