View Javadoc

    1   /*
    2    * joey-gen and its relative products are published under the terms
    3    * of the Apache Software License.
    4    *
    5    * Created on 2004/08/10 18:06:50
    6    */
    7   package org.asyrinx.joey.gen.model;
    8
    9   import java.util.ArrayList;
    10  import java.util.Iterator;
    11  import java.util.List;
    12
    13  import org.apache.commons.lang.StringUtils;
    14
    15  /***
    16   * @author akima
    17   */
    18  public abstract class ElementSet extends Element {
    19      /***
    20       * @param name
    21       */
    22      public ElementSet(String name) {
    23          this(null, name, null);
    24      }
    25
    26      /***
    27       * @param parent
    28       */
    29      public ElementSet(Element parent) {
    30          this(parent, null, null);
    31      }
    32
    33      /***
    34       * @param parent
    35       * @param name
    36       */
    37      public ElementSet(Element parent, String name) {
    38          this(parent, name, null);
    39      }
    40
    41      /***
    42       * @param parent
    43       * @param name
    44       * @param label
    45       * @param isEntity
    46       */
    47      public ElementSet(Element parent, String name, String label) {
    48          super(parent, name, label);
    49      }
    50
    51      protected final List elementList = new ArrayList();
    52
    53      /***
    54       * @return Returns the entity.
    55       */
    56      abstract public boolean isEntity();
    57
    58      public void add(Element element) {
    59          if (element == null)
    60              throw new NullPointerException("element must not be null");
    61          if (!elementList.contains(element))
    62              elementList.add(element);
    63          if (element.getParentElement() != this)
    64              element.setParentElement(isEntity() ? this : this.getParentElement());
    65      }
    66
    67      /***
    68       *
    69       */
    70      public final void clear() {
    71          elementList.clear();
    72      }
    73
    74      /***
    75       * @param key
    76       * @return
    77       */
    78      public final boolean contains(String name) {
    79          if (StringUtils.isEmpty(name))
    80              return false;
    81          for (int i = 0; i < elementList.size(); i++) {
    82              final Element element = (Element) elementList.get(i);
    83              if (name.equals(element.getName()))
    84                  return true;
    85          }
    86          return false;
    87      }
    88
    89      /***
    90       * @param value
    91       * @return
    92       */
    93      protected boolean contains(Element element) {
    94          return elementList.contains(element);
    95      }
    96
    97      /***
    98       * @param key
    99       * @return
    100      */
    101     protected Element getElement(int index) {
    102         return (index < 0) ? null : (Element) elementList.get(index);
    103     }
    104
    105     /***
    106      * @param key
    107      * @return
    108      */
    109     protected Element getElement(String name) {
    110         return getElement(indexOf(name));
    111     }
    112
    113     /***
    114      * @param key
    115      * @return
    116      */
    117     public final int indexOf(String name) {
    118         if (StringUtils.isEmpty(name))
    119             return -1;
    120         for (int i = 0; i < elementList.size(); i++) {
    121             final Element element = (Element) elementList.get(i);
    122             if (name.equals(element.getName()))
    123                 return i;
    124         }
    125         return -1;
    126     }
    127
    128     /***
    129      * @param key
    130      * @return
    131      */
    132     public final int indexOf(Element element) {
    133         return elementList.indexOf(element);
    134     }
    135
    136     /***
    137      * @return
    138      */
    139     public final boolean isEmpty() {
    140         return elementList.isEmpty();
    141     }
    142
    143     /***
    144      * @return
    145      */
    146     public final Iterator iterator() {
    147         return elementList.iterator();
    148     }
    149
    150     /***
    151      * @param key
    152      * @return
    153      */
    154     protected Element removeElement(String name) {
    155         final int index = indexOf(name);
    156         return (index < 0) ? null : (Element) elementList.remove(index);
    157     }
    158
    159     /***
    160      * @return
    161      */
    162     public final int size() {
    163         return elementList.size();
    164     }
    165
    166     public List toList() {
    167         return new ArrayList(this.elementList);
    168     }
    169 }