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.MassUnit;
7   import org.djunits.value.StorageType;
8   import org.djunits.value.ValueException;
9   import org.djunits.value.vdouble.scalar.Mass;
10  
11  /**
12   * Immutable Double MassVector, a vector of values with a MassUnit.
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 MassVector extends AbstractDoubleVectorRel<MassUnit, MassVector, MutableMassVector, Mass>
23  {
24      /** */
25      private static final long serialVersionUID = 20151109L;
26  
27      /**
28       * Construct a new Relative Immutable Double MassVector.
29       * @param values double[]; the values of the entries in the new Relative Immutable Double MassVector
30       * @param unit U; the unit of the new Relative Immutable Double MassVector
31       * @param storageType the data type to use (e.g., DENSE or SPARSE)
32       * @throws ValueException when values is null
33       */
34      public MassVector(final double[] values, final MassUnit unit, final StorageType storageType) throws ValueException
35      {
36          super(values, unit, storageType);
37      }
38  
39      /**
40       * Construct a new Relative Immutable Double MassVector.
41       * @param values List; the values of the entries in the new Relative Immutable Double MassVector
42       * @param unit U; the unit of the new Relative Immutable Double MassVector
43       * @param storageType the data type to use (e.g., DENSE or SPARSE)
44       * @throws ValueException when values is null
45       */
46      public MassVector(final List<Double> values, final MassUnit unit, final StorageType storageType) throws ValueException
47      {
48          super(values, unit, storageType);
49      }
50  
51      /**
52       * Construct a new Relative Immutable Double MassVector.
53       * @param values DoubleScalar.Rel&lt;U&gt;[]; the values of the entries in the new Relative Immutable Double MassVector
54       * @param storageType the data type to use (e.g., DENSE or SPARSE)
55       * @throws ValueException when values has zero entries
56       */
57      public MassVector(final Mass[] values, final StorageType storageType) throws ValueException
58      {
59          super(values, storageType);
60      }
61  
62      /**
63       * Construct a new Relative Immutable Double MassVector.
64       * @param values List; the values of the entries in the new Relative Immutable Double MassVector
65       * @param storageType the data type to use (e.g., DENSE or SPARSE)
66       * @throws ValueException when values has zero entries
67       */
68      public MassVector(final List<Mass> values, final StorageType storageType) throws ValueException
69      {
70          super(values, storageType);
71      }
72  
73      /**
74       * Construct a new Relative Immutable Double MassVector.
75       * @param values DoubleScalar.Rel&lt;U&gt;[]; the values of the entries in the new Relative Sparse Mutable Double MassVector
76       * @param length the size of the vector
77       * @param storageType the data type to use (e.g., DENSE or SPARSE)
78       * @throws ValueException when values has zero entries
79       */
80      public MassVector(final SortedMap<Integer, Mass> values, final int length, final StorageType storageType)
81              throws ValueException
82      {
83          super(values, length, storageType);
84      }
85  
86      /**
87       * Construct a new Relative Immutable Double MassVector.
88       * @param values Map; the map of indexes to values of the Relative Sparse Mutable Double MassVector
89       * @param unit U; the unit of the new Relative Sparse Mutable Double MassVector
90       * @param length the size of the vector
91       * @param storageType the data type to use (e.g., DENSE or SPARSE)
92       * @throws ValueException when values is null
93       */
94      public MassVector(final SortedMap<Integer, Double> values, final MassUnit unit, final int length,
95              final StorageType storageType) throws ValueException
96      {
97          super(values, unit, length, storageType);
98      }
99  
100     /**
101      * @param data an internal data object
102      * @param unit the unit
103      */
104     MassVector(final DoubleVectorData data, final MassUnit unit)
105     {
106         super(data, unit);
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     protected final MassVector instantiateType(final DoubleVectorData dvd, final MassUnit unit)
112     {
113         return new MassVector(dvd, unit);
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     protected final MutableMassVector instantiateMutableType(final DoubleVectorData dvd, final MassUnit unit)
119     {
120         return new MutableMassVector(dvd, unit);
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     protected final Mass instantiateScalar(final double value, final MassUnit unit)
126     {
127         return new Mass(value, unit);
128     }
129 
130     /** {@inheritDoc} */
131     @Override
132     public final MassVector toDense()
133     {
134         return this.data.isDense() ? (MassVector) this : instantiateType(this.data.toDense(), getUnit());
135     }
136 
137     /** {@inheritDoc} */
138     @Override
139     public final MassVector toSparse()
140     {
141         return this.data.isSparse() ? (MassVector) this : instantiateType(this.data.toSparse(), getUnit());
142     }
143 
144     /**
145      * Return an array of Mass Scalars from this vector.
146      * @return Mass[]; an array of Mass Scalars from this vector
147      * @throws RuntimeException wrapping a ValueException on error getting one of the values
148      */
149     public Mass[] toArray()
150     {
151         Mass[] array = new Mass[size()];
152         for (int i = 0; i < size(); i++)
153         {
154             try
155             {
156                 array[i] = get(i);
157             }
158             catch (ValueException exception)
159             {
160                 throw new RuntimeException(exception);
161             }
162         }
163         return array;
164     }
165 
166 }