View Javadoc
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   * The most basic abstract class for the FloatVector.
15   * <p>
16   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
18   * </p>
19   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
20   * initial version Oct 16, 2016 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
23   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
24   * @param <U> the unit
25   * @param <T> the type
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      /** The stored data as an object, can be sparse or dense. */
34      @SuppressWarnings("checkstyle:visibilitymodifier")
35      protected FloatVectorData data;
36  
37      /**
38       * Construct a new FloatVector.
39       * @param unit U; the unit
40       * @param data FloatVectorData; an internal data object
41       */
42      AbstractFloatVector(final U unit, final FloatVectorData data)
43      {
44          super(unit);
45          this.data = data;
46      }
47  
48      /**
49       * @return the internal data -- can only be used within package and by subclasses.
50       */
51      protected final FloatVectorData getData()
52      {
53          return this.data;
54      }
55  
56      /**
57       * Return the StorageType (DENSE, SPARSE, etc.) for the stored Vector.
58       * @return the StorageType (DENSE, SPARSE, etc.) for the stored Vector
59       */
60      public final StorageType getStorageType()
61      {
62          return this.data.getStorageType();
63      }
64  
65      /**
66       * Retrieve a value from the vector.
67       * @param index int; the index to retrieve the value at
68       * @return the value as a FloatScalar
69       * @throws ValueException in case index is out of bounds
70       */
71      public abstract AbstractFloatScalar<U, ?> get(int index) throws ValueException;
72  
73      /** {@inheritDoc} */
74      @Override
75      public final float[] getValuesSI()
76      {
77          return this.data.getDenseVectorSI();
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public final float[] getValuesInUnit()
83      {
84          return getValuesInUnit(getUnit());
85      }
86  
87      /** {@inheritDoc} */
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      /** {@inheritDoc} */
100     @Override
101     public final int size()
102     {
103         return this.data.size();
104     }
105 
106     /**
107      * Check that a provided index is valid.
108      * @param index int; the value to check
109      * @throws ValueException when index is invalid
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
128     @Override
129     public final float getInUnit(final int index) throws ValueException
130     {
131         return (float) expressAsSpecifiedUnit(getSI(index));
132     }
133 
134     /** {@inheritDoc} */
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     /** {@inheritDoc} */
142     @Override
143     public final float zSum()
144     {
145         return this.data.zSum();
146     }
147 
148     /** {@inheritDoc} */
149     @Override
150     public final int cardinality()
151     {
152         return this.data.cardinality();
153     }
154 
155     /** {@inheritDoc} */
156     @Override
157     public final String toString()
158     {
159         return toString(getUnit(), false, true);
160     }
161 
162     /** {@inheritDoc} */
163     @Override
164     public final String toString(final U displayUnit)
165     {
166         return toString(displayUnit, false, true);
167     }
168 
169     /** {@inheritDoc} */
170     @Override
171     public final String toString(final boolean verbose, final boolean withUnit)
172     {
173         return toString(getUnit(), verbose, withUnit);
174     }
175 
176     /** {@inheritDoc} */
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      * Centralized size equality check.
217      * @param other AbstractFloatVector&lt;U, ?&gt;; other FloatVector
218      * @throws ValueException when other is null, or vectors have unequal size
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      * Centralized size equality check.
234      * @param other float[]; array of float
235      * @throws ValueException when vectors have unequal size
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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 }