Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/Access.java @ 3651:06a65baae494
merged flys-artifacts/2.9
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:43 +0200 |
parents | 9422b559b2d5 |
children | 5ff3b2f5fb1c |
comparison
equal
deleted
inserted
replaced
3549:6a8f83c538e3 | 3651:06a65baae494 |
---|---|
1 package de.intevation.flys.artifacts.access; | |
2 | |
3 import de.intevation.artifactdatabase.data.StateData; | |
4 | |
5 import de.intevation.flys.artifacts.FLYSArtifact; | |
6 | |
7 import de.intevation.flys.artifacts.model.DateRange; | |
8 | |
9 import de.intevation.flys.utils.FLYSUtils; | |
10 | |
11 import gnu.trove.TDoubleArrayList; | |
12 | |
13 import java.util.ArrayList; | |
14 import java.util.Date; | |
15 | |
16 import org.apache.log4j.Logger; | |
17 | |
18 public class Access | |
19 { | |
20 private static Logger log = Logger.getLogger(Access.class); | |
21 | |
22 protected FLYSArtifact artifact; | |
23 | |
24 public Access() { | |
25 } | |
26 | |
27 public Access(FLYSArtifact artifact) { | |
28 this.artifact = artifact; | |
29 } | |
30 | |
31 public FLYSArtifact getArtifact() { | |
32 return artifact; | |
33 } | |
34 | |
35 public void setArtifact(FLYSArtifact artifact) { | |
36 this.artifact = artifact; | |
37 } | |
38 | |
39 protected String getString(String key) { | |
40 StateData sd = artifact.getData(key); | |
41 if (sd == null) { | |
42 log.warn("missing '" + key + "' value"); | |
43 return null; | |
44 } | |
45 return (String)sd.getValue(); | |
46 } | |
47 | |
48 protected Double getDouble(String key) { | |
49 StateData sd = artifact.getData(key); | |
50 if (sd == null) { | |
51 log.warn("missing '" + key + "' value"); | |
52 return null; | |
53 } | |
54 try { | |
55 return Double.valueOf((String)sd.getValue()); | |
56 } | |
57 catch (NumberFormatException nfe) { | |
58 log.warn(key + " '" + sd.getValue() + "' is not numeric."); | |
59 } | |
60 return null; | |
61 } | |
62 | |
63 protected Long getLong(String key) { | |
64 StateData sd = artifact.getData(key); | |
65 if (sd == null) { | |
66 log.warn("missing '" + key + "' value"); | |
67 return null; | |
68 } | |
69 try { | |
70 return Long.valueOf((String)sd.getValue()); | |
71 } | |
72 catch (NumberFormatException nfe) { | |
73 log.warn(key + " '" + sd.getValue() + "' is not a long integer."); | |
74 } | |
75 return null; | |
76 } | |
77 | |
78 protected Integer getInteger(String key) { | |
79 StateData sd = artifact.getData(key); | |
80 if (sd == null) { | |
81 log.warn("missing '" + key + "' value"); | |
82 return null; | |
83 } | |
84 try { | |
85 return Integer.valueOf((String)sd.getValue()); | |
86 } | |
87 catch (NumberFormatException nfe) { | |
88 log.warn(key + " '" + sd.getValue() + "' is not a integer."); | |
89 } | |
90 return null; | |
91 } | |
92 | |
93 protected int [] getIntArray(String key) { | |
94 StateData sd = artifact.getData(key); | |
95 if (sd == null) { | |
96 log.warn("missing '" + key +"' value"); | |
97 return null; | |
98 } | |
99 return FLYSUtils.intArrayFromString((String)sd.getValue()); | |
100 } | |
101 | |
102 protected DateRange [] getDateRange(String key) { | |
103 | |
104 StateData sd = artifact.getData(key); | |
105 | |
106 if (sd == null) { | |
107 log.warn("missing '" + key + "'"); | |
108 return null; | |
109 } | |
110 | |
111 String data = (String)sd.getValue(); | |
112 String[] pairs = data.split("\\s*;\\s*"); | |
113 | |
114 ArrayList<DateRange> aPs = new ArrayList<DateRange>(pairs.length); | |
115 | |
116 for (int i = 0; i < pairs.length; i++) { | |
117 String[] fromTo = pairs[i].split("\\s*,\\s*"); | |
118 if (fromTo.length >= 2) { | |
119 try { | |
120 Date from = new Date(Long.parseLong(fromTo[0])); | |
121 Date to = new Date(Long.parseLong(fromTo[1])); | |
122 DateRange aP = new DateRange(from, to); | |
123 if (!aPs.contains(aP)) { | |
124 aPs.add(aP); | |
125 } | |
126 } | |
127 catch (NumberFormatException nfe) { | |
128 log.warn(key + " contains no long values.", nfe); | |
129 } | |
130 } | |
131 } | |
132 | |
133 DateRange [] result = aPs.toArray(new DateRange[aPs.size()]); | |
134 | |
135 if (log.isDebugEnabled()) { | |
136 for (int i = 0; i < result.length; ++i) { | |
137 DateRange ap = result[i]; | |
138 log.debug("period " + | |
139 ap.getFrom() + " - " + ap.getTo()); | |
140 } | |
141 } | |
142 | |
143 return result; | |
144 } | |
145 | |
146 protected Boolean getBoolean(String key) { | |
147 StateData sd = artifact.getData(key); | |
148 if (sd == null) { | |
149 log.warn("missing '" + key + "' value"); | |
150 return null; | |
151 } | |
152 return Boolean.valueOf((String)sd.getValue()); | |
153 } | |
154 | |
155 protected double [] getDoubleArray(String key) { | |
156 StateData sd = artifact.getData(key); | |
157 if (sd == null) { | |
158 log.warn("missing '" + key + "'"); | |
159 return null; | |
160 } | |
161 String [] parts = ((String)sd.getValue()).split("[\\s;]+"); | |
162 TDoubleArrayList list = new TDoubleArrayList(parts.length); | |
163 for (String part: parts) { | |
164 try { | |
165 list.add(Double.parseDouble(part)); | |
166 } | |
167 catch (NumberFormatException nfe) { | |
168 log.warn("'" + part + "' is not numeric."); | |
169 } | |
170 } | |
171 return list.toNativeArray(); | |
172 } | |
173 } | |
174 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |