limpbizkit | e39d8d8 | 2008-03-26 07:19:25 +0000 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | package com.google.inject.internal; |
| 18 | |
| 19 | import com.google.inject.BindingAnnotation; |
| 20 | |
| 21 | import java.lang.annotation.Annotation; |
| 22 | import java.lang.annotation.Retention; |
| 23 | import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 24 | import java.util.concurrent.atomic.AtomicInteger; |
| 25 | |
limpbizkit | c878481 | 2008-03-26 07:20:04 +0000 | [diff] [blame] | 26 | /** |
| 27 | * @author jessewilson@google.com (Jesse Wilson) |
| 28 | */ |
limpbizkit | e39d8d8 | 2008-03-26 07:19:25 +0000 | [diff] [blame] | 29 | public 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() { |
limpbizkit | 56400ca | 2008-05-23 19:20:08 +0000 | [diff] [blame] | 60 | return (127 * "value".hashCode()) ^ value; |
limpbizkit | e39d8d8 | 2008-03-26 07:19:25 +0000 | [diff] [blame] | 61 | } |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | @Retention(RUNTIME) @BindingAnnotation |
| 66 | @interface Internal { |
| 67 | int value(); |
| 68 | } |
| 69 | } |