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 static com.google.common.collect.testing.Helpers.copyToSet; 020import static java.util.Collections.emptySet; 021 022import com.google.common.annotations.GwtCompatible; 023import java.lang.annotation.Inherited; 024import java.lang.annotation.Retention; 025import java.lang.annotation.RetentionPolicy; 026import java.util.Collection; 027import java.util.Set; 028import org.jspecify.annotations.Nullable; 029 030/** 031 * When describing the features of the collection produced by a given generator (i.e. in a call to 032 * {@link 033 * com.google.common.collect.testing.FeatureSpecificTestSuiteBuilder#withFeatures(Feature...)}), 034 * this annotation specifies each of the different sizes for which a test suite should be built. (In 035 * a typical case, the features should include {@link CollectionSize#ANY}.) These semantics are thus 036 * a little different from those of other Collection-related features such as {@link 037 * CollectionFeature} or {@link SetFeature}. 038 * 039 * <p>However, when {@link CollectionSize.Require} is used to annotate a test it behaves normally 040 * (i.e. it requires the collection instance under test to be a certain size for the test to run). 041 * Note that this means a test should not require more than one CollectionSize, since a particular 042 * collection instance can only be one size at once. 043 * 044 * @author George van den Driessche 045 */ 046@SuppressWarnings("rawtypes") // maybe avoidable if we rework the whole package? 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 = 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 = 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}