MapFeature.java revision dbd967a6e5c96cc1a97c5521f88dc1564ba2f81b
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
17package com.google.common.collect.testing.features;
18
19import com.google.common.collect.testing.Helpers;
20
21import java.lang.annotation.Inherited;
22import java.lang.annotation.Retention;
23import java.lang.annotation.RetentionPolicy;
24import java.util.Map;
25import java.util.Set;
26
27/**
28 * Optional features of classes derived from {@code Map}.
29 *
30 * <p>This class is GWT compatible.
31 *
32 * @author George van den Driessche
33 */
34// Enum values use constructors with generic varargs.
35@SuppressWarnings("unchecked")
36public enum MapFeature implements Feature<Map> {
37  /**
38   * The map does not throw {@code NullPointerException} on calls such as
39   * {@code containsKey(null)}, {@code get(null)}, or {@code remove(null)}.
40   */
41  ALLOWS_NULL_QUERIES,
42  ALLOWS_NULL_KEYS (ALLOWS_NULL_QUERIES),
43  ALLOWS_NULL_VALUES,
44  RESTRICTS_KEYS,
45  RESTRICTS_VALUES,
46  SUPPORTS_PUT,
47  SUPPORTS_PUT_ALL,
48  SUPPORTS_REMOVE,
49  SUPPORTS_CLEAR,
50  /**
51   * Indicates that the constructor or factory method of a map, usually an
52   * immutable map, throws an {@link IllegalArgumentException} when presented
53   * with duplicate keys instead of discarding all but one.
54   */
55  REJECTS_DUPLICATES_AT_CREATION,
56
57  GENERAL_PURPOSE(
58      SUPPORTS_PUT,
59      SUPPORTS_PUT_ALL,
60      SUPPORTS_REMOVE,
61      SUPPORTS_CLEAR
62  ),
63
64  /** Features supported by maps where only removal is allowed. */
65  REMOVE_OPERATIONS(
66      SUPPORTS_REMOVE,
67      SUPPORTS_CLEAR
68    );
69
70  private final Set<Feature<? super Map>> implied;
71
72  MapFeature(Feature<? super Map> ... implied) {
73    this.implied = Helpers.copyToSet(implied);
74  }
75
76  @Override
77  public Set<Feature<? super Map>> getImpliedFeatures() {
78    return implied;
79  }
80
81  @Retention(RetentionPolicy.RUNTIME)
82  @Inherited
83  @TesterAnnotation
84  public @interface Require {
85    public abstract MapFeature[] value() default {};
86    public abstract MapFeature[] absent() default {};
87  }
88}
89