IntDef.java revision 1c086e1e7849c0b3fe31d11ea83d42bd78abcd6d
1/*
2 * Copyright (C) 2014 The Android Open Source Project
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 */
16package android.support.annotation;
17
18import java.lang.annotation.Retention;
19import java.lang.annotation.RetentionPolicy;
20import java.lang.annotation.Target;
21
22import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
23import static java.lang.annotation.ElementType.FIELD;
24import static java.lang.annotation.ElementType.METHOD;
25import static java.lang.annotation.ElementType.PARAMETER;
26import static java.lang.annotation.RetentionPolicy.CLASS;
27import static java.lang.annotation.RetentionPolicy.SOURCE;
28
29/**
30 * Denotes that the annotated element of integer type, represents
31 * a logical type and that its value should be one of the explicitly
32 * named constants. If the IntDef#flag() attribute is set to true,
33 * multiple constants can be combined.
34 * <p>
35 * Example:
36 * <pre>{@code
37 *  &#64;Retention(CLASS)
38 *  &#64;IntDef(&#123;NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS&#125;)
39 *  public &#64;interface NavigationMode &#123;&#125;
40 *  public static final int NAVIGATION_MODE_STANDARD = 0;
41 *  public static final int NAVIGATION_MODE_LIST = 1;
42 *  public static final int NAVIGATION_MODE_TABS = 2;
43 *  ...
44 *  public abstract void setNavigationMode(&#64;NavigationMode int mode);
45 *  &#64;NavigationMode
46 *  public abstract int getNavigationMode();
47 * }</pre>
48 * For a flag, set the flag attribute:
49 * <pre>{@code
50 *  &#64;IntDef(
51 *      flag = true
52 *      value = &#123;NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS&#125;)
53 * }</pre>
54 */
55@Retention(CLASS)
56@Target({ANNOTATION_TYPE})
57public @interface IntDef {
58    /** Defines the allowed constants for this element */
59    long[] value() default {};
60
61    /** Defines whether the constants can be used as a flag, or just as an enum (the default) */
62    boolean flag() default false;
63}
64