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;
027import org.checkerframework.checker.nullness.qual.Nullable;
028
029/**
030 * When describing the features of the collection produced by a given generator (i.e. in a call to
031 * {@link
032 * com.google.common.collect.testing.FeatureSpecificTestSuiteBuilder#withFeatures(Feature...)}),
033 * this annotation specifies each of the different sizes for which a test suite should be built. (In
034 * a typical case, the features should include {@link CollectionSize#ANY}.) These semantics are thus
035 * a little different from those of other Collection-related features such as {@link
036 * CollectionFeature} or {@link SetFeature}.
037 *
038 * <p>However, when {@link CollectionSize.Require} is used to annotate a test it behaves normally
039 * (i.e. it requires the collection instance under test to be a certain size for the test to run).
040 * Note that this means a test should not require more than one CollectionSize, since a particular
041 * collection instance can only be one size at once.
042 *
043 * @author George van den Driessche
044 */
045// Enum values use constructors with generic varargs.
046@SuppressWarnings("unchecked")
047@GwtCompatible
048public enum CollectionSize implements Feature<Collection>, Comparable<CollectionSize> {
049  /** Test an empty collection. */
050  ZERO(0),
051  /** Test a one-element collection. */
052  ONE(1),
053  /** Test a three-element collection. */
054  SEVERAL(3),
055  /*
056   * TODO: add VERY_LARGE, noting that we currently assume that the fourth
057   * sample element is not in any collection
058   */
059
060  ANY(ZERO, ONE, SEVERAL);
061
062  private final Set<Feature<? super Collection>> implied;
063  private final @Nullable Integer numElements;
064
065  CollectionSize(int numElements) {
066    this.implied = Collections.emptySet();
067    this.numElements = numElements;
068  }
069
070  CollectionSize(Feature<? super Collection>... implied) {
071    // Keep the order here, so that PerCollectionSizeTestSuiteBuilder
072    // gives a predictable order of test suites.
073    this.implied = Helpers.copyToSet(implied);
074    this.numElements = null;
075  }
076
077  @Override
078  public Set<Feature<? super Collection>> getImpliedFeatures() {
079    return implied;
080  }
081
082  public int getNumElements() {
083    if (numElements == null) {
084      throw new IllegalStateException(
085          "A compound CollectionSize doesn't specify a number of elements.");
086    }
087    return numElements;
088  }
089
090  @Retention(RetentionPolicy.RUNTIME)
091  @Inherited
092  @TesterAnnotation
093  public @interface Require {
094    CollectionSize[] value() default {};
095
096    CollectionSize[] absent() default {};
097  }
098}