comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/DefaultInputData.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents f953c9a559d8
children
comparison
equal deleted inserted replaced
1027:fca4b5eb8d2f 1119:7c4f81f74c47
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.gnv.state;
10
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import org.apache.log4j.Logger;
15
16 /**
17 * The default implementation of <code>InputData</code>. This object stores
18 * multiple values separated by {@link #VALUE_SEPARATOR} for a specific key.
19 *
20 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
21 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
22 */
23 public class DefaultInputData implements InputData {
24
25 /**
26 *
27 */
28 private static final long serialVersionUID = 4308041648698108066L;
29
30 private static final Logger logger = Logger.getLogger(DefaultInputData.class);
31
32 /**
33 * The character used to separate the different values.
34 */
35 public final static String VALUE_SEPARATOR = " , ";
36
37 /**
38 * The key/name of this object.
39 */
40 protected String name;
41
42 /**
43 * Values separated by {@link #VALUE_SEPARATOR}.
44 */
45 protected String value;
46
47 /**
48 * Descriptions for values used for user interface creation. Each value
49 * should have an own description in this map.
50 */
51 protected Map description;
52
53 /**
54 * An extra object. Might be everything.
55 */
56 protected Object object;
57
58
59 /**
60 * Constructor
61 */
62 public DefaultInputData(String name, String value) {
63 this.name = name;
64 this.value = value;
65 }
66
67
68 public DefaultInputData(String name, Object object) {
69 this.name = name;
70 this.object = object;
71 }
72
73
74 public DefaultInputData(
75 String name,
76 String value,
77 Object object)
78 {
79 this.name = name;
80 this.object = object;
81 this.value = value;
82 }
83
84
85 public String getName() {
86 return this.name;
87 }
88
89
90 public String getValue() {
91 return this.value;
92 }
93
94
95 public void setObject(Object object) {
96 this.object = object;
97 }
98
99
100 public Object getObject() {
101 return object;
102 }
103
104 /**
105 *
106 * @param key Key needs to be a single value of {@link #value}.
107 * @return the description.
108 */
109 public String getDescription(String key) {
110 if (description == null)
111 return null;
112
113 return (String) description.get(key);
114 }
115
116 /**
117 * Return all descriptions as array.
118 *
119 * @return descriptions as array.
120 */
121 public String[] getDescription() {
122 String[] values = splitValue();
123 int length = values.length;
124
125 String[] description = new String[length];
126 for (int i = 0; i < length; i++) {
127 description[i] = (String) this.description.get(values[i]);
128 }
129
130 return description;
131 }
132
133
134 public void setDescription(String[] description) {
135 if (this.description == null)
136 this.description = new HashMap();
137
138 String[] values = splitValue();
139
140 int length = values.length;
141 int descLength = description.length;
142
143 for (int i = 0; i < length; i++) {
144 if (i < descLength) {
145 this.description.put(values[i], description[i]);
146 }
147 else {
148 break;
149 }
150 }
151 }
152
153
154 @Override
155 public String toString() {
156 return this.name + "==> " + this.value;
157 }
158
159
160 public void concartValue(String value) {
161 this.value = this.value + VALUE_SEPARATOR + value;
162 }
163
164
165 public String[] splitValue() {
166 if (this.value != null){
167 return this.value.split(VALUE_SEPARATOR);
168 }
169 return null;
170 }
171
172 @Override
173 public int hashCode() {
174 logger.debug("*************************************");
175 logger.debug("HashCode name: " + name);
176
177 int hash = 0;
178
179 hash ^= name.hashCode();
180
181 if (value != null) {
182 hash ^= value.hashCode() << 2;
183 }
184
185 if (object != null) {
186 hash ^= object.hashCode() << 4;
187 }
188
189 logger.debug("HashCode value: " + hash);
190 logger.debug("*************************************");
191
192 return hash;
193 }
194
195
196 @Override
197 public boolean equals(Object o) {
198 if (!(o instanceof DefaultInputData))
199 return false;
200
201 DefaultInputData other = (DefaultInputData) o;
202
203 if (!name.equals(other.name))
204 return false;
205
206 if ((value == null) && (other.value == null))
207 return false;
208
209 if (!value.equals(other.value))
210 return false;
211
212 if (!(object == null) && (other.object == null))
213 return false;
214
215 if (!(object == other.object))
216 return false;
217
218 return true;
219 }
220 }
221 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org