BrightSide Workbench Full Report + Source Code
AttributeItem.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.context;
19 
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import org.turro.string.ObjectString;
26 import org.turro.string.Strings;
27 import org.turro.elephant.context.IConstructor;
28 import org.turro.util.PhraseBuilder;
29 
34 public class AttributeItem {
35  public static final int
37  NUMBER_ATTR = 1,
38  DATE_ATTR = 2,
39  BOOLEAN_ATTR = 3,
40  SELECT_ATTR = 4;
41 
42  public String name, value, defaultValue;
43  public int type;
44  public List choices = null;
45 
47  public AttributeItem(String name, Boolean value, Boolean defaultValue) {
48  this(name,
49  value.booleanValue() ? "true" : "false",
50  NUMBER_ATTR,
51  defaultValue.booleanValue() ? "true" : "false");
52  }
53 
55  public AttributeItem(String name, Number value, Number defaultValue) {
56  this(name,
57  ObjectString.formatNativeObject(value, true),
58  NUMBER_ATTR,
59  ObjectString.formatNativeObject(defaultValue, true));
60  }
61 
63  public AttributeItem(String name, Date value, Date defaultValue) {
64  this(name,
65  ObjectString.formatObject(value, ObjectString.COMPRESSED_DATE_PATTERN, true),
66  DATE_ATTR,
67  ObjectString.formatObject(defaultValue, ObjectString.COMPRESSED_DATE_PATTERN, true));
68  }
69 
71  public AttributeItem(String name, String value, int type, String defaultValue) {
72  this.name = name;
73  this.value = value;
74  this.type = type;
75  this.defaultValue = defaultValue;
76  }
77 
78  public void addChoice(String choice) {
79  if(choices == null)
80  choices = new ArrayList();
81  choices.add(choice);
82  type = SELECT_ATTR;
83  }
84 
85  public String getValue() {
86  if(value == null) return defaultValue;
87  return value;
88  }
89 
90  public String getValue(String defaultValue) {
91  if(value == null) return defaultValue;
92  return value;
93  }
94 
95  public void setValue(String value) {
96  this.value = value;
97  }
98 
99  public double getDoubleValue() {
100  return ((Double) ObjectString.parseNativeString(getValue(), Double.class, true)).doubleValue();
101  }
102 
103  public double getDoubleValue(double defaultValue) {
104  if(value == null) {
105  return defaultValue;
106  }
107  return getDoubleValue();
108  }
109 
110  public int getIntegerValue() {
111  return ((Integer) ObjectString.parseNativeString(getValue(), Integer.class, true)).intValue();
112  }
113 
114  public int getIntegerValue(int defaultValue) {
115  if(value == null) {
116  return defaultValue;
117  }
118  return getIntegerValue();
119  }
120 
121  public long getLongValue() {
122  return ((Long) ObjectString.parseNativeString(getValue(), Long.class, true)).longValue();
123  }
124 
125  public long getLongValue(long defaultValue) {
126  if(value == null) {
127  return defaultValue;
128  }
129  return getLongValue();
130  }
131 
132  public boolean getBooleanValue() {
133  String v = getValue();
134  return "true".equals(v);
135  }
136 
137  public boolean getBooleanValue(boolean defaultValue) {
138  if(value == null) {
139  return defaultValue;
140  }
141  return getBooleanValue();
142  }
143 
144  public void setBooleanValue(boolean value) {
145  this.value = value ? "true" : "false";
146  }
147 
148  public Date getDateValue() {
149  String v = getValue();
150  return (Date) ObjectString.parseString(v, ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false);
151  }
152 
153  public Date getDateValue(Date defaultValue) {
154  if(value == null) {
155  return defaultValue;
156  }
157  return getDateValue();
158  }
159 
160  public List getChoices() {
161  return choices;
162  }
163 
164  public <T extends Enum<T>> Set getEnumSet(Class<T> javaClass) {
165  Set set = new HashSet();
166  if(javaClass.isEnum() && !Strings.isBlank(value)) {
167  for(Enum enumVal: javaClass.getEnumConstants()) {
168  if(value.contains(enumVal.toString())) {
169  set.add(enumVal);
170  }
171  }
172  }
173  return set;
174  }
175 
176  public static AttributeItem getFromParameter(IConstructor constructor, String name, String defaultValue) {
177  String[] values = constructor.getParameterValues(name);
178  return new AttributeItem(name, format(values), STRING_ATTR, defaultValue);
179  }
180 
181  private static String format(String[] values) {
182  if(values != null) {
183  PhraseBuilder pb = new PhraseBuilder();
184  for(String s : values) {
185  pb.addWord(s);
186  pb.addPendingSeparator(",");
187  }
188  return pb.toString();
189  }
190  return null;
191  }
192 
193 }
static AttributeItem getFromParameter(IConstructor constructor, String name, String defaultValue)
AttributeItem(String name, Date value, Date defaultValue)
AttributeItem(String name, Boolean value, Boolean defaultValue)
AttributeItem(String name, String value, int type, String defaultValue)
AttributeItem(String name, Number value, Number defaultValue)
boolean getBooleanValue(boolean defaultValue)
String[] getParameterValues(String param)