This project has retired. For details please refer to its Attic page.
ListAttribute xref
View Javadoc

1   /*
2    * $Id: ListAttribute.java 832867 2009-11-04 20:16:23Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.tiles;
23  
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /**
29   * An attribute as a <code>List</code>.
30   * This attribute associates a name with a list. The list can be found by the
31   * property name.
32   * Elements in list are retrieved using List methods.
33   * This class is used to read configuration files.
34   *
35   * @version $Rev: 832867 $ $Date: 2009-11-05 07:16:23 +1100 (Thu, 05 Nov 2009) $
36   * @since 2.1.0
37   */
38  public class ListAttribute extends Attribute {
39  
40      /**
41       * If true, the attribute will put the elements of the attribute with the
42       * same name of the parent definition before the ones specified here. By
43       * default, it is 'false'.
44       */
45      private boolean inherit = false;
46  
47      /**
48       * Constructor.
49       *
50       * @since 2.1.0
51       */
52      public ListAttribute() {
53          setValue(new ArrayList<Object>());
54      }
55  
56      /**
57       * Constructor.
58       *
59       * @param value List.
60       * @since 2.1.0
61       */
62      public ListAttribute(List<Attribute> value) {
63          setValue(value);
64      }
65  
66      /**
67       * Copy constructor.
68       *
69       * @param toCopy The list attribute to copy.
70       * @since 2.1.3
71       */
72      public ListAttribute(ListAttribute toCopy) {
73          super(toCopy);
74          List<Attribute> attributesToCopy = toCopy.getValue();
75          if (attributesToCopy != null) {
76              List<Attribute> attributes = new ArrayList<Attribute>(attributesToCopy.size());
77              for (Attribute attribute : attributesToCopy) {
78                  if (attribute != null) {
79                      attributes.add(attribute.clone());
80                  } else {
81                      attributes.add(null);
82                  }
83              }
84              setValue(attributes);
85          }
86          this.inherit = toCopy.inherit;
87      }
88  
89      /**
90       * Sets the list of the attributes that are elements of this attribute.
91       *
92       * @param attributes The attributes.
93       * @since 3.0.0
94       */
95      public void setValue(List<Attribute> attributes) {
96          super.setValue(attributes);
97      }
98  
99      /**
100      * Returns the list of the attributes that are elements of this attribute.
101      *
102      * @return The attributes.
103      * @since 3.0.0
104      */
105     @SuppressWarnings("unchecked")
106     @Override
107     public List<Attribute> getValue() {
108         return (List<Attribute>) super.getValue();
109     }
110 
111     /**
112      * Add an element in list.
113      * We use a property to avoid rewriting a new class.
114      *
115      * @param element XmlAttribute to add.
116      * @since 2.1.0
117      */
118     public void add(Attribute element) {
119         getValue().add(element);
120     }
121 
122     /**
123      * If true, the attribute will put the elements of the attribute with the
124      * same name of the parent definition before the ones specified here. By
125      * default, it is 'false'
126      *
127      * @param inherit The "inherit" value.
128      * @since 2.1.0
129      */
130     public void setInherit(boolean inherit) {
131         this.inherit = inherit;
132     }
133 
134     /**
135      * If true, the attribute will put the elements of the attribute with the
136      * same name of the parent definition before the ones specified here. By
137      * default, it is 'false'
138      *
139      * @return inherit The "inherit" value.
140      * @since 2.1.0
141      */
142     public boolean isInherit() {
143         return inherit;
144     }
145 
146     /**
147      * Inherits elements present in a "parent" list attribute. The elements will
148      * be put before the ones already present.
149      *
150      * @param parent The parent list attribute.
151      * @since 2.1.0
152      */
153     @SuppressWarnings("unchecked")
154     public void inherit(ListAttribute parent) {
155         List<Attribute> tempList = new ArrayList<Attribute>();
156         tempList.addAll((List<Attribute>) parent.value);
157         tempList.addAll((List<Attribute>) value);
158         setValue(tempList);
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public boolean equals(Object obj) {
164         ListAttribute attribute = (ListAttribute) obj;
165         return super.equals(attribute) && this.inherit == attribute.inherit;
166     }
167 
168     /** {@inheritDoc} */
169     @Override
170     public int hashCode() {
171         return super.hashCode() + Boolean.valueOf(inherit).hashCode();
172     }
173 
174     /** {@inheritDoc} */
175     @Override
176     public ListAttribute clone() {
177         return new ListAttribute(this);
178     }
179 }