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.models.object.xml;
24  
25  import com.google.common.collect.ImmutableSet;
26  import com.google.common.collect.Sets;
27  import org.nuiton.eugene.models.object.ObjectModelJavaModifier;
28  import org.nuiton.eugene.models.object.ObjectModelModifier;
29  import org.nuiton.eugene.models.object.ObjectModelOperation;
30  import org.nuiton.eugene.models.object.ObjectModelParameter;
31  
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.HashSet;
35  import java.util.Iterator;
36  import java.util.List;
37  import java.util.Set;
38  
39  /**
40   * ObjectModelOperationImpl.java
41   *
42   * Created: 14 janv. 2004
43   *
44   * @author Cédric Pineau - pineau@codelutin.com Copyright Code Lutin
45   */
46  public class ObjectModelOperationImpl extends ObjectModelElementImpl implements ObjectModelOperation {
47  
48      public static final ObjectModelJavaModifier DEFAULT_VISIBILITY = ObjectModelJavaModifier.PUBLIC;
49  
50      protected ObjectModelParameter returnParameter;
51  
52      protected String transactionLevel = "supports";
53  
54      protected List<ObjectModelParameter> parameters = new ArrayList<>();
55  
56      protected Set<String> exceptions = new HashSet<>();
57  
58      protected String bodyCode = "";
59  
60      private static Set<ObjectModelModifier> authorizedModifiers;
61  
62      public ObjectModelOperationImpl() {
63      }
64  
65      @Override
66      protected Set<ObjectModelModifier> getAuthorizedModifiers() {
67          if (authorizedModifiers == null) {
68              // http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.3
69              // Annotation public protected private abstract
70              // static final synchronized native strictfp
71              Set<ObjectModelModifier> modifiers = Sets.newHashSet(
72                      (ObjectModelModifier) ObjectModelJavaModifier.ABSTRACT, // Force cast because of generics limitation
73                      ObjectModelJavaModifier.STATIC,
74                      ObjectModelJavaModifier.FINAL,
75                      ObjectModelJavaModifier.SYNCHRONIZED,
76                      ObjectModelJavaModifier.NATIVE,
77                      ObjectModelJavaModifier.STRICTFP);
78              modifiers.addAll(ObjectModelJavaModifier.visibilityModifiers);
79              authorizedModifiers = ImmutableSet.copyOf(modifiers);
80          }
81          return authorizedModifiers;
82      }
83  
84      public String toString() {
85          return getName() + "(" + parameters + ")" + "<<" + getStereotypes()
86                 + ">> throws " + exceptions + " tagvalue: " + getTagValues();
87      }
88  
89      /**
90       * 2 operations are equal if the have same name and same argument type.
91       */
92      public boolean equals(Object o) {
93          if (o instanceof ObjectModelOperation) {
94              ObjectModelOperation op = (ObjectModelOperation) o;
95              if (getName().equals(op.getName())
96                  && getParameters().size() == op.getParameters().size()) {
97                  for (Iterator<?> i = getParameters().iterator(), opi = op
98                          .getParameters().iterator(); i.hasNext()
99                                                       && opi.hasNext(); ) {
100                     ObjectModelParameter p = (ObjectModelParameter) i.next();
101                     ObjectModelParameter pop = (ObjectModelParameter) opi
102                             .next();
103                     if (!p.getType().equals(pop.getType())) {
104                         return false;
105                     }
106                 }
107                 return true;
108             }
109         }
110         return false;
111     }
112 
113     public void addParameter(ObjectModelParameterImpl parameter) {
114         // if (parameter == null)
115         // return new ObjectModelParameterImpl(objectModelImpl, this);
116         parameter.postInit();
117         parameter.setDeclaringElement(this);
118         parameters.add(parameter);
119         // return parameter;
120     }
121 
122     public void setVisibility(String visibility) {
123         ObjectModelModifier modifier = ObjectModelJavaModifier.fromVisibility(visibility);
124         removeModifiers(ObjectModelJavaModifier.visibilityModifiers);
125         if (modifier == null) {
126             modifier = DEFAULT_VISIBILITY; // default visibility
127         }
128         addModifier(modifier);
129     }
130 
131     public void setAbstract(boolean abstractz) {
132         addOrRemoveModifier(ObjectModelJavaModifier.ABSTRACT, abstractz);
133     }
134 
135     public void setReturnParameter(ObjectModelParameterImpl returnParameter) {
136         // /if (returnParameter == null)
137         // return new ObjectModelParameterImpl(objectModelImpl, this);
138         returnParameter.postInit();
139         returnParameter.setDeclaringElement(this);
140         this.returnParameter = returnParameter;
141     }
142 
143     public void setBodyCode(String bodyCode) {
144         this.bodyCode = bodyCode;
145     }
146 
147     /**
148      * Add some code to current body
149      *
150      * @param bodyCode code to add
151      */
152     public void addBodyCode(String bodyCode) {
153         this.bodyCode += bodyCode;
154     }
155 
156     @Override
157     public ObjectModelParameter getReturnParameter() {
158         return returnParameter;
159     }
160 
161     @Override
162     public String getReturnType() {
163         if (returnParameter != null) {
164             return returnParameter.getType();
165         }
166         return "void";
167     }
168 
169     @Override
170     public String getVisibility() {
171         String visibility = DEFAULT_VISIBILITY.toString(); // default
172         if (modifiers.contains(ObjectModelJavaModifier.PRIVATE)) {
173             visibility = ObjectModelJavaModifier.PRIVATE.toString();
174         } else if (modifiers.contains(ObjectModelJavaModifier.PROTECTED)) {
175             visibility = ObjectModelJavaModifier.PROTECTED.toString();
176         }
177         if (modifiers.contains(ObjectModelJavaModifier.PACKAGE)) {
178             visibility = ObjectModelJavaModifier.PACKAGE.toString();
179         }
180         return visibility;
181     }
182 
183     /**
184      * Returns whether this operation is abstract or not.
185      *
186      * @return a boolean indicating whether this operation is abstract or not.
187      */
188     @Override
189     public boolean isAbstract() {
190         return modifiers.contains(ObjectModelJavaModifier.ABSTRACT);
191     }
192 
193     @Override
194     public Collection<ObjectModelParameter> getParameters() {
195         return parameters;
196     }
197 
198     /**
199      * Add new raised exception.
200      *
201      * @param raisedParameter exception to add
202      */
203     public void addExceptionParameter(ObjectModelParameterImpl raisedParameter) {
204         raisedParameter.postInit();
205         raisedParameter.setDeclaringElement(this);
206 
207         exceptions.add(raisedParameter.getType());
208     }
209 
210     @Override
211     public Set<String> getExceptions() {
212         return exceptions;
213     }
214 
215     @Override
216     public String getBodyCode() {
217         return bodyCode;
218     }
219 
220 }