1313 */
1414public final class AnnotationMap implements Annotations
1515{
16- protected HashMap <Class <?>,Annotation > _annotations ;
16+ protected Map <Class <?>,Annotation > _annotations ;
1717
18+ /*
19+ /**********************************************************
20+ /* Construction
21+ /**********************************************************
22+ */
23+
1824 public AnnotationMap () { }
1925
26+ public AnnotationMap (Map <Class <?>,Annotation > a ) {
27+ _annotations = a ;
28+ }
29+
2030 public static AnnotationMap of (Class <?> type , Annotation value ) {
21- HashMap <Class <?>,Annotation > ann = new HashMap <>(4 );
31+ Map <Class <?>,Annotation > ann = new HashMap <>(4 );
2232 ann .put (type , value );
2333 return new AnnotationMap (ann );
2434 }
2535
26- AnnotationMap (HashMap <Class <?>,Annotation > a ) {
27- _annotations = a ;
36+ public static AnnotationMap of (Collection <Annotation > rawAnnotations ) {
37+ Map <Class <?>,Annotation > map = new HashMap <>(rawAnnotations .size ());
38+ for (Annotation raw : rawAnnotations ) {
39+ map .put (raw .annotationType (), raw );
40+ }
41+ return new AnnotationMap (map );
2842 }
2943
3044 /*
@@ -55,8 +69,6 @@ public boolean has(Class<?> cls)
5569 /**
5670 * Helper method that can be used for a "bulk" check to see if at least
5771 * one of given annotation types is included within this map.
58- *
59- * @since 2.7
6072 */
6173 @ Override
6274 public boolean hasOneOf (Class <? extends Annotation >[] annoClasses ) {
@@ -75,10 +87,7 @@ public boolean hasOneOf(Class<? extends Annotation>[] annoClasses) {
7587 /* Other API
7688 /**********************************************************
7789 */
78-
79- /**
80- * @since 2.3
81- */
90+
8291 public Iterable <Annotation > annotations () {
8392 if (_annotations == null || _annotations .size () == 0 ) {
8493 return Collections .emptyList ();
@@ -111,48 +120,11 @@ public int size() {
111120 return (_annotations == null ) ? 0 : _annotations .size ();
112121 }
113122
114- /**
115- * Method called to add specified annotation in the Map, but
116- * only if it didn't yet exist.
117- */
118- public boolean addIfNotPresent (Annotation ann )
119- {
120- if (_annotations == null || !_annotations .containsKey (ann .annotationType ())) {
121- _add (ann );
122- return true ;
123- }
124- return false ;
125- }
126-
127- /**
128- * Method called to add specified annotation in the Map.
129- *
130- * @return True if the addition changed the contents, that is, this map did not
131- * already have specified annotation
132- */
133- public boolean add (Annotation ann ) {
134- return _add (ann );
135- }
136-
137123 @ Override
138124 public String toString () {
139125 if (_annotations == null ) {
140126 return "[null]" ;
141127 }
142128 return _annotations .toString ();
143129 }
144-
145- /*
146- /**********************************************************
147- /* Helper methods
148- /**********************************************************
149- */
150-
151- protected final boolean _add (Annotation ann ) {
152- if (_annotations == null ) {
153- _annotations = new HashMap <Class <?>,Annotation >();
154- }
155- Annotation previous = _annotations .put (ann .annotationType (), ann );
156- return (previous == null ) || !previous .equals (ann );
157- }
158130}
0 commit comments