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