-
Notifications
You must be signed in to change notification settings - Fork 5
/
FactoryData.java
162 lines (146 loc) · 4.73 KB
/
FactoryData.java
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io.factory;
import edu.ie3.datamodel.exceptions.FactoryException;
import java.util.*;
import javax.measure.Quantity;
import javax.measure.Unit;
import tech.units.indriya.ComparableQuantity;
import tech.units.indriya.quantity.Quantities;
public abstract class FactoryData {
private final Map<String, String> fieldsToAttributes;
private final Class<?> targetClass;
protected FactoryData(Map<String, String> fieldsToAttributes, Class<?> targetClass) {
// this does the magic: case-insensitive get/set calls on keys
TreeMap<String, String> insensitiveFieldsToAttributes =
new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
insensitiveFieldsToAttributes.putAll(fieldsToAttributes);
this.fieldsToAttributes = insensitiveFieldsToAttributes;
this.targetClass = targetClass;
}
public Map<String, String> getFieldsToValues() {
return fieldsToAttributes;
}
public Class<?> getTargetClass() {
return targetClass;
}
/**
* Checks whether attribute map contains a value for given key
*
* @param key key to check for
* @return whether attribute map contains given field or not
*/
public boolean containsKey(String key) {
return fieldsToAttributes.containsKey(key);
}
/**
* Returns field value for given field name. Throws {@link FactoryException} if field does not
* exist.
*
* @param field field name
* @return field value
*/
public String getField(String field) {
if (!fieldsToAttributes.containsKey(field))
throw new FactoryException(String.format("Field \"%s\" not found in EntityData", field));
return fieldsToAttributes.get(field);
}
/**
* Returns field value for given field name, or empty Optional if field does not exist.
*
* @param field field name
* @return field value
*/
public Optional<String> getFieldOptional(String field) {
return Optional.ofNullable(fieldsToAttributes.get(field));
}
/**
* Parses and returns a Quantity from field value of given field name. Throws {@link
* FactoryException} if field does not exist or parsing fails.
*
* @param field field name
* @param unit unit of Quantity
* @param <Q> unit type parameter
* @return Quantity of given field with given unit
*/
public <Q extends Quantity<Q>> ComparableQuantity<Q> getQuantity(String field, Unit<Q> unit) {
return Quantities.getQuantity(getDouble(field), unit);
}
/**
* Returns int value for given field name. Throws {@link FactoryException} if field does not exist
* or parsing fails.
*
* @param field field name
* @return int value
*/
public int getInt(String field) {
try {
return Integer.parseInt(getField(field));
} catch (NumberFormatException nfe) {
throw new FactoryException(
String.format(
"Exception while trying to parse field \"%s\" with supposed int value \"%s\"",
field, getField(field)),
nfe);
}
}
/**
* Returns double value for given field name. Throws {@link FactoryException} if field does not
* exist or parsing fails.
*
* @param field field name
* @return double value
*/
public double getDouble(String field) {
try {
return Double.parseDouble(getField(field));
} catch (NumberFormatException nfe) {
throw new FactoryException(
String.format(
"Exception while trying to parse field \"%s\" with supposed double value \"%s\"",
field, getField(field)),
nfe);
}
}
/**
* Parses and returns a UUID from field value of given field name. Throws {@link FactoryException}
* if field does not exist or parsing fails.
*
* @param field field name
* @return UUID
*/
public UUID getUUID(String field) {
try {
return UUID.fromString(getField(field));
} catch (IllegalArgumentException iae) {
throw new FactoryException(
String.format(
"Exception while trying to parse UUID of field \"%s\" with value \"%s\"",
field, getField(field)),
iae);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof FactoryData that)) return false;
return fieldsToAttributes.equals(that.fieldsToAttributes)
&& targetClass.equals(that.targetClass);
}
@Override
public int hashCode() {
return Objects.hash(fieldsToAttributes, targetClass);
}
@Override
public String toString() {
return "FactoryData{"
+ "fieldsToAttributes="
+ fieldsToAttributes
+ ", targetClass="
+ targetClass
+ '}';
}
}