-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathBaseMeasurementUnit.java
More file actions
115 lines (105 loc) · 5.2 KB
/
BaseMeasurementUnit.java
File metadata and controls
115 lines (105 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* UnitOf v1.0.0.0 - https://github.com/Digidemic/UnitOf
* (c) 2018 DIGIDEMIC, LLC - All Rights Reserved
* UnitOf developed by Adam Steinberg of DIGIDEMIC, LLC
* License: Apache License 2.0
*
* ====
*
* Copyright 2018 DIGIDEMIC, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.digidemic.unitof;
import java.io.Serializable;
/**
* Generic base class for most UnitOf measurement classes.
*/
public class BaseMeasurementUnit<M> implements Serializable {
protected ConversionHelper<M> conversionHelper = new ConversionHelper<>(); //Instantiate class ConversionHelper.java for variables needed to perform conversions.
/**
* Returns value initially passed into the measurement's "from" method.
*/
public final Object getValuePassed() {
return (conversionHelper.isObjDouble) ? conversionHelper.value : conversionHelper.obj; //true returns numeric value passed, false returns Object passed.
}
/**
* Returns the enum constant value representing the "from" method of the measurement used.
*/
public final String getTypeConstantPassed() {
return conversionHelper.typeString;
}
/**
* Stores the needed values to do conversions of the measurement.
* This overload of the method is used in "from" methods in every measurement class other than Anything(), DataType(), and NumericBase().
*
* @param <T> Class context of measurement passed (usually "this" is passed from caller).
* @param <TT> Enum constant value representing the "from" method of the measurement used.
* @param t Class context of measurement passed (usually "this" is passed from caller).
* @param v User passed "from" value (double).
* @param tt Enum constant value representing the "from" method of the measurement used.
* @return class context passed in so variable like "UnitOf.Length len" can be used as the variable type
*/
protected final <T, TT> T store(T t, double v, TT tt) {
conversionHelper = new ConversionHelper(v, tt, getAsString(tt));
return t;
}
/**
* Stores the needed values to do conversions of the measurement.
* This overload of the method is used only in DataType and NumericBase as Objects can be passed as "from" values.
*
* @param <T> Class context of measurement passed (usually "this" is passed from caller).
* @param <TT> Enum constant value representing the "from" method of the measurement used.
* @param t Class context of measurement passed (usually "this" is passed from caller).
* @param v User passed "from" value (Object).
* @param tt Enum constant value representing the "from" method of the measurement used.
* @return class context passed in so variable like "UnitOf.DataType dt" can be used as the variable type
*/
protected final <T, TT> T store(T t, Object v, TT tt) {
conversionHelper = new ConversionHelper(v, tt, getAsString(tt));
return t;
}
/**
* Gets and returns the string value of constant representing the "from" method of the measurement used.
*/
private final <TT> String getAsString(TT tt) {
try {
return tt.toString();
} catch (Exception e) {
}
return "";
}
/**
* Used by every measurement class that converts just numbers (Anything(), DataType(), NumericBase() do not apply here).
* Method performs the full conversion of taking the user defined "from" value and converting it into the user desired "to" value.
*
* @param a Enum constant value of "to" unit. Unit being converted into conversion constant value.
* @param b Enum constant value of "from" unit. Unit starting from conversion constant value.
* @return Finished conversion. "From" converted into "to" value.
*/
protected final double doConvert(double a, double b) {
return doConvert(a, b, true);
}
/**
* Used by every measurement class that converts just numbers (Anything(), DataType(), NumericBase() do not apply here)
* Method performs the full conversion of taking the user defined "from" value and converting it into the user desired "to" value.
*
* @param a Enum constant value of "to" unit. Unit being converted into conversion constant value.
* @param b Enum constant value of "from" unit. Unit starting from conversion constant value.
* @param q Multiply then divide conversion algorithm, false will divide then multiply when converting "to"
* @return Finished conversion. "from" converted into "to" value.
*/
protected final double doConvert(double a, double b, boolean q) {
return Utils.divideOrMultiply(Utils.divideOrMultiply(conversionHelper.value, a, q), b, !q);
}
}