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.state.xml;
24  
25  import org.codehaus.plexus.component.annotations.Component;
26  import org.nuiton.eugene.ModelHelper;
27  import org.nuiton.eugene.models.Model;
28  import org.nuiton.eugene.models.state.StateModel;
29  import org.nuiton.eugene.models.state.StateModelStateChart;
30  
31  import java.util.ArrayList;
32  import java.util.Collection;
33  import java.util.HashMap;
34  import java.util.LinkedHashSet;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Set;
38  
39  /**
40   * Implementation of the {@link StateModel}.
41   *
42   * @author chatellier
43   */
44  @Component(role = Model.class, hint = "statemodel")
45  public class StateModelImpl implements StateModel {
46  
47      /**
48       * List of charts composing this model
49       */
50      protected List<StateModelStateChart> listStateCharts;
51  
52      /**
53       * Name of this model
54       */
55      protected String name;
56  
57      /**
58       * Version of this model
59       */
60      protected String version;
61  
62      /**
63       * Model tagged values
64       */
65      protected Map<String, String> modelTagValues;
66  
67      /**
68       * Used to add others specific object to the model
69       * The key defined must be unique to get the significative extension associated to
70       */
71      private Map<String, Object> extensions = new HashMap<>();
72  
73      /**
74       * Construteur
75       */
76      public StateModelImpl() {
77          listStateCharts = new ArrayList<>();
78          modelTagValues = new HashMap<>();
79      }
80  
81      @Override
82      public String getModelType() {
83          return ModelHelper.ModelType.STATE.getAlias();
84      }
85  
86      @Override
87      public String getName() {
88          return name;
89      }
90  
91      @Override
92      public Set<String> getStereotypes() {
93          Set<String> result = new LinkedHashSet<>();
94          for (Map.Entry<String, String> entry : modelTagValues.entrySet()) {
95              if ("true".equals(entry.getValue())) {
96                  result.add(entry.getKey());
97              }
98          }
99          return result;
100     }
101 
102     @Override
103     public boolean hasStereotype(String stereotypeName) {
104         return modelTagValues.containsKey(stereotypeName);
105     }
106 
107     @Override
108     public void addStereotype(String stereotype) {
109         modelTagValues.put(stereotype, "true");
110     }
111 
112     @Override
113     public void removeStereotype(String stereotype) {
114         modelTagValues.remove(stereotype);
115     }
116 
117     /**
118      * Set model name.
119      *
120      * @param name model name
121      */
122     public void setName(String name) {
123         this.name = name;
124     }
125 
126     /**
127      * Add chart.
128      *
129      * @param chart chart
130      */
131     public void addStateChart(StateModelStateChart chart) {
132 
133         // appele apres construction du StateModelStateChartImpl
134         // corrige les liens entre les nom d'etat, et les instances d'etat
135         ((StateModelStateChartImpl) chart)
136                 .correctTransitionNameToInstance(null);
137 
138         listStateCharts.add(chart);
139     }
140 
141     @Override
142     public List<StateModelStateChart> getStateCharts() {
143         return listStateCharts;
144     }
145 
146     /**
147      * Add a list of stateCharts into current model
148      *
149      * @param charts list
150      */
151     public void addAllStateCharts(Collection<StateModelStateChart> charts) {
152         listStateCharts.addAll(charts);
153     }
154 
155     @Override
156     public Map<String, String> getTagValues() {
157         return modelTagValues;
158     }
159 
160     @Override
161     public void addTagValue(String key, String value) {
162         modelTagValues.put(key, value);
163     }
164 
165     @Override
166     public String getTagValue(String key) {
167         return key == null ? null : modelTagValues.get(key);
168     }
169 
170     @Override
171     public boolean hasTagValue(String tagValue) {
172         return modelTagValues.containsKey(tagValue);
173     }
174 
175     @Override
176     public void removeTagValue(String tagvalue) {
177         modelTagValues.remove(tagvalue);
178     }
179 
180     /**
181      * Set model version.
182      *
183      * @param version model version
184      */
185     public void setVersion(String version) {
186         this.version = version;
187     }
188 
189     @Override
190     public String getVersion() {
191         return version;
192     }
193 
194     /**
195      * Get the extension associated to the reference (unique). Create it if not exist.
196      *
197      * @param <O>            object type returned
198      * @param reference      unique corresponding to the extension to get
199      * @param extensionClass class of the extension
200      * @return the object value for the extension
201      * @throws ClassCastException when extensionClass is not valid
202      * @throws RuntimeException   when instantiation problem to create new extension
203      */
204     @Override
205     @SuppressWarnings("unchecked")
206     public <O> O getExtension(String reference, Class<O> extensionClass) throws RuntimeException {
207         if (reference == null) {
208             throw new NullPointerException("reference parameter can not be null in method ObjectModelImpl#getExtension");
209         }
210         if (extensionClass == null) {
211             throw new NullPointerException("extensionClass parameter can not be null in method ObjectModelImpl#getExtension.");
212         }
213         Object object = extensions.get(reference);
214         O result;
215         if (object != null && !extensionClass.isAssignableFrom(object.getClass())) {
216             throw new ClassCastException("Invalid cast for " + extensionClass.getName());
217         }
218         if (object == null) {
219             try {
220                 result = extensionClass.newInstance();
221             } catch (Exception eee) { // IllegalAccessException and InstantiationException
222                 throw new RuntimeException("Unable to create new extension '" + extensionClass.getName() +
223                                                    "' for '" + reference + "'", eee);
224             }
225             extensions.put(reference, result);
226         } else {
227             result = (O) object;
228         }
229         return result;
230     }
231 
232 }