1 package org.djunits.value.vfloat.vector;
2
3 import org.djunits.unit.Unit;
4 import org.djunits.value.Absolute;
5 import org.djunits.value.AbstractValue;
6 import org.djunits.value.Mutable;
7 import org.djunits.value.StorageType;
8 import org.djunits.value.ValueException;
9 import org.djunits.value.ValueUtil;
10 import org.djunits.value.formatter.Format;
11 import org.djunits.value.vfloat.scalar.AbstractFloatScalar;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public abstract class AbstractFloatVector<U extends Unit<U>, T extends AbstractFloatVector<U, T>> extends AbstractValue<U>
28 implements FloatVectorInterface<U>
29 {
30
31 private static final long serialVersionUID = 20151015L;
32
33
34 @SuppressWarnings("checkstyle:visibilitymodifier")
35 protected FloatVectorData data;
36
37
38
39
40
41
42 AbstractFloatVector(final U unit, final FloatVectorData data)
43 {
44 super(unit);
45 this.data = data;
46 }
47
48
49
50
51 protected final FloatVectorData getData()
52 {
53 return this.data;
54 }
55
56
57
58
59
60 public final StorageType getStorageType()
61 {
62 return this.data.getStorageType();
63 }
64
65
66
67
68
69
70
71 public abstract AbstractFloatScalar<U, ?> get(int index) throws ValueException;
72
73
74 @Override
75 public final float[] getValuesSI()
76 {
77 return this.data.getDenseVectorSI();
78 }
79
80
81 @Override
82 public final float[] getValuesInUnit()
83 {
84 return getValuesInUnit(getUnit());
85 }
86
87
88 @Override
89 public final float[] getValuesInUnit(final U targetUnit)
90 {
91 float[] values = getValuesSI();
92 for (int i = values.length; --i >= 0;)
93 {
94 values[i] = (float) ValueUtil.expressAsUnit(values[i], targetUnit);
95 }
96 return values;
97 }
98
99
100 @Override
101 public final int size()
102 {
103 return this.data.size();
104 }
105
106
107
108
109
110
111 protected final void checkIndex(final int index) throws ValueException
112 {
113 if (index < 0 || index >= size())
114 {
115 throw new ValueException("index out of range (valid range is 0.." + (size() - 1) + ", got " + index + ")");
116 }
117 }
118
119
120 @Override
121 public final float getSI(final int index) throws ValueException
122 {
123 checkIndex(index);
124 return this.data.getSI(index);
125 }
126
127
128 @Override
129 public final float getInUnit(final int index) throws ValueException
130 {
131 return (float) expressAsSpecifiedUnit(getSI(index));
132 }
133
134
135 @Override
136 public final float getInUnit(final int index, final U targetUnit) throws ValueException
137 {
138 return (float) ValueUtil.expressAsUnit(getSI(index), targetUnit);
139 }
140
141
142 @Override
143 public final float zSum()
144 {
145 return this.data.zSum();
146 }
147
148
149 @Override
150 public final int cardinality()
151 {
152 return this.data.cardinality();
153 }
154
155
156 @Override
157 public final String toString()
158 {
159 return toString(getUnit(), false, true);
160 }
161
162
163 @Override
164 public final String toString(final U displayUnit)
165 {
166 return toString(displayUnit, false, true);
167 }
168
169
170 @Override
171 public final String toString(final boolean verbose, final boolean withUnit)
172 {
173 return toString(getUnit(), verbose, withUnit);
174 }
175
176
177 @Override
178 public final String toString(final U displayUnit, final boolean verbose, final boolean withUnit)
179 {
180 StringBuffer buf = new StringBuffer();
181 if (verbose)
182 {
183 String ar = this instanceof Absolute ? "Abs " : "Rel ";
184 String ds = this.data.isDense() ? "Dense " : this.data.isSparse() ? "Sparse " : "?????? ";
185 if (this instanceof Mutable)
186 {
187 buf.append("Mutable " + ar + ds);
188 }
189 else
190 {
191 buf.append("Immutable " + ar + ds);
192 }
193 }
194 buf.append("[");
195 for (int i = 0; i < size(); i++)
196 {
197 try
198 {
199 float d = (float) ValueUtil.expressAsUnit(getSI(i), displayUnit);
200 buf.append(" " + Format.format(d));
201 }
202 catch (ValueException ve)
203 {
204 buf.append(" " + "********************".substring(0, Format.DEFAULTSIZE));
205 }
206 }
207 buf.append("]");
208 if (withUnit)
209 {
210 buf.append(" " + displayUnit.getAbbreviation());
211 }
212 return buf.toString();
213 }
214
215
216
217
218
219
220 protected final void checkSize(final AbstractFloatVector<U, ?> other) throws ValueException
221 {
222 if (null == other)
223 {
224 throw new ValueException("other is null");
225 }
226 if (size() != other.size())
227 {
228 throw new ValueException("The vectors have different sizes: " + size() + " != " + other.size());
229 }
230 }
231
232
233
234
235
236
237 protected final void checkSize(final float[] other) throws ValueException
238 {
239 if (size() != other.length)
240 {
241 throw new ValueException("The vector and the array have different sizes: " + size() + " != " + other.length);
242 }
243 }
244
245
246 @Override
247 @SuppressWarnings("checkstyle:designforextension")
248 public int hashCode()
249 {
250 final int prime = 31;
251 int result = getUnit().getStandardUnit().hashCode();
252 result = prime * result + ((this.data == null) ? 0 : this.data.hashCode());
253 return result;
254 }
255
256
257 @Override
258 @SuppressWarnings({ "checkstyle:designforextension", "checkstyle:needbraces", "unchecked" })
259 public boolean equals(final Object obj)
260 {
261 if (this == obj)
262 return true;
263 if (obj == null)
264 return false;
265 if (getClass() != obj.getClass())
266 return false;
267 AbstractFloatVector<U, T> other = (AbstractFloatVector<U, T>) obj;
268 if (!getUnit().getStandardUnit().equals(other.getUnit().getStandardUnit()))
269 return false;
270 if (this.data == null)
271 {
272 if (other.data != null)
273 return false;
274 }
275 else if (!this.data.equals(other.data))
276 return false;
277 return true;
278 }
279
280 }