View Javadoc

1   /*
2   Copyright (C) 2004 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  package com.gridsystems.beanfilter;
18  
19  import java.util.Collection;
20  import java.util.HashSet;
21  import java.util.Set;
22  
23  /**
24   * List / Set Value.
25   *
26   * @author Rodrigo Ruiz
27   * @version 1.0
28   */
29  public class CollectionValue extends EvalValue {
30    /**
31     * Collection of values.
32     */
33    private Collection<EvalValue> col = new HashSet<EvalValue>();
34  
35    /**
36     * Class of the elements of this Collections.
37     */
38    private Class<?> itemsClass = null;
39  
40    /**
41     * Type of list returned. ? or *
42     */
43    private int typeList = FilterParserConstants.ALLITEMS;
44  
45    /**
46     * Creates a new instance.
47     *
48     * @param token  The token
49     */
50    public CollectionValue(Token token) {
51      super(token.beginLine, token.beginColumn);
52    }
53  
54    /**
55     * {@inheritDoc}
56     */
57    @SuppressWarnings("unchecked")
58    @Override public Object getValue(Object src) throws EvalException {
59      HashSet<Object> set;
60      if (typeList == FilterParserConstants.ANYITEMS) {
61        set = new AnyItemsHashSet();
62      } else {
63        set = new AllItemsHashSet();
64      }
65      for (EvalValue val : col) {
66        Object value = val.getValue(src);
67        if (value instanceof Set) {
68          set.addAll((Set<Object>)value);
69        } else {
70          set.add(value);
71        }
72      }
73      return set;
74    }
75  
76    /**
77     * Adds an item to this collection.
78     *
79     * @param value  The item to add
80     * @param parentClass Parent Class
81     * @throws EvalException If an error occurs
82     */
83    public void add(EvalValue value, Class<?> parentClass) throws EvalException {
84      if (value != null) {
85        if (itemsClass == null) {
86          itemsClass = value.getClassValue(parentClass);
87        } else {
88          Class<?> valueClass = value.getClassValue(parentClass);
89          if ((valueClass != null) && !itemsClass.equals(valueClass)) {
90            // FTR008=The element {0} with type {1} at position [{2}, {3}],
91            // has the type that it is different to the collection ({4})
92            Object[] params = new Object[] { value.toString(), valueClass.getName(),
93              value.getLinePos(), value.getCharPos(), itemsClass.getName() };
94            throw new EvalException("FTR008", params);
95          }
96        }
97        this.col.add(value);
98      }
99    }
100 
101   /**
102    * {@inheritDoc}
103    */
104   @Override public String toString() {
105     return col.toString();
106   }
107 
108   /**
109    * @param typeList The typeList to set.
110    */
111   public void setTypeList(int typeList) {
112     this.typeList = typeList;
113   }
114 
115   /**
116    * {@inheritDoc}
117    */
118   @Override
119   public Class<?> getClassValue(Class<?> parentClass) {
120     return itemsClass;
121   }
122 
123 }