View Javadoc

1   package cz.cuni.amis.pogamut.sposh.elements;
2   
3   import java.util.List;
4   import java.util.NoSuchElementException;
5   
6   /**
7    * This is class used as intermediate class between PoshElement and DataNodes of
8    * POSH elements. Some common functions are implemented here.
9    *
10   * @author HonzaH
11   */
12  public abstract class PoshDummyElement<THIS extends PoshElement, PARENT extends PoshElement> extends PoshElement<THIS, PARENT> {
13  
14      /**
15       * Regular pattern, only text that matches this pattern can be a name of the
16       * POSH element. This pattern must be same as token
17       * <code>NAME</code> in the PoshParser.jj
18       * <p/>
19       * Because of dash (-), not all strings that match this pattern are FQN.
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       * Take the @list of elements and one @child of the @list. Move the @child
25       * so that it is at the @newIndex and the rest of elements keep their order.
26       * Once it is done, emit the event. If @child is not part of the @list,
27       * throw {@link NoSuchElementException}, if @newIndex is same as the index
28       * the @child is currently at, don't do anything.
29       *
30       * @param <T>
31       * @param list
32       * @param child
33       * @param newIndex Index
34       * @return did method move and emited?
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       * Is passed name used by one of elements?
55       *
56       * @param name name to be tested
57       * @param elements elements againt which the test is done
58       * @return true if name is used, false otherwise
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       * Get valid name for new element, but name can't be same as any name used
72       * by passed elements.
73       *
74       * @param template string used as prefix of created name
75       * @param elements elements that must have different name that created name
76       * @return valid name that can be immediately used as OK name of new drive.
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       * Get index of @element in all @elements.
88       *
89       * @param <ELEMENT> Type of elements.
90       * @param elements List of elements where
91       * @param element element for which we are looking for in elements.
92       * @return Found index.
93       * @throws IllegalArgumentException If element is not among elements.
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 }