blob: 4fff62b24e7af83156dd15fabc05776cfde76be6 [file] [log] [blame]
limpbizkite39d8d82008-03-26 07:19:25 +00001/**
2 * Copyright (C) 2008 Google Inc.
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.inject.internal;
18
19import com.google.inject.BindingAnnotation;
20
21import java.lang.annotation.Annotation;
22import java.lang.annotation.Retention;
23import static java.lang.annotation.RetentionPolicy.RUNTIME;
24import java.util.concurrent.atomic.AtomicInteger;
25
limpbizkitc8784812008-03-26 07:20:04 +000026/**
27 * @author jessewilson@google.com (Jesse Wilson)
28 */
limpbizkite39d8d82008-03-26 07:19:25 +000029public class UniqueAnnotations {
30 private static final AtomicInteger nextUniqueValue = new AtomicInteger(1);
31
32 /**
33 * Returns an annotation instance that is not equal to any other annotation
34 * instances, for use in creating distinct {@link com.google.inject.Key}s.
35 */
36 public static Annotation create() {
37 return create(nextUniqueValue.getAndIncrement());
38 }
39
40 static Annotation create(final int value) {
41 return new Internal() {
42 public int value() {
43 return value;
44 }
45
46 public Class<? extends Annotation> annotationType() {
47 return Internal.class;
48 }
49
50 @Override public String toString() {
51 return "@" + Internal.class.getName() + "(value=" + value + ")";
52 }
53
54 @Override public boolean equals(Object o) {
55 return o instanceof Internal
56 && ((Internal) o).value() == value();
57 }
58
59 @Override public int hashCode() {
limpbizkit56400ca2008-05-23 19:20:08 +000060 return (127 * "value".hashCode()) ^ value;
limpbizkite39d8d82008-03-26 07:19:25 +000061 }
62 };
63 }
64
65 @Retention(RUNTIME) @BindingAnnotation
66 @interface Internal {
67 int value();
68 }
69}