1 package org.opentrafficsim.core.egtf;
2
3 /**
4 * Defines a quantity that data sources can provide, such as speed, flow, etc.
5 * <p>
6 * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
7 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
8 * <p>
9 * @version $Revision$, $LastChangedDate$, by $Author$, initial version 24 okt. 2018 <br>
10 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
11 * @param <T> data type
12 * @param <K> grid output format
13 */
14 public class Quantity<T extends Number, K>
15 {
16 /** Standard quantity for speed. */
17 public static final Quantity<Double, double[][]> SPEED_SI = new Quantity<>("Speed", true, Converter.SI);
18
19 /** Standard quantity for flow. */
20 public static final Quantity<Double, double[][]> FLOW_SI = new Quantity<>("Flow", Converter.SI);
21
22 /** Standard quantity for density. */
23 public static final Quantity<Double, double[][]> DENSITY_SI = new Quantity<>("Density", Converter.SI);
24
25 /** Name. */
26 private final String name;
27
28 /** Whether this quantity is speed. */
29 private final boolean speed;
30
31 /** Converter for output format. */
32 private final Converter<K> converter;
33
34 /**
35 * Constructor.
36 * @param name String; name
37 * @param converter Converter<K>; converter for output format
38 */
39 public Quantity(final String name, final Converter<K> converter)
40 {
41 this(name, false, converter);
42 }
43
44 /**
45 * Constructor. Protected so only the default SPEED_SI quantity is speed.
46 * @param name String; name
47 * @param speed boolean; whether this quantity is speed
48 * @param converter Converter<K>; converter for output format
49 */
50 protected Quantity(final String name, final boolean speed, final Converter<K> converter)
51 {
52 this.name = name;
53 this.speed = speed;
54 this.converter = converter;
55 }
56
57 /**
58 * Returns a quantity with {@code double[][]} containing SI values as output format.
59 * @param name String; name
60 * @return quantity with {@code double[][]} containing SI values as output format
61 */
62 public static Quantity<?, double[][]> si(final String name)
63 {
64 return new SI<>(name);
65 }
66
67 /**
68 * Returns the name.
69 * @return String; name
70 */
71 public final String getName()
72 {
73 return this.name;
74 }
75
76 /**
77 * Returns whether this quantity is speed.
78 * @return boolean; whether this quantity is speed
79 */
80 final boolean isSpeed()
81 {
82 return this.speed;
83 }
84
85 /**
86 * Converts the filtered data to an output format.
87 * @param data double[][]; filtered data
88 * @return K; output data
89 */
90 final K convert(final double[][] data)
91 {
92 return this.converter.convert(data);
93 }
94
95 /** {@inheritDoc} */
96 @Override
97 public int hashCode()
98 {
99 final int prime = 31;
100 int result = 1;
101 result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
102 return result;
103 }
104
105 /** {@inheritDoc} */
106 @Override
107 public boolean equals(final Object obj)
108 {
109 if (this == obj)
110 {
111 return true;
112 }
113 if (obj == null)
114 {
115 return false;
116 }
117 if (getClass() != obj.getClass())
118 {
119 return false;
120 }
121 Quantity<?, ?> other = (Quantity<?, ?>) obj;
122 if (this.name == null)
123 {
124 if (other.name != null)
125 {
126 return false;
127 }
128 }
129 else if (!this.name.equals(other.name))
130 {
131 return false;
132 }
133 return true;
134 }
135
136 /** {@inheritDoc} */
137 @Override
138 public String toString()
139 {
140 return "Quantity [name=" + this.name + "]";
141 }
142
143 /**
144 * Class to return in {@code double[][]} output format.
145 * @param <T> data type
146 */
147 private static class SI<T extends Number> extends Quantity<T, double[][]>
148 {
149
150 /**
151 * Constructor.
152 * @param name String; String name
153 */
154 SI(final String name)
155 {
156 super(name, Converter.SI);
157 }
158
159 /** {@inheritDoc} */
160 @Override
161 public String toString()
162 {
163 return "SI []";
164 }
165
166 }
167
168 }