1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package net.sourceforge.plantumldependency.cli.main.option.programminglanguage.context;
26
27 import static java.util.logging.Level.SEVERE;
28 import static java.util.logging.Logger.getLogger;
29 import static net.sourceforge.plantumldependency.cli.constants.PlantUMLDependencyConstants.DEFAULT_DISPLAY_NAME_OPTIONS_PATTERN;
30 import static net.sourceforge.plantumldependency.cli.constants.PlantUMLDependencyConstants.DEFAULT_DISPLAY_PACKAGE_NAME_OPTIONS_PATTERN;
31 import static net.sourceforge.plantumldependency.cli.constants.PlantUMLDependencyConstants.JAVA_LANG_PACKAGE;
32 import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.DEPENDENCY_NAME_NULL_ERROR;
33 import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.DEPENDENCY_NULL_ERROR;
34 import static net.sourceforge.plantumldependency.cli.main.option.display.type.argument.DisplayType.DISPLAY_TYPES_OPTIONS;
35 import static net.sourceforge.plantumldependency.common.constants.log.ErrorConstants.UNEXPECTED_ERROR;
36 import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
37 import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.AFTER;
38 import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.BEFORE;
39 import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.EQUAL;
40
41 import java.util.Collection;
42 import java.util.Map;
43 import java.util.Set;
44 import java.util.TreeMap;
45 import java.util.TreeSet;
46 import java.util.logging.Logger;
47 import java.util.regex.Pattern;
48
49 import net.sourceforge.plantumldependency.cli.generic.GenericDependency;
50 import net.sourceforge.plantumldependency.cli.main.option.display.type.argument.DisplayType;
51 import net.sourceforge.plantumldependency.cli.plantumldiagram.classes.PlantUMLClassesDiagram;
52 import net.sourceforge.plantumldependency.cli.plantumldiagram.classes.element.PlantUMLClassesDiagramElement;
53 import net.sourceforge.plantumldependency.cli.plantumldiagram.classes.impl.PlantUMLClassesDiagramImpl;
54 import net.sourceforge.plantumldependency.cli.plantumldiagram.classes.relation.PlantUMLClassesDiagramRelation;
55
56
57
58
59
60
61
62
63
64 public abstract class AbstractProgrammingLanguageContext implements ProgrammingLanguageContext {
65
66
67 private static final transient Logger LOGGER = getLogger(AbstractProgrammingLanguageContext.class.getName());
68
69
70 private static final long serialVersionUID = -2181655116426569842L;
71
72
73
74
75
76 private Set < DisplayType > displayTypesOptions;
77
78
79
80
81
82 private Pattern displayPackageNamePattern;
83
84
85
86
87
88 private Pattern displayNamePattern;
89
90
91
92
93
94
95 private Map < String, GenericDependency > parsedAndSeenDependenciesMap;
96
97
98
99
100
101
102
103 private Map < String, GenericDependency > potentialJavaLangSeenDependenciesMap;
104
105
106
107
108
109
110 private Map < String, GenericDependency > parsedDependenciesMap;
111
112
113
114
115
116
117 protected AbstractProgrammingLanguageContext() {
118 this(new TreeSet < GenericDependency >(), DISPLAY_TYPES_OPTIONS, DEFAULT_DISPLAY_PACKAGE_NAME_OPTIONS_PATTERN,
119 DEFAULT_DISPLAY_NAME_OPTIONS_PATTERN);
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 protected AbstractProgrammingLanguageContext(final Set < DisplayType > displayTypesOpts,
137 final Pattern displayPackageNamePatternOpt, final Pattern displayNamePatternOpt) {
138 this(new TreeSet < GenericDependency >(), displayTypesOpts, displayPackageNamePatternOpt, displayNamePatternOpt);
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 protected AbstractProgrammingLanguageContext(final Set < GenericDependency > parsedAndSeenDependencies,
159 final Set < DisplayType > displayTypesOpts, final Pattern displayPackageNamePatternOpt,
160 final Pattern displayNamePatternOpt) {
161 this(parsedAndSeenDependencies, parsedAndSeenDependencies, new TreeSet < GenericDependency >(),
162 displayTypesOpts, displayPackageNamePatternOpt, displayNamePatternOpt);
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 protected AbstractProgrammingLanguageContext(final Set < GenericDependency > parsedAndSeenDependencies,
189 final Set < GenericDependency > parsedDependencies,
190 final Set < GenericDependency > potentialJavaLangSeenDependencies,
191 final Set < DisplayType > displayTypesOpts, final Pattern displayPackageNamePatternOpt,
192 final Pattern displayNamePatternOpt) {
193
194 final Map < String, GenericDependency > firstDependenciesMap = new TreeMap < String, GenericDependency >();
195 for (final GenericDependency genericDependency : parsedAndSeenDependencies) {
196 firstDependenciesMap.put(genericDependency.getFullName(), genericDependency);
197 }
198 setParsedAndSeenDependenciesMap(firstDependenciesMap);
199
200
201
202 final Map < String, GenericDependency > secondDependenciesMap = new TreeMap < String, GenericDependency >();
203 for (final GenericDependency genericDependency : parsedDependencies) {
204 secondDependenciesMap.put(genericDependency.getFullName(), genericDependency);
205 }
206 setParsedDependenciesMap(secondDependenciesMap);
207
208
209
210 final Map < String, GenericDependency > thirdDependenciesMap = new TreeMap < String, GenericDependency >();
211 for (final GenericDependency genericDependency : potentialJavaLangSeenDependencies) {
212 thirdDependenciesMap.put(genericDependency.getFullName(), genericDependency);
213 }
214 setPotentialJavaLangSeenDependenciesMap(thirdDependenciesMap);
215
216 setDisplayTypesOptions(new TreeSet < DisplayType >(displayTypesOpts));
217 setDisplayPackageNamePattern(displayPackageNamePatternOpt);
218 setDisplayNamePattern(displayNamePatternOpt);
219 }
220
221
222
223
224
225
226 @Override
227 public void addParsedAndSeenDependencies(final GenericDependency dependency) {
228 checkNull(dependency, DEPENDENCY_NULL_ERROR);
229 getParsedDependenciesMap().put(dependency.getFullName(), dependency);
230 getParsedAndSeenDependenciesMap().put(dependency.getFullName(), dependency);
231 }
232
233
234
235
236
237
238 @Override
239 public void addPotentialJavaLangSeenDependencies(final GenericDependency dependency) {
240 checkNull(dependency, DEPENDENCY_NULL_ERROR);
241 getParsedAndSeenDependenciesMap().put(dependency.getFullName(), dependency);
242 getPotentialJavaLangSeenDependenciesMap().put(dependency.getFullName(), dependency);
243 }
244
245
246
247
248
249
250 @Override
251 public void addSeenDependencies(final GenericDependency dependency) {
252 checkNull(dependency, DEPENDENCY_NULL_ERROR);
253 getParsedAndSeenDependenciesMap().put(dependency.getFullName(), dependency);
254 }
255
256
257
258
259
260
261 @Override
262 public int compareTo(final ProgrammingLanguageContext o) {
263 final int thisSize = getParsedDependencies().size();
264 final int anotherSize = o.getParsedDependencies().size();
265 return thisSize < anotherSize ? BEFORE.getResult() : thisSize == anotherSize ? EQUAL.getResult() : AFTER
266 .getResult();
267 }
268
269
270
271
272
273
274 @Override
275 public ProgrammingLanguageContext deepClone() {
276 AbstractProgrammingLanguageContext a = null;
277
278 try {
279 a = (AbstractProgrammingLanguageContext) super.clone();
280 a.parsedAndSeenDependenciesMap = new TreeMap < String, GenericDependency >();
281 for (final GenericDependency genericDependency : getParsedAndSeenDependencies()) {
282 a.parsedAndSeenDependenciesMap.put(genericDependency.getFullName(), genericDependency.deepClone());
283 }
284 a.potentialJavaLangSeenDependenciesMap = new TreeMap < String, GenericDependency >(
285 a.potentialJavaLangSeenDependenciesMap);
286 a.parsedDependenciesMap = new TreeMap < String, GenericDependency >(a.parsedDependenciesMap);
287 a.displayTypesOptions = new TreeSet < DisplayType >(getDisplayTypesOptions());
288 a.displayPackageNamePattern = displayPackageNamePattern;
289 a.displayNamePattern = displayNamePattern;
290 } catch (final CloneNotSupportedException cnse) {
291 LOGGER.log(SEVERE, UNEXPECTED_ERROR, cnse);
292 }
293
294 return a;
295 }
296
297
298
299
300
301
302 @Override
303 public boolean equals(final Object obj) {
304 if (this == obj) {
305 return true;
306 }
307 if (obj == null) {
308 return false;
309 }
310 if (getClass() != obj.getClass()) {
311 return false;
312 }
313 final AbstractProgrammingLanguageContext other = (AbstractProgrammingLanguageContext) obj;
314 if (displayNamePattern == null) {
315 if (other.displayNamePattern != null) {
316 return false;
317 }
318 } else if (!displayNamePattern.equals(other.displayNamePattern)) {
319 return false;
320 }
321 if (displayPackageNamePattern == null) {
322 if (other.displayPackageNamePattern != null) {
323 return false;
324 }
325 } else if (!displayPackageNamePattern.equals(other.displayPackageNamePattern)) {
326 return false;
327 }
328 if (displayTypesOptions == null) {
329 if (other.displayTypesOptions != null) {
330 return false;
331 }
332 } else if (!displayTypesOptions.equals(other.displayTypesOptions)) {
333 return false;
334 }
335 if (parsedAndSeenDependenciesMap == null) {
336 if (other.parsedAndSeenDependenciesMap != null) {
337 return false;
338 }
339 } else if (!parsedAndSeenDependenciesMap.equals(other.parsedAndSeenDependenciesMap)) {
340 return false;
341 }
342 if (parsedDependenciesMap == null) {
343 if (other.parsedDependenciesMap != null) {
344 return false;
345 }
346 } else if (!parsedDependenciesMap.equals(other.parsedDependenciesMap)) {
347 return false;
348 }
349 if (potentialJavaLangSeenDependenciesMap == null) {
350 if (other.potentialJavaLangSeenDependenciesMap != null) {
351 return false;
352 }
353 } else if (!potentialJavaLangSeenDependenciesMap.equals(other.potentialJavaLangSeenDependenciesMap)) {
354 return false;
355 }
356 return true;
357 }
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377 private Collection < GenericDependency > getDisplayableParsedAndSeenDependencies(
378 final Set < DisplayType > displayTypesOpts, final Pattern displayPackageNamePatternOpt,
379 final Pattern displayNamePatternOpt) {
380 final Set < GenericDependency > displayableParsedAndSeenDependencies = new TreeSet < GenericDependency >();
381 for (final GenericDependency dependency : getParsedAndSeenDependencies()) {
382 if (dependency.getDependencyType().isDisplayable(displayTypesOpts, displayPackageNamePatternOpt,
383 displayNamePatternOpt)) {
384 displayableParsedAndSeenDependencies.add(dependency);
385 }
386 }
387
388 return displayableParsedAndSeenDependencies;
389 }
390
391
392
393
394
395
396
397
398 private Pattern getDisplayNamePattern() {
399 return displayNamePattern;
400 }
401
402
403
404
405
406
407
408
409 private Pattern getDisplayPackageNamePattern() {
410 return displayPackageNamePattern;
411 }
412
413
414
415
416
417
418
419
420 private Set < DisplayType > getDisplayTypesOptions() {
421 return displayTypesOptions;
422 }
423
424
425
426
427
428
429 @Override
430 public Collection < GenericDependency > getParsedAndSeenDependencies() {
431 return getParsedAndSeenDependenciesMap().values();
432 }
433
434
435
436
437
438
439
440
441 private Map < String, GenericDependency > getParsedAndSeenDependenciesMap() {
442 return parsedAndSeenDependenciesMap;
443 }
444
445
446
447
448
449
450 @Override
451 public Collection < GenericDependency > getParsedDependencies() {
452 return getParsedDependenciesMap().values();
453 }
454
455
456
457
458
459
460
461
462 private Map < String, GenericDependency > getParsedDependenciesMap() {
463 return parsedDependenciesMap;
464 }
465
466
467
468
469
470
471 @Override
472 public GenericDependency getParsedDependency(final String fullName) {
473 checkNull(fullName, DEPENDENCY_NAME_NULL_ERROR);
474 return getParsedDependenciesMap().get(fullName);
475 }
476
477
478
479
480
481
482 @Override
483 public GenericDependency getParsedOrSeenDependency(final String fullName) {
484 checkNull(fullName, DEPENDENCY_NAME_NULL_ERROR);
485 return getParsedAndSeenDependenciesMap().get(fullName);
486 }
487
488
489
490
491
492
493 @Override
494 public PlantUMLClassesDiagram getPlantUMLClassesDiagram() {
495 final Set < PlantUMLClassesDiagramElement > plantUMLClassesDiagramElements = new TreeSet < PlantUMLClassesDiagramElement >();
496 final Set < PlantUMLClassesDiagramRelation > plantUMLClassesDiagramRelations = new TreeSet < PlantUMLClassesDiagramRelation >();
497
498 for (final GenericDependency genericDependency : getDisplayableParsedAndSeenDependencies(
499 getDisplayTypesOptions(), getDisplayPackageNamePattern(), getDisplayNamePattern())) {
500 plantUMLClassesDiagramElements
501 .add(genericDependency.getDependencyType().getPlantUMLClassesDiagramElement());
502 plantUMLClassesDiagramRelations.addAll(genericDependency.getDependencyType()
503 .getPlantUMLClassesDiagramRelations(getDisplayTypesOptions(), getDisplayPackageNamePattern(),
504 getDisplayNamePattern()));
505 }
506
507 return new PlantUMLClassesDiagramImpl(plantUMLClassesDiagramElements, plantUMLClassesDiagramRelations);
508 }
509
510
511
512
513
514
515 @Override
516 public Collection < GenericDependency > getPotentialJavaLangSeenDependencies() {
517 return getPotentialJavaLangSeenDependenciesMap().values();
518 }
519
520
521
522
523
524
525
526
527 private Map < String, GenericDependency > getPotentialJavaLangSeenDependenciesMap() {
528 return potentialJavaLangSeenDependenciesMap;
529 }
530
531
532
533
534
535
536 @Override
537 public int hashCode() {
538 final int prime = 31;
539 int result = 1;
540 result = prime * result + ((displayNamePattern == null) ? 0 : displayNamePattern.hashCode());
541 result = prime * result + ((displayPackageNamePattern == null) ? 0 : displayPackageNamePattern.hashCode());
542 result = prime * result + ((displayTypesOptions == null) ? 0 : displayTypesOptions.hashCode());
543 result = prime * result
544 + ((parsedAndSeenDependenciesMap == null) ? 0 : parsedAndSeenDependenciesMap.hashCode());
545 result = prime * result + ((parsedDependenciesMap == null) ? 0 : parsedDependenciesMap.hashCode());
546 result = prime
547 * result
548 + ((potentialJavaLangSeenDependenciesMap == null) ? 0 : potentialJavaLangSeenDependenciesMap.hashCode());
549 return result;
550 }
551
552
553
554
555
556
557 @Override
558 public boolean hasToDisplay(final DisplayType displayType) {
559 return getDisplayTypesOptions().contains(displayType);
560 }
561
562
563
564
565
566
567 @Override
568 public void removeAllPotentialJavaLangSeenDependencyAndChangePackageToJavaLang() {
569 for (final GenericDependency dependency : getPotentialJavaLangSeenDependencies()) {
570 getParsedAndSeenDependenciesMap().remove(dependency.getFullName());
571 dependency.getDependencyType().setFullName(JAVA_LANG_PACKAGE, dependency.getName());
572 addSeenDependencies(dependency);
573 }
574
575 getPotentialJavaLangSeenDependenciesMap().clear();
576 }
577
578
579
580
581
582
583 @Override
584 public void removePotentialJavaLangSeenDependency(final String fullName) {
585 checkNull(fullName, DEPENDENCY_NAME_NULL_ERROR);
586 getPotentialJavaLangSeenDependenciesMap().remove(fullName);
587 }
588
589
590
591
592
593
594
595
596
597 private void setDisplayNamePattern(final Pattern value) {
598 displayNamePattern = value;
599 }
600
601
602
603
604
605
606
607
608
609 private void setDisplayPackageNamePattern(final Pattern value) {
610 displayPackageNamePattern = value;
611 }
612
613
614
615
616
617
618
619
620
621 private void setDisplayTypesOptions(final Set < DisplayType > value) {
622 displayTypesOptions = value;
623 }
624
625
626
627
628
629
630
631
632
633 private void setParsedAndSeenDependenciesMap(final Map < String, GenericDependency > value) {
634 parsedAndSeenDependenciesMap = value;
635 }
636
637
638
639
640
641
642
643
644
645 private void setParsedDependenciesMap(final Map < String, GenericDependency > value) {
646 parsedDependenciesMap = value;
647 }
648
649
650
651
652
653
654
655
656
657
658 private void setPotentialJavaLangSeenDependenciesMap(final Map < String, GenericDependency > value) {
659 potentialJavaLangSeenDependenciesMap = value;
660 }
661
662
663
664
665
666
667 @Override
668 public String toString() {
669 return getClass().getSimpleName() + " [displayTypesOptions=" + displayTypesOptions
670 + ", displayPackageNamePattern=" + displayPackageNamePattern + ", displayNamePattern="
671 + displayNamePattern + ", parsedAndSeenDependenciesMap=" + parsedAndSeenDependenciesMap
672 + ", potentialJavaLangSeenDependenciesMap=" + potentialJavaLangSeenDependenciesMap
673 + ", parsedDependenciesMap=" + parsedDependenciesMap + "]";
674 }
675 }