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