001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.features;
018
019import com.google.common.annotations.GwtCompatible;
020import com.google.common.collect.testing.Helpers;
021import java.lang.annotation.Inherited;
022import java.lang.annotation.Retention;
023import java.lang.annotation.RetentionPolicy;
024import java.util.Collection;
025import java.util.Collections;
026import java.util.Set;
027
028/**
029 * When describing the features of the collection produced by a given generator (i.e. in a call to
030 * {@link
031 * com.google.common.collect.testing.FeatureSpecificTestSuiteBuilder#withFeatures(Feature...)}),
032 * this annotation specifies each of the different sizes for which a test suite should be built. (In
033 * a typical case, the features should include {@link CollectionSize#ANY}.) These semantics are thus
034 * a little different from those of other Collection-related features such as {@link
035 * CollectionFeature} or {@link SetFeature}.
036 *
037 * <p>However, when {@link CollectionSize.Require} is used to annotate a test it behaves normally
038 * (i.e. it requires the collection instance under test to be a certain size for the test to run).
039 * Note that this means a test should not require more than one CollectionSize, since a particular
040 * collection instance can only be one size at once.
041 *
042 * @author George van den Driessche
043 */
044// Enum values use constructors with generic varargs.
045@SuppressWarnings("unchecked")
046@GwtCompatible
047public enum CollectionSize implements Feature<Collection>, Comparable<CollectionSize> {
048  /** Test an empty collection. */
049  ZERO(0),
050  /** Test a one-element collection. */
051  ONE(1),
052  /** Test a three-element collection. */
053  SEVERAL(3),
054  /*
055   * TODO: add VERY_LARGE, noting that we currently assume that the fourth
056   * sample element is not in any collection
057   */
058
059  ANY(ZERO, ONE, SEVERAL);
060
061  private final Set<Feature<? super Collection>> implied;
062  private final Integer numElements;
063
064  CollectionSize(int numElements) {
065    this.implied = Collections.emptySet();
066    this.numElements = numElements;
067  }
068
069  CollectionSize(Feature<? super Collection>... implied) {
070    // Keep the order here, so that PerCollectionSizeTestSuiteBuilder
071    // gives a predictable order of test suites.
072    this.implied = Helpers.copyToSet(implied);
073    this.numElements = null;
074  }
075
076  @Override
077  public Set<Feature<? super Collection>> getImpliedFeatures() {
078    return implied;
079  }
080
081  public int getNumElements() {
082    if (numElements == null) {
083      throw new IllegalStateException(
084          "A compound CollectionSize doesn't specify a number of elements.");
085    }
086    return numElements;
087  }
088
089  @Retention(RetentionPolicy.RUNTIME)
090  @Inherited
091  @TesterAnnotation
092  public @interface Require {
093    CollectionSize[] value() default {};
094
095    CollectionSize[] absent() default {};
096  }
097}