1 /*
2 * Grapht, an open source dependency injector.
3 * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4 * Copyright 2010-2014 Regents of the University of Minnesota
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 package org.grouplens.grapht.reflect.internal;
21
22 import org.grouplens.grapht.annotation.Attribute;
23 import org.grouplens.grapht.reflect.Qualifiers;
24
25 import java.lang.annotation.Annotation;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 /**
32 * Helper class for managing annotations on an injection point.
33 *
34 * @author <a href="http://grouplens.org">GroupLens Research</a>
35 */
36 class AnnotationHelper {
37 private final Map<Class<? extends Annotation>, Annotation> attrs;
38 private final Annotation qualifier;
39
40 public AnnotationHelper(Annotation... annots) {
41 attrs = new HashMap<Class<? extends Annotation>, Annotation>();
42 Annotation foundQualifier = null;
43 for (Annotation a: annots) {
44 if (a.annotationType().getAnnotation(Attribute.class) != null) {
45 // a is an attribute
46 attrs.put(a.annotationType(), a);
47 }
48 if (foundQualifier == null && Qualifiers.isQualifier(a.annotationType())) {
49 // a is a qualifier
50 foundQualifier = a;
51 }
52 }
53
54 qualifier = foundQualifier;
55 }
56
57 public Annotation getQualifier() {
58 return qualifier;
59 }
60
61 public <A extends Annotation> A getAttribute(Class<A> atype) {
62 return atype.cast(attrs.get(atype));
63 }
64
65 public Collection<Annotation> getAttributes() {
66 return Collections.unmodifiableCollection(attrs.values());
67 }
68 }