View Javadoc
1   /*
2    * #%L
3    * EUGene :: EUGene
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.eugene.java.extension;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.nuiton.eugene.models.object.ObjectModelClassifier;
28  import org.nuiton.eugene.models.object.ObjectModelElement;
29  
30  import java.util.Collections;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  /**
36   * Created: 17 déc. 2009
37   *
38   * @author Tony Chemit - chemit@codelutin.com
39   */
40  public class AnnotationsManagerExtension {
41  
42      private static final Log log = LogFactory.getLog(AnnotationsManagerExtension.class);
43  
44      /**
45       * Extension static used to identify AnnotationsManagerExtension in
46       * ObjectModel.
47       */
48      public static final String OBJECTMODEL_EXTENSION = "annotations";
49  
50      /**
51       * Map of AnotationsManager with key equals to the classifier qualified
52       * name associated to the AnotationsManager
53       */
54      protected Map<String, AnnotationsManager> managers;
55  
56  //    private static final String[] EMPTY_STRING_ARRAY = new String[]{};
57  
58      /**
59       * Get the registred annotations for the given {@code element} in the
60       * given {@code classifier}.
61       *
62       * <b>Note:</b> The method always returns a {@code none null} value, but
63       * an empty array when no annotation when no annotation found for the
64       * element.
65       *
66       * @param classifier the classifier where is the element
67       * @param element    the element on which searching annotations
68       * @return the list of annotations registred or an empty list if none.
69       */
70      public List<ObjectModelAnnotation> getAnnotations(ObjectModelClassifier classifier,
71                                                        ObjectModelElement element) {
72          AnnotationsManager annotationsManager = getManager(classifier);
73          List<ObjectModelAnnotation> result = null;
74          if (annotationsManager != null) {
75              result = annotationsManager.getAnnotations(element);
76          }
77          return result == null ? Collections.<ObjectModelAnnotation>emptyList() : result;
78      }
79  
80      /**
81       * Get the AnotationsManager associated to the classifier. If not exist,
82       * it will be created.
83       *
84       * @param classifier reference for the AnotationsManager
85       * @return the annotationsManager associated to the classifier (never null)
86       */
87      public AnnotationsManager getManager(ObjectModelClassifier classifier) {
88          Map<String, AnnotationsManager> managers = getManagers();
89          String fqn = classifier.getQualifiedName();
90          AnnotationsManager manager = managers.get(fqn);
91          if (manager == null) {
92              manager = new AnnotationsManager();
93              managers.put(fqn, manager);
94              if (log.isDebugEnabled()) {
95                  log.debug("Add new annotationsManager for : " + fqn);
96              }
97          }
98          return manager;
99      }
100 
101     protected Map<String, AnnotationsManager> getManagers() {
102         if (managers == null) {
103             managers = new HashMap<>();
104         }
105         return managers;
106     }
107 }