View Javadoc
1   package org.djunits.value.vdouble.vector;
2   
3   import java.util.List;
4   import java.util.SortedMap;
5   
6   import org.djunits.unit.DensityUnit;
7   import org.djunits.value.StorageType;
8   import org.djunits.value.ValueException;
9   import org.djunits.value.vdouble.scalar.Density;
10  
11  /**
12   * Immutable Double DensityVector, a vector of values with a DensityUnit.
13   * <p>
14   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16   * </p>
17   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
18   * initial version Oct 9, 2015 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   */
22  public class DensityVector extends AbstractDoubleVectorRel<DensityUnit, DensityVector, MutableDensityVector, Density>
23  {
24      /** */
25      private static final long serialVersionUID = 20151109L;
26  
27      /**
28       * Construct a new Relative Immutable Double DensityVector.
29       * @param values double[]; the values of the entries in the new Relative Immutable Double DensityVector
30       * @param unit U; the unit of the new Relative Immutable Double DensityVector
31       * @param storageType the data type to use (e.g., DENSE or SPARSE)
32       * @throws ValueException when values is null
33       */
34      public DensityVector(final double[] values, final DensityUnit unit, final StorageType storageType) throws ValueException
35      {
36          super(values, unit, storageType);
37      }
38  
39      /**
40       * Construct a new Relative Immutable Double DensityVector.
41       * @param values List; the values of the entries in the new Relative Immutable Double DensityVector
42       * @param unit U; the unit of the new Relative Immutable Double DensityVector
43       * @param storageType the data type to use (e.g., DENSE or SPARSE)
44       * @throws ValueException when values is null
45       */
46      public DensityVector(final List<Double> values, final DensityUnit unit, final StorageType storageType) throws ValueException
47      {
48          super(values, unit, storageType);
49      }
50  
51      /**
52       * Construct a new Relative Immutable Double DensityVector.
53       * @param values DoubleScalar.Rel&lt;U&gt;[]; the values of the entries in the new Relative Immutable Double DensityVector
54       * @param storageType the data type to use (e.g., DENSE or SPARSE)
55       * @throws ValueException when values has zero entries
56       */
57      public DensityVector(final Density[] values, final StorageType storageType) throws ValueException
58      {
59          super(values, storageType);
60      }
61  
62      /**
63       * Construct a new Relative Immutable Double DensityVector.
64       * @param values List; the values of the entries in the new Relative Immutable Double DensityVector
65       * @param storageType the data type to use (e.g., DENSE or SPARSE)
66       * @throws ValueException when values has zero entries
67       */
68      public DensityVector(final List<Density> values, final StorageType storageType) throws ValueException
69      {
70          super(values, storageType);
71      }
72  
73      /**
74       * Construct a new Relative Immutable Double DensityVector.
75       * @param values DoubleScalar.Rel&lt;U&gt;[]; the values of the entries in the new Relative Sparse Mutable Double
76       *            DensityVector
77       * @param length the size of the vector
78       * @param storageType the data type to use (e.g., DENSE or SPARSE)
79       * @throws ValueException when values has zero entries
80       */
81      public DensityVector(final SortedMap<Integer, Density> values, final int length, final StorageType storageType)
82              throws ValueException
83      {
84          super(values, length, storageType);
85      }
86  
87      /**
88       * Construct a new Relative Immutable Double DensityVector.
89       * @param values Map; the map of indexes to values of the Relative Sparse Mutable Double DensityVector
90       * @param unit U; the unit of the new Relative Sparse Mutable Double DensityVector
91       * @param length the size of the vector
92       * @param storageType the data type to use (e.g., DENSE or SPARSE)
93       * @throws ValueException when values is null
94       */
95      public DensityVector(final SortedMap<Integer, Double> values, final DensityUnit unit, final int length,
96              final StorageType storageType) throws ValueException
97      {
98          super(values, unit, length, storageType);
99      }
100 
101     /**
102      * @param data an internal data object
103      * @param unit the unit
104      */
105     DensityVector(final DoubleVectorData data, final DensityUnit unit)
106     {
107         super(data, unit);
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     protected final DensityVector instantiateType(final DoubleVectorData dvd, final DensityUnit unit)
113     {
114         return new DensityVector(dvd, unit);
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     protected final MutableDensityVector instantiateMutableType(final DoubleVectorData dvd, final DensityUnit unit)
120     {
121         return new MutableDensityVector(dvd, unit);
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     protected final Density instantiateScalar(final double value, final DensityUnit unit)
127     {
128         return new Density(value, unit);
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     public final DensityVector toDense()
134     {
135         return this.data.isDense() ? (DensityVector) this : instantiateType(this.data.toDense(), getUnit());
136     }
137 
138     /** {@inheritDoc} */
139     @Override
140     public final DensityVector toSparse()
141     {
142         return this.data.isSparse() ? (DensityVector) this : instantiateType(this.data.toSparse(), getUnit());
143     }
144 
145     /**
146      * Return an array of Density Scalars from this vector.
147      * @return Density[]; an array of Density Scalars from this vector
148      * @throws RuntimeException wrapping a ValueException on error getting one of the values
149      */
150     public Density[] toArray()
151     {
152         Density[] array = new Density[size()];
153         for (int i = 0; i < size(); i++)
154         {
155             try
156             {
157                 array[i] = get(i);
158             }
159             catch (ValueException exception)
160             {
161                 throw new RuntimeException(exception);
162             }
163         }
164         return array;
165     }
166 
167 }