3
3
import java .util .ArrayList ;
4
4
import java .util .Collection ;
5
5
import java .util .List ;
6
+ import java .util .Objects ;
6
7
7
8
/**
8
9
* This class defines the response for a M-to-N requests.
@@ -13,6 +14,9 @@ public class MatrixResponse {
13
14
14
15
private String debugInfo = "" ;
15
16
private final List <Throwable > errors = new ArrayList <>(4 );
17
+ private final List <PointPair > disconnectedPoints = new ArrayList <>(0 );
18
+ private final List <Integer > invalidFromPoints = new ArrayList <>(0 );
19
+ private final List <Integer > invalidToPoints = new ArrayList <>(0 );
16
20
private long [][] times = new long [0 ][];
17
21
private int [][] distances = new int [0 ][];
18
22
private double [][] weights = new double [0 ][];
@@ -45,7 +49,7 @@ public MatrixResponse(int fromCap, int toCap, boolean withTimes, boolean withDis
45
49
throw new IllegalArgumentException ("Please specify times, distances or weights that should be calculated by the matrix" );
46
50
}
47
51
48
- public void setFromRow (int row , long timeRow [] , int distanceRow [] , double weightRow [] ) {
52
+ public void setFromRow (int row , long [] timeRow , int [] distanceRow , double [] weightRow ) {
49
53
if (times .length > 0 ) {
50
54
check (timeRow .length , toCount , "to times" );
51
55
times [row ] = timeRow ;
@@ -68,7 +72,7 @@ private void check(int currentLength, int expectedLength, String times) {
68
72
"Expected " + expectedLength + " was: " + currentLength + ". Matrix: " + fromCount + "x" + toCount );
69
73
}
70
74
71
- public void setTimeRow (int row , long timeRow [] ) {
75
+ public void setTimeRow (int row , long [] timeRow ) {
72
76
if (times .length > 0 ) {
73
77
check (timeRow .length , toCount , "to times" );
74
78
times [row ] = timeRow ;
@@ -77,7 +81,7 @@ public void setTimeRow(int row, long timeRow[]) {
77
81
}
78
82
}
79
83
80
- public void setDistanceRow (int row , int distanceRow [] ) {
84
+ public void setDistanceRow (int row , int [] distanceRow ) {
81
85
if (distances .length > 0 ) {
82
86
check (distanceRow .length , toCount , "to distances" );
83
87
distances [row ] = distanceRow ;
@@ -86,7 +90,7 @@ public void setDistanceRow(int row, int distanceRow[]) {
86
90
}
87
91
}
88
92
89
- public void setWeightRow (int row , double weightRow [] ) {
93
+ public void setWeightRow (int row , double [] weightRow ) {
90
94
if (weights .length > 0 ) {
91
95
check (weightRow .length , toCount , "to weights" );
92
96
weights [row ] = weightRow ;
@@ -95,12 +99,20 @@ public void setWeightRow(int row, double weightRow[]) {
95
99
}
96
100
}
97
101
102
+ public boolean isConnected (int from , int to ) {
103
+ if (hasErrors ()) {
104
+ return false ;
105
+ }
106
+ return getWeight (from , to ) < Double .MAX_VALUE ;
107
+ }
108
+
98
109
/**
99
- * Returns the time for the specific entry (from -> to) in milliseconds.
110
+ * Returns the time for the specific entry (from -> to) in milliseconds or {@link Long#MAX_VALUE} in case
111
+ * no connection was found (and {@link GHMRequest#setFailFast(boolean)} was set to true).
100
112
*/
101
113
public long getTime (int from , int to ) {
102
114
if (hasErrors ()) {
103
- throw new IllegalStateException ("Cannot return time (" + from + "," + to + ") if errors occured " + getErrors ());
115
+ throw new IllegalStateException ("Cannot return time (" + from + "," + to + ") if errors occurred " + getErrors ());
104
116
}
105
117
106
118
if (from >= times .length ) {
@@ -112,7 +124,8 @@ public long getTime(int from, int to) {
112
124
}
113
125
114
126
/**
115
- * Returns the distance for the specific entry (from -> to) in meter.
127
+ * Returns the distance for the specific entry (from -> to) in meter or {@link Double#MAX_VALUE} in case
128
+ * no connection was found (and {@link GHMRequest#setFailFast(boolean)} was set to true).
116
129
*/
117
130
public double getDistance (int from , int to ) {
118
131
if (hasErrors ()) {
@@ -124,12 +137,13 @@ public double getDistance(int from, int to) {
124
137
} else if (to >= distances [from ].length ) {
125
138
throw new IllegalStateException ("Cannot get 'to' " + to + " from distances with size " + distances [from ].length );
126
139
}
127
- return distances [from ][to ];
140
+ return distances [from ][to ] == Integer . MAX_VALUE ? Double . MAX_VALUE : distances [ from ][ to ] ;
128
141
}
129
142
130
143
/**
131
- * Returns the weight for the specific entry (from -> to) in arbitrary units
132
- * ('costs').
144
+ * Returns the weight for the specific entry (from -> to) in arbitrary units ('costs'), or
145
+ * {@link Double#MAX_VALUE} in case no connection was found (and {@link GHMRequest#setFailFast(boolean)} was set
146
+ * to true).
133
147
*/
134
148
public double getWeight (int from , int to ) {
135
149
if (hasErrors ()) {
@@ -176,6 +190,44 @@ public MatrixResponse addErrors(Collection<Throwable> errorList) {
176
190
return this ;
177
191
}
178
192
193
+ /**
194
+ * @return true if there are invalid or disconnected points (which both do not yield an error in case we do not fail fast).
195
+ * @see GHMRequest#setFailFast(boolean)
196
+ */
197
+ public boolean hasProblems () {
198
+ return !disconnectedPoints .isEmpty () || !invalidFromPoints .isEmpty () || !invalidToPoints .isEmpty ();
199
+ }
200
+
201
+ public MatrixResponse setDisconnectedPoints (List <PointPair > disconnectedPoints ) {
202
+ this .disconnectedPoints .clear ();
203
+ this .disconnectedPoints .addAll (disconnectedPoints );
204
+ return this ;
205
+ }
206
+
207
+ public List <PointPair > getDisconnectedPoints () {
208
+ return disconnectedPoints ;
209
+ }
210
+
211
+ public MatrixResponse setInvalidFromPoints (List <Integer > invalidFromPoints ) {
212
+ this .invalidFromPoints .clear ();
213
+ this .invalidFromPoints .addAll (invalidFromPoints );
214
+ return this ;
215
+ }
216
+
217
+ public MatrixResponse setInvalidToPoints (List <Integer > invalidToPoints ) {
218
+ this .invalidToPoints .clear ();
219
+ this .invalidToPoints .addAll (invalidToPoints );
220
+ return this ;
221
+ }
222
+
223
+ public List <Integer > getInvalidFromPoints () {
224
+ return invalidFromPoints ;
225
+ }
226
+
227
+ public List <Integer > getInvalidToPoints () {
228
+ return invalidToPoints ;
229
+ }
230
+
179
231
@ Override
180
232
public String toString () {
181
233
String addInfo = "" ;
@@ -188,6 +240,45 @@ public String toString() {
188
240
addInfo += ", distances: " + distances .length + "x" + distances [0 ].length ;
189
241
}
190
242
191
- return "[" + addInfo + "] errors:" + errors .toString ();
243
+ String result = "[" + addInfo + "] errors:" + errors .toString ();
244
+ if (!disconnectedPoints .isEmpty ()) {
245
+ result += ", disconnectedPoints: " + disconnectedPoints .size ();
246
+ }
247
+ if (!invalidFromPoints .isEmpty ()) {
248
+ result += ", invalidFromPoints: " + invalidFromPoints .size ();
249
+ }
250
+ if (!invalidToPoints .isEmpty ()) {
251
+ result += ", invalidToPoints: " + invalidToPoints .size ();
252
+ }
253
+ return result ;
254
+ }
255
+
256
+ public static class PointPair {
257
+ public final int sourceIndex ;
258
+ public final int targetIndex ;
259
+
260
+ public PointPair (int sourceIndex , int targetIndex ) {
261
+ this .sourceIndex = sourceIndex ;
262
+ this .targetIndex = targetIndex ;
263
+ }
264
+
265
+ @ Override
266
+ public boolean equals (Object o ) {
267
+ if (this == o ) return true ;
268
+ if (o == null || getClass () != o .getClass ()) return false ;
269
+ PointPair pointPair = (PointPair ) o ;
270
+ return sourceIndex == pointPair .sourceIndex &&
271
+ targetIndex == pointPair .targetIndex ;
272
+ }
273
+
274
+ @ Override
275
+ public int hashCode () {
276
+ return Objects .hash (sourceIndex , targetIndex );
277
+ }
278
+
279
+ @ Override
280
+ public String toString () {
281
+ return "[" + sourceIndex + ", " + targetIndex + "]" ;
282
+ }
192
283
}
193
284
}
0 commit comments