41
41
*/
42
42
public class EncodingManager implements EncodedValueLookup {
43
43
private final LinkedHashMap <String , EncodedValue > encodedValueMap ;
44
+ private final LinkedHashMap <String , EncodedValue > turnEncodedValueMap ;
44
45
private int intsForFlags ;
45
46
private int intsForTurnCostFlags ;
46
47
@@ -49,6 +50,7 @@ public static void putEncodingManagerIntoProperties(EncodingManager encodingMana
49
50
properties .put ("graph.em.ints_for_flags" , encodingManager .intsForFlags );
50
51
properties .put ("graph.em.ints_for_turn_cost_flags" , encodingManager .intsForTurnCostFlags );
51
52
properties .put ("graph.encoded_values" , encodingManager .toEncodedValuesAsString ());
53
+ properties .put ("graph.turn_encoded_values" , encodingManager .toTurnEncodedValuesAsString ());
52
54
}
53
55
54
56
public static EncodingManager fromProperties (StorableProperties properties ) {
@@ -69,7 +71,16 @@ public static EncodingManager fromProperties(StorableProperties properties) {
69
71
throw new IllegalStateException ("Duplicate encoded value name: " + encodedValue .getName () + " in: graph.encoded_values=" + encodedValueStr );
70
72
});
71
73
72
- return new EncodingManager (encodedValues ,
74
+ String turnEncodedValueStr = properties .get ("graph.turn_encoded_values" );
75
+ ArrayNode tevList = deserializeEncodedValueList (turnEncodedValueStr );
76
+ LinkedHashMap <String , EncodedValue > turnEncodedValues = new LinkedHashMap <>();
77
+ tevList .forEach (serializedEV -> {
78
+ EncodedValue encodedValue = EncodedValueSerializer .deserializeEncodedValue (serializedEV .textValue ());
79
+ if (turnEncodedValues .put (encodedValue .getName (), encodedValue ) != null )
80
+ throw new IllegalStateException ("Duplicate turn encoded value name: " + encodedValue .getName () + " in: graph.turn_encoded_values=" + turnEncodedValueStr );
81
+ });
82
+
83
+ return new EncodingManager (encodedValues , turnEncodedValues ,
73
84
getIntegerProperty (properties , "graph.em.ints_for_flags" ),
74
85
getIntegerProperty (properties , "graph.em.ints_for_turn_cost_flags" )
75
86
);
@@ -97,14 +108,17 @@ public static Builder start() {
97
108
return new Builder ();
98
109
}
99
110
100
- public EncodingManager (LinkedHashMap <String , EncodedValue > encodedValueMap , int intsForFlags , int intsForTurnCostFlags ) {
111
+ public EncodingManager (LinkedHashMap <String , EncodedValue > encodedValueMap ,
112
+ LinkedHashMap <String , EncodedValue > turnEncodedValueMap ,
113
+ int intsForFlags , int intsForTurnCostFlags ) {
101
114
this .encodedValueMap = encodedValueMap ;
115
+ this .turnEncodedValueMap = turnEncodedValueMap ;
102
116
this .intsForFlags = intsForFlags ;
103
117
this .intsForTurnCostFlags = intsForTurnCostFlags ;
104
118
}
105
119
106
120
private EncodingManager () {
107
- this (new LinkedHashMap <>(), 0 , 0 );
121
+ this (new LinkedHashMap <>(), new LinkedHashMap <>(), 0 , 0 );
108
122
}
109
123
110
124
public static class Builder {
@@ -128,18 +142,21 @@ public Builder add(EncodedValue encodedValue) {
128
142
checkNotBuiltAlready ();
129
143
if (em .hasEncodedValue (encodedValue .getName ()))
130
144
throw new IllegalArgumentException ("EncodedValue already exists: " + encodedValue .getName ());
145
+ if (em .hasTurnEncodedValue (encodedValue .getName ()))
146
+ throw new IllegalArgumentException ("Already defined as 'turn'-EncodedValue: " + encodedValue .getName ());
131
147
encodedValue .init (edgeConfig );
132
148
em .encodedValueMap .put (encodedValue .getName (), encodedValue );
133
149
return this ;
134
150
}
135
151
136
152
public Builder addTurnCostEncodedValue (EncodedValue turnCostEnc ) {
137
153
checkNotBuiltAlready ();
154
+ if (em .hasTurnEncodedValue (turnCostEnc .getName ()))
155
+ throw new IllegalArgumentException ("Already defined: " + turnCostEnc .getName ());
138
156
if (em .hasEncodedValue (turnCostEnc .getName ()))
139
- throw new IllegalArgumentException ("Already defined: " + turnCostEnc .getName () + ". Please note that " +
140
- "EncodedValues for edges and turn costs are in the same namespace." );
157
+ throw new IllegalArgumentException ("Already defined as EncodedValue: " + turnCostEnc .getName ());
141
158
turnCostEnc .init (turnCostConfig );
142
- em .encodedValueMap .put (turnCostEnc .getName (), turnCostEnc );
159
+ em .turnEncodedValueMap .put (turnCostEnc .getName (), turnCostEnc );
143
160
return this ;
144
161
}
145
162
@@ -194,6 +211,10 @@ public boolean hasEncodedValue(String key) {
194
211
return encodedValueMap .get (key ) != null ;
195
212
}
196
213
214
+ public boolean hasTurnEncodedValue (String key ) {
215
+ return turnEncodedValueMap .get (key ) != null ;
216
+ }
217
+
197
218
public List <String > getVehicles () {
198
219
// we define the 'vehicles' as all the prefixes for which there is an access and speed EV
199
220
// any EVs that contain prefix_average_speed are accepted
@@ -272,6 +293,35 @@ public <T extends EncodedValue> T getEncodedValue(String key, Class<T> encodedVa
272
293
return (T ) ev ;
273
294
}
274
295
296
+ public List <EncodedValue > getTurnEncodedValues () {
297
+ return Collections .unmodifiableList (new ArrayList <>(turnEncodedValueMap .values ()));
298
+ }
299
+
300
+ public DecimalEncodedValue getTurnDecimalEncodedValue (String key ) {
301
+ return getTurnEncodedValue (key , DecimalEncodedValue .class );
302
+ }
303
+
304
+ public BooleanEncodedValue getTurnBooleanEncodedValue (String key ) {
305
+ return getTurnEncodedValue (key , BooleanEncodedValue .class );
306
+ }
307
+
308
+ public <T extends EncodedValue > T getTurnEncodedValue (String key , Class <T > encodedValueType ) {
309
+ EncodedValue ev = turnEncodedValueMap .get (key );
310
+ // todo: why do we not just return null when EV is missing? just like java.util.Map? -> https://github.com/graphhopper/graphhopper/pull/2561#discussion_r859770067
311
+ if (ev == null )
312
+ throw new IllegalArgumentException ("Cannot find Turn-EncodedValue " + key + " in collection: " + encodedValueMap .keySet ());
313
+ return (T ) ev ;
314
+ }
315
+
316
+ private String toTurnEncodedValuesAsString () {
317
+ List <String > serializedEVsList = turnEncodedValueMap .values ().stream ().map (EncodedValueSerializer ::serializeEncodedValue ).collect (Collectors .toList ());
318
+ try {
319
+ return Jackson .newObjectMapper ().writeValueAsString (serializedEVsList );
320
+ } catch (JsonProcessingException e ) {
321
+ throw new UncheckedIOException (e );
322
+ }
323
+ }
324
+
275
325
public static String getKey (String prefix , String str ) {
276
326
return prefix + "_" + str ;
277
327
}
0 commit comments