-
Notifications
You must be signed in to change notification settings - Fork 5
/
CharacteristicPoint.java
137 lines (121 loc) · 4.28 KB
/
CharacteristicPoint.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
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.models.input.system.characteristic;
import edu.ie3.datamodel.exceptions.ParsingException;
import java.io.Serializable;
import java.util.Locale;
import java.util.Objects;
import javax.measure.Quantity;
import javax.measure.Unit;
import tech.units.indriya.ComparableQuantity;
import tech.units.indriya.quantity.Quantities;
/** Class to describe one point of a given {@link CharacteristicInput} */
public class CharacteristicPoint<A extends Quantity<A>, O extends Quantity<O>>
implements Comparable<CharacteristicPoint<A, O>>, Serializable {
public static final String REQUIRED_FORMAT = "(%d,%d)";
private final ComparableQuantity<A> x;
private final ComparableQuantity<O> y;
/**
* Build a new point
*
* @param x Value on the abscissa
* @param y Value on the ordinate
*/
public CharacteristicPoint(ComparableQuantity<A> x, ComparableQuantity<O> y) {
this.x = x;
this.y = y;
}
/**
* Builds a point from a given input string. The string has to be of format '(%d,%d)'. Spaces are
* tolerated
*
* @param input Input string to parse
* @param abscissaUnit Unit to use on the abscissa
* @param ordinateUnit Unit to use on the ordinate
* @throws ParsingException If the input is malformed
*/
public CharacteristicPoint(String input, Unit<A> abscissaUnit, Unit<O> ordinateUnit)
throws ParsingException {
String trimmed = input.trim();
if (!trimmed.startsWith("(") || !trimmed.endsWith(")"))
throw new ParsingException(buildExceptionMessage(input));
String[] entries = trimmed.replaceAll("^\\(|\\)$", "").split(",");
if (entries.length != 2) throw new ParsingException(buildExceptionMessage(input));
try {
this.x = Quantities.getQuantity(Double.parseDouble(entries[0]), abscissaUnit);
} catch (NumberFormatException nfe) {
throw new ParsingException(
buildExceptionMessage(input, "Abscissa value cannot be parsed to double."), nfe);
}
try {
this.y = Quantities.getQuantity(Double.parseDouble(entries[1]), ordinateUnit);
} catch (NumberFormatException nfe) {
throw new ParsingException(
buildExceptionMessage(input, "Abscissa value cannot be parsed to double."), nfe);
}
}
/**
* Builds a message for an exception to throw.
*
* @param input Malformed input string
*/
private static String buildExceptionMessage(String input) {
return buildExceptionMessage(
input, "It doesn't comply with the required format '" + REQUIRED_FORMAT + "'.");
}
/**
* Builds a custom message for an exception to throw.
*
* @param input Malformed input string
* @param message Additional, custom message
*/
private static String buildExceptionMessage(String input, String message) {
return "Cannot parse " + input + " to CharacteristicCoordinate. " + message;
}
/** @return the position on the abscissa */
public ComparableQuantity<A> getX() {
return x;
}
/** @return the position on the ordinate */
public ComparableQuantity<O> getY() {
return y;
}
/**
* De-serializes the given point to a string
*
* @return The de-serialized point
*/
public String serialize() {
return String.format(
Locale.ENGLISH, "(%s,%s)", x.getValue().doubleValue(), y.getValue().doubleValue());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CharacteristicPoint<?, ?> that)) return false;
return Objects.equals(x, that.x) && Objects.equals(y, that.y);
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public String toString() {
return "CharacteristicCoordinate{" + "x=" + x + ", y=" + y + '}';
}
/**
* Compares this instance against another point. They are compared on the abscissa first. If they
* are on the same location there, the ordinate is taken into account.
*
* @param b The other point
* @return The comparision result
*/
@Override
public int compareTo(CharacteristicPoint<A, O> b) {
int abscissaCompare = x.compareTo(b.getX());
return abscissaCompare != 0 ? abscissaCompare : y.compareTo(b.getY());
}
}