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      protected final List elementList = new ArrayList();
    21
    22      /***
    23       * @param name
    24       */
    25      public ElementSet(String name) {
    26          this(null, name, null);
    27      }
    28
    29      /***
    30       * @param parent
    31       */
    32      public ElementSet(Element parent) {
    33          this(parent, null, null);
    34      }
    35
    36      /***
    37       * @param parent
    38       * @param name
    39       */
    40      public ElementSet(Element parent, String name) {
    41          this(parent, name, null);
    42      }
    43
    44      /***
    45       * @param parent
    46       * @param name
    47       * @param label
    48       * @param isEntity
    49       */
    50      public ElementSet(Element parent, String name, String label) {
    51          super(parent, name, label);
    52      }
    53
    54      /***
    55       * @return Returns the entity.
    56       */
    57      abstract public boolean isEntity();
    58
    59      public void add(Element element) {
    60          if (element == null)
    61              throw new NullPointerException("element must not be null");
    62          if (!elementList.contains(element))
    63              elementList.add(element);
    64          if (element.getParentElement() != this)
    65              element.setParentElement(isEntity() ? this : this.getParentElement());
    66      }
    67
    68      /***
    69       *
    70       */
    71      public final void clear() {
    72          elementList.clear();
    73      }
    74
    75      /***
    76       * @param key
    77       * @return
    78       */
    79      public final boolean contains(String name) {
    80          if (StringUtils.isEmpty(name))
    81              return false;
    82          for (int i = 0; i < elementList.size(); i++) {
    83              final Element element = (Element) elementList.get(i);
    84              if (name.equals(element.getName()))
    85                  return true;
    86          }
    87          return false;
    88      }
    89
    90      /***
    91       * @param value
    92       * @return
    93       */
    94      protected boolean contains(Element element) {
    95          return elementList.contains(element);
    96      }
    97
    98      /***
    99       * @param key
    100      * @return
    101      */
    102     public Element getElement(int index) {
    103         return (index < 0) ? null : (Element) elementList.get(index);
    104     }
    105
    106     /***
    107      * @param key
    108      * @return
    109      */
    110     public Element getElement(String name) {
    111         return getElement(indexOf(name));
    112     }
    113
    114     /***
    115      * @param key
    116      * @return
    117      */
    118     public final int indexOf(String name) {
    119         if (StringUtils.isEmpty(name))
    120             return -1;
    121         for (int i = 0; i < elementList.size(); i++) {
    122             final Element element = (Element) elementList.get(i);
    123             if (name.equals(element.getName()))
    124                 return i;
    125         }
    126         return -1;
    127     }
    128
    129     /***
    130      * @param key
    131      * @return
    132      */
    133     public final int indexOf(Element element) {
    134         return elementList.indexOf(element);
    135     }
    136
    137     /***
    138      * @return
    139      */
    140     public final boolean isEmpty() {
    141         return elementList.isEmpty();
    142     }
    143
    144     /***
    145      * @return
    146      */
    147     public final Iterator iterator() {
    148         return elementList.iterator();
    149     }
    150
    151     /***
    152      * @param key
    153      * @return
    154      */
    155     protected Element removeElement(String name) {
    156         final int index = indexOf(name);
    157         return (index < 0) ? null : (Element) elementList.remove(index);
    158     }
    159
    160     /***
    161      * @return
    162      */
    163     public final int size() {
    164         return (elementList == null) ? 0 : elementList.size();
    165     }
    166
    167     public List toList() {
    168         return new ArrayList(this.elementList);
    169     }
    170
    171     /*
    172      * (non-Javadoc)
    173      *
    174      * @see org.asyrinx.joey.gen.model.Element#equals(java.lang.Object)
    175      */
    176     public boolean equals(Object obj) {
    177         if (!(obj instanceof ElementSet))
    178             return false;
    179         if (!super.equals(obj))
    180             return false;
    181         final ElementSet other = (ElementSet) obj;
    182         if (this.size() != other.size())
    183             return false;
    184         if (!equalsChildren(other))
    185             return false;
    186         return true;
    187     }
    188
    189     protected boolean equalsChildren(ElementSet other) {
    190         if (careChildOrder())
    191             return equalsChildrenByOder(other);
    192         else if (hasNullNamedChild())
    193             return equalsChildrenByOder(other);
    194         else
    195             return equalsChildrenByName(other);
    196     }
    197
    198     public boolean hasNullNamedChild() {
    199         for (Iterator i = this.iterator(); i.hasNext();) {
    200             final Element element = (Element) i.next();
    201             if (element.getName() == null)
    202                 return true;
    203         }
    204         return false;
    205     }
    206
    207     protected boolean equalsChildrenByOder(ElementSet other) {
    208         for (int i = 0; i < this.size(); i++) {
    209             final Element mine = this.getElement(i);
    210             final Element others = other.getElement(i);
    211             if (!mine.equals(others))
    212                 return false;
    213         }
    214         return true;
    215     }
    216
    217     protected boolean equalsChildrenByName(ElementSet other) {
    218         for (Iterator i = this.iterator(); i.hasNext();) {
    219             final Element mine = (Element) i.next();
    220             final Element others = other.getElement(mine.getName());
    221             if (!mine.equals(others))
    222                 return false;
    223         }
    224         return true;
    225     }
    226
    227     public boolean careChildOrder() {
    228         return false;
    229     }
    230
    231 }