1 package cz.cuni.amis.pogamut.sposh.elements;
2
3 import java.util.List;
4 import java.util.NoSuchElementException;
5
6
7
8
9
10
11
12 public abstract class PoshDummyElement<THIS extends PoshElement, PARENT extends PoshElement> extends PoshElement<THIS, PARENT> {
13
14
15
16
17
18
19
20
21 public static final String IDENT_PATTERN = "([a-zA-Z][_\\-a-zA-Z0-9]*\\.)*[a-zA-Z][_\\-a-zA-Z0-9]*";
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 protected <T extends PoshElement> boolean moveChildInList(List<T> list, T child, int newIndex) {
37 int oldIndex = list.indexOf(child);
38 if (oldIndex == -1) {
39 throw new NoSuchElementException("Child " + child + "is not in collection.");
40 }
41 if (newIndex == oldIndex) {
42 return false;
43 }
44
45 list.remove(oldIndex);
46 list.add(newIndex, child);
47
48 emitChildMove(child, oldIndex, newIndex);
49
50 return true;
51 }
52
53
54
55
56
57
58
59
60 protected static boolean isUsedName(String name, List<? extends INamedElement> elements) {
61 for (INamedElement element : elements) {
62 String elementName = element.getName();
63 if (elementName.equals(name)) {
64 return true;
65 }
66 }
67 return false;
68 }
69
70
71
72
73
74
75
76
77
78 protected static String getUnusedName(String template, List<? extends INamedElement> elements) {
79 int i = 1;
80 while (isUsedName(template + i, elements)) {
81 i++;
82 }
83 return template + i;
84 }
85
86
87
88
89
90
91
92
93
94
95 protected <ELEMENT extends PoshElement & INamedElement> int getElementId(List<ELEMENT> elements, ELEMENT element) {
96 int elementIndex = elements.indexOf(element);
97 if (elementIndex == -1) {
98 throw new IllegalArgumentException("Element of type '" + element.getType() + "' with name '" + element.getName() + "' not present in the plan.");
99 }
100 return elementIndex;
101 }
102 }