1 /*
2 ProgrammingLanguage.java
3 Creation date : 19/06/2010
4 Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 or GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 3 of the License,
10 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 Public License for more details.
16
17 You should have received copies of the GNU General Public License
18 and GNU Lesser General Public License along with this program;
19 if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 http://www.fsf.org/licensing/licenses/gpl.html
22 http://www.gnu.org/licenses/lgpl.html
23 */
24
25 package net.sourceforge.plantumldependency.cli.main.option.programminglanguage.argument;
26
27 import static java.util.Collections.unmodifiableMap;
28 import static java.util.logging.Level.FINE;
29 import static java.util.logging.Logger.getLogger;
30 import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.PROGRAMMING_LANGUAGE_NAME_NULL_ERROR;
31 import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.UNKNOWN_PROGRAMMING_LANGUAGE_ERROR;
32 import static net.sourceforge.plantumldependency.cli.constants.log.FineConstants.PROGRAMMING_LANGUAGE_FOUND_FINE;
33 import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNullOrEmpty;
34 import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.EQUAL;
35 import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
36
37 import java.io.Serializable;
38 import java.util.Collection;
39 import java.util.Map;
40 import java.util.Set;
41 import java.util.TreeMap;
42 import java.util.TreeSet;
43 import java.util.logging.Logger;
44 import java.util.regex.Pattern;
45
46 import net.sourceforge.plantumldependency.cli.exception.PlantUMLDependencyException;
47 import net.sourceforge.plantumldependency.cli.generic.GenericDependency;
48 import net.sourceforge.plantumldependency.cli.main.option.display.type.argument.DisplayType;
49 import net.sourceforge.plantumldependency.cli.main.option.programminglanguage.context.ProgrammingLanguageContext;
50
51 /**
52 * The abstract class which describes all supported programming language which can be reverse
53 * engineered.
54 *
55 * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
56 * @since 1.0.0
57 * @version 1.4.0
58 */
59 public abstract class ProgrammingLanguage implements Comparable < ProgrammingLanguage >, Serializable {
60
61 /** Serial version UID. */
62 private static final long serialVersionUID = -4593346488847506817L;
63
64 /** The class logger. */
65 private static final transient Logger LOGGER = getLogger(ProgrammingLanguage.class.getName());
66
67 /** The java programming language. */
68 public static final ProgrammingLanguage JAVA = new JavaProgrammingLanguage("java");
69
70 /** The c++ programming language. */
71 public static final ProgrammingLanguage CPP = new CppProgrammingLanguage("cpp");
72
73 /** The number of millisecond in one second. */
74 private static final Map < String, ProgrammingLanguage > PROGRAMMING_LANGUAGE_MAP = createProgrammingLanguageMap();
75
76 /**
77 * Creates the static {@link Map} containing all {@link ProgrammingLanguage}.
78 *
79 * @return the {@link Map} of all {@link ProgrammingLanguage} as values, with their associated
80 * names as keys.
81 * @since 1.0.0
82 */
83 private static Map < String, ProgrammingLanguage > createProgrammingLanguageMap() {
84 final Map < String, ProgrammingLanguage > programmingLanguageMap = new TreeMap < String, ProgrammingLanguage >();
85
86 programmingLanguageMap.put(CPP.getName(), CPP);
87 programmingLanguageMap.put(JAVA.getName(), JAVA);
88
89 return unmodifiableMap(programmingLanguageMap);
90 }
91
92 /**
93 * Gets the {@link Collection} of all {@link ProgrammingLanguage}.
94 *
95 * @return the {@link Collection} of all {@link ProgrammingLanguage} available.
96 * @since 1.0.0
97 */
98 public static Collection < ProgrammingLanguage > getProgrammingLanguageCollection() {
99 return PROGRAMMING_LANGUAGE_MAP.values();
100 }
101
102 /**
103 * Gets the {@link Set} of all programming languages names.
104 *
105 * @return the {@link Set} of all programming languages names available.
106 * @since 1.0.0
107 */
108 public static Set < String > getProgrammingLanguageNamesSet() {
109 final Set < String > names = new TreeSet < String >();
110
111 for (final ProgrammingLanguage programmingLanguage : getProgrammingLanguageCollection()) {
112 names.add(programmingLanguage.getName());
113 }
114
115 return names;
116 }
117
118 /**
119 * Gets the {@link ProgrammingLanguage} instance associated to the passed name. Throw an
120 * {@link IllegalArgumentException} if the programming language name isn't recognized.
121 *
122 * @param programmingLanguageName
123 * the programming language name to get the instance from, mustn't be
124 * <code>null</code> nor empty.
125 * @return the {@link ProgrammingLanguage} instance associated to the passed name if available.
126 * @since 1.0.0
127 */
128 public static ProgrammingLanguage valueOfIgnoringCase(final String programmingLanguageName) {
129 checkNullOrEmpty(programmingLanguageName, PROGRAMMING_LANGUAGE_NAME_NULL_ERROR);
130
131 ProgrammingLanguage programmingLanguage = null;
132
133 programmingLanguage = PROGRAMMING_LANGUAGE_MAP.get(programmingLanguageName.toLowerCase());
134 if (programmingLanguage != null) {
135 LOGGER.log(FINE, buildLogString(PROGRAMMING_LANGUAGE_FOUND_FINE, programmingLanguage));
136 } else {
137 throw new IllegalArgumentException(buildLogString(UNKNOWN_PROGRAMMING_LANGUAGE_ERROR,
138 programmingLanguageName));
139 }
140
141 return programmingLanguage;
142 }
143
144 /** The programming language name. */
145 private String name;
146
147 /**
148 * Default constructor.
149 *
150 * @param programmingLanguageName
151 * the programming language name to get the instance from, mustn't be
152 * <code>null</code> nor empty.
153 * @since 1.0.0
154 */
155 protected ProgrammingLanguage(final String programmingLanguageName) {
156 setName(programmingLanguageName);
157 }
158
159 /**
160 * {@inheritDoc}
161 *
162 * @since 1.0.0
163 */
164 @Override
165 public int compareTo(final ProgrammingLanguage o) {
166 if (this == o) {
167 return EQUAL.getResult();
168 }
169
170 return getName().compareTo(o.getName());
171 }
172
173 /**
174 * Create a new {@link ProgrammingLanguageContext} following the language.
175 *
176 * @param displayTypesOpts
177 * the {@link Set} of display types options which filter type to appear in the
178 * plantUML description, mustn't be <code>null</code>.
179 * @param displayPackageNamePattern
180 * the {@link Pattern} which filter package name to appear in the plantUML
181 * description, mustn't be <code>null</code>.
182 * @param displayNamePattern
183 * the {@link Pattern} which filter name to appear in the plantUML description,
184 * mustn't be <code>null</code>.
185 * @return a new empty {@link ProgrammingLanguageContext} instance.
186 * @since 1.0.0
187 */
188 public abstract ProgrammingLanguageContext createNewContext(final Set < DisplayType > displayTypesOpts,
189 final Pattern displayPackageNamePattern, final Pattern displayNamePattern);
190
191 /**
192 * {@inheritDoc}
193 *
194 * @since 1.0.0
195 */
196 @Override
197 public boolean equals(final Object obj) {
198 if (this == obj) {
199 return true;
200 }
201 if (obj == null) {
202 return false;
203 }
204 if (getClass() != obj.getClass()) {
205 return false;
206 }
207 final ProgrammingLanguage other = (ProgrammingLanguage) obj;
208 if (name == null) {
209 if (other.name != null) {
210 return false;
211 }
212 } else if (!name.equals(other.name)) {
213 return false;
214 }
215 return true;
216 }
217
218 /**
219 * Gets the value of <code>name</code>.
220 *
221 * @return the value of <code>name</code>.
222 * @since 1.0.0
223 */
224 public String getName() {
225 return name;
226 }
227
228 /**
229 * {@inheritDoc}
230 *
231 * @since 1.0.0
232 */
233 @Override
234 public int hashCode() {
235 final int prime = 31;
236 int result = 1;
237 result = prime * result + ((name == null) ? 0 : name.hashCode());
238 return result;
239 }
240
241 /**
242 * Reads the following source file content as a {@link String} to build the
243 * {@link GenericDependency} instance. This method also modify the
244 * <code>programmingLanguageContext</code> parameter by adding read objects and the parsed
245 * dependencies.
246 *
247 * @param sourceFileContent
248 * the source file content as a {@link String} to read, mustn't be <code>null</code>.
249 * Should contain the expected programming language.
250 * @param programmingLanguageContext
251 * the context instance containing all dependencies which have already been seen in
252 * previous treatment, and other information which can be shared when parsing several
253 * source files, mustn't be <code>null</code>.
254 * @return the {@link GenericDependency} instance if it has been found and correctly parsed,
255 * <code>null</code> otherwise. May also be <code>null</code> if the context display
256 * types options doesn't manage the dependency type.
257 * @throws PlantUMLDependencyException
258 * if any parsing exception occurs while reading the source file.
259 * @since 1.0.0
260 */
261 public abstract GenericDependency readDependencyFromFile(String sourceFileContent,
262 ProgrammingLanguageContext programmingLanguageContext) throws PlantUMLDependencyException;
263
264 /**
265 * Sets the value of <code>name</code>.
266 *
267 * @param value
268 * the <code>name</code> to set, can be <code>null</code>.
269 * @see #getName()
270 * @since 1.0.0
271 */
272 private void setName(final String value) {
273 checkNullOrEmpty(value, PROGRAMMING_LANGUAGE_NAME_NULL_ERROR);
274
275 name = value;
276 }
277
278 /**
279 * {@inheritDoc}
280 *
281 * @since 1.0.0
282 */
283 @Override
284 public String toString() {
285 return getClass().getSimpleName() + " [name=" + name + "]";
286 }
287 }