View Javadoc
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   * The most basic abstract class for the DoubleVector.
14   * <p>
15   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
17   * </p>
18   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
19   * initial version Oct 16, 2016 <br>
20   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
23   * @param <U> the unit
24   * @param <T> the type
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      /** The stored data as an object, can be sparse or dense. */
33      @SuppressWarnings("checkstyle:visibilitymodifier")
34      protected DoubleVectorData data;
35  
36      /**
37       * Construct a new DoubleVector.
38       * @param unit the unit
39       * @param data an internal data object
40       */
41      AbstractDoubleVector(final U unit, final DoubleVectorData data)
42      {
43          super(unit);
44          this.data = data;
45      }
46  
47      /**
48       * @return the internal data -- can only be used within package and by subclasses.
49       */
50      protected final DoubleVectorData getData()
51      {
52          return this.data;
53      }
54  
55      /**
56       * Return the StorageType (DENSE, SPARSE, etc.) for the stored Vector.
57       * @return the StorageType (DENSE, SPARSE, etc.) for the stored Vector
58       */
59      public final StorageType getStorageType()
60      {
61          return this.data.getStorageType();
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public final double[] getValuesSI()
67      {
68          return this.data.getDenseVectorSI();
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public final double[] getValuesInUnit()
74      {
75          return getValuesInUnit(getUnit());
76      }
77  
78      /** {@inheritDoc} */
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      /** {@inheritDoc} */
91      @Override
92      public final int size()
93      {
94          return this.data.size();
95      }
96  
97      /**
98       * Check that a provided index is valid.
99       * @param index int; the value to check
100      * @throws ValueException when index is invalid
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
119     @Override
120     public final double getInUnit(final int index) throws ValueException
121     {
122         return expressAsSpecifiedUnit(getSI(index));
123     }
124 
125     /** {@inheritDoc} */
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     /** {@inheritDoc} */
133     @Override
134     public final double zSum()
135     {
136         return this.data.zSum();
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public final int cardinality()
142     {
143         return this.data.cardinality();
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     public final String toString()
149     {
150         return toString(getUnit(), false, true);
151     }
152 
153     /** {@inheritDoc} */
154     @Override
155     public final String toString(final U displayUnit)
156     {
157         return toString(displayUnit, false, true);
158     }
159 
160     /** {@inheritDoc} */
161     @Override
162     public final String toString(final boolean verbose, final boolean withUnit)
163     {
164         return toString(getUnit(), verbose, withUnit);
165     }
166 
167     /** {@inheritDoc} */
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      * Centralized size equality check.
208      * @param other DoubleVector&lt;?&gt;; other DoubleVector
209      * @throws ValueException when other is null, or vectors have unequal size
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      * Centralized size equality check.
225      * @param other double[]; array of double
226      * @throws ValueException when vectors have unequal size
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
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 }