1 /**
2 * Redistribution and use of this software and associated documentation ("Software"), with or
3 * without modification, are permitted provided that the following conditions are met:
4 *
5 * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6 * must also contain a copy of this document.
7 *
8 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9 * conditions and the following disclaimer in the documentation and/or other materials provided with
10 * the distribution.
11 *
12 * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13 * without prior written permission of Intalio, Inc. For written permission, please contact
14 * info@exolab.org.
15 *
16 * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17 * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18 * Intalio, Inc.
19 *
20 * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21 *
22 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Copyright 2001-2002 (C) Intalio, Inc. All Rights Reserved.
32 *
33 * $Id: JAnnotatedElementHelper
34 */
35 package org.exolab.javasource;
36
37 import java.util.Iterator;
38 import java.util.LinkedHashMap;
39
40 /**
41 * Implements JAnnotatedElement interface on behalf of other classes in this package that implement
42 * this interface.
43 *
44 * @author <a href="mailto:andrew DOT fawcett AT coda DOTcom">Andrew Fawcett</a>
45 * @version $Revision$ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
46 */
47 public class JAnnotatedElementHelper implements JAnnotatedElement {
48
49 /**
50 * Stores annotations associated with the source element containing this helper.
51 */
52 private LinkedHashMap<String, JAnnotation> _annotations =
53 new LinkedHashMap<String, JAnnotation>();
54
55 /**
56 * Creates a JAnnodatedElementHelper.
57 */
58 public JAnnotatedElementHelper() {
59 super();
60 }
61
62 /**
63 * {@inheritDoc}
64 */
65 public final JAnnotation getAnnotation(final JAnnotationType annotationType) {
66 if (_annotations == null) {
67 return null;
68 }
69 return _annotations.get(annotationType.getName());
70 }
71
72 /**
73 * {@inheritDoc}
74 */
75 public final JAnnotation[] getAnnotations() {
76 return _annotations.values().toArray(new JAnnotation[_annotations.size()]);
77 }
78
79 /**
80 * {@inheritDoc}
81 */
82 public final boolean isAnnotationPresent(final JAnnotationType annotationType) {
83 return _annotations.containsKey(annotationType.getName());
84 }
85
86 /**
87 * {@inheritDoc}
88 */
89 public final void addAnnotation(final JAnnotation annotation) {
90 if (isAnnotationPresent(annotation.getAnnotationType())) {
91 throw new IllegalArgumentException(
92 "Annotation for '" + annotation.getAnnotationType().getName() + "' already added.");
93 }
94 String annotationType = annotation.getAnnotationType().getName();
95 _annotations.put(annotationType, annotation);
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 public final JAnnotation removeAnnotation(final JAnnotationType annotationType) {
102 if (!isAnnotationPresent(annotationType)) {
103 throw new IllegalArgumentException(
104 "Annotation for '" + annotationType.getName() + "' not present.");
105 }
106 return _annotations.remove(annotationType.getName());
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 public final boolean hasAnnotations() {
113 return !_annotations.isEmpty();
114 }
115
116 /**
117 * Outputs the list of annotations maintained by this object.
118 *
119 * @param jsw the JSourceWriter to print the annotations to
120 * @return true if at least one annotation was printed, false otherwise.
121 */
122 public final boolean printAnnotations(final JSourceWriter jsw) {
123 boolean printed = false;
124 Iterator<JAnnotation> annotations = _annotations.values().iterator();
125 while (annotations.hasNext()) {
126 JAnnotation annotation = annotations.next();
127 annotation.print(jsw);
128 jsw.writeln();
129 printed = true;
130 }
131 return printed;
132 }
133
134 }