Skip to content

Commit 128f987

Browse files
committed
#372 Deprecate casting methods on PropertyReader, add getValue
1 parent f4c71be commit 128f987

37 files changed

Lines changed: 171 additions & 168 deletions

src/main/java/ch/jalu/configme/properties/TypeBasedProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public TypeBasedProperty(@NotNull String path, @NotNull PropertyType<T> type, @N
3232

3333
@Override
3434
protected @Nullable T getFromReader(@NotNull PropertyReader reader, @NotNull ConvertErrorRecorder errorRecorder) {
35-
return type.convert(reader.getObject(getPath()), errorRecorder);
35+
return type.convert(reader.getValue(getPath()), errorRecorder);
3636
}
3737

3838
@Override

src/main/java/ch/jalu/configme/resource/PropertyReader.java

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package ch.jalu.configme.resource;
22

3+
import ch.jalu.configme.properties.BooleanProperty;
4+
import ch.jalu.configme.properties.DoubleProperty;
5+
import ch.jalu.configme.properties.IntegerProperty;
6+
import ch.jalu.configme.properties.StringProperty;
7+
import ch.jalu.configme.properties.convertresult.PropertyValue;
38
import org.jetbrains.annotations.NotNull;
49
import org.jetbrains.annotations.Nullable;
10+
511
import java.util.List;
612
import java.util.Set;
713

@@ -23,6 +29,14 @@ public interface PropertyReader {
2329
*/
2430
boolean contains(@NotNull String path);
2531

32+
/**
33+
* Returns the object at the given path, or null if absent.
34+
*
35+
* @param path the path to retrieve the value for
36+
* @return the value, or null if there is none
37+
*/
38+
@Nullable Object getValue(@NotNull String path);
39+
2640
/**
2741
* Returns the keys available in the file. Depending on the parameter either all keys are returned,
2842
* or only the keys of the leaf nodes are considered.
@@ -46,47 +60,85 @@ public interface PropertyReader {
4660
*
4761
* @param path the path to retrieve the value for
4862
* @return the value, or null if there is none
63+
* @deprecated Use {@link #getValue}
4964
*/
50-
@Nullable Object getObject(@NotNull String path);
65+
@Deprecated
66+
default @Nullable Object getObject(@NotNull String path) {
67+
return getValue(path);
68+
}
5169

5270
/**
5371
* Returns the value of the given path as a String if available.
5472
*
5573
* @param path the path to retrieve a String for
5674
* @return the value as a String, or null if not applicable or unavailable
75+
* @deprecated read the value with a {@link StringProperty},
76+
* or call {@link #getValue} and perform your own casts
5777
*/
58-
@Nullable String getString(@NotNull String path);
78+
@Deprecated
79+
default @Nullable String getString(@NotNull String path) {
80+
StringProperty strProperty = new StringProperty(path, "");
81+
PropertyValue<String> value = strProperty.determineValue(this);
82+
return value.isValidInResource() ? value.getValue() : null;
83+
}
5984

6085
/**
6186
* Returns the value of the given path as an integer if available.
6287
*
6388
* @param path the path to retrieve an integer for
6489
* @return the value as integer, or null if not applicable or unavailable
90+
* @deprecated read the value with an {@link IntegerProperty},
91+
* or call {@link #getValue} and perform your own casts
6592
*/
66-
@Nullable Integer getInt(@NotNull String path);
93+
@Deprecated
94+
default @Nullable Integer getInt(@NotNull String path) {
95+
IntegerProperty intProperty = new IntegerProperty(path, 0);
96+
PropertyValue<Integer> value = intProperty.determineValue(this);
97+
return value.isValidInResource() ? value.getValue() : null;
98+
}
6799

68100
/**
69101
* Returns the value of the given path as a double if available.
70102
*
71103
* @param path the path to retrieve a double for
72104
* @return the value as a double, or null if not applicable or unavailable
105+
* @deprecated read the value with an {@link DoubleProperty},
106+
* or call {@link #getValue} and perform your own casts
73107
*/
74-
@Nullable Double getDouble(@NotNull String path);
108+
@Deprecated
109+
default @Nullable Double getDouble(@NotNull String path) {
110+
DoubleProperty doubleProperty = new DoubleProperty(path, 0);
111+
PropertyValue<Double> value = doubleProperty.determineValue(this);
112+
return value.isValidInResource() ? value.getValue() : null;
113+
}
75114

76115
/**
77116
* Returns the value of the given path as a boolean if available.
78117
*
79118
* @param path the path to retrieve a boolean for
80119
* @return the value as a boolean, or null if not applicable or unavailable
120+
* @deprecated read the value with a {@link BooleanProperty},
121+
* or call {@link #getValue} and perform your own casts
81122
*/
82-
@Nullable Boolean getBoolean(@NotNull String path);
123+
@Deprecated
124+
default @Nullable Boolean getBoolean(@NotNull String path) {
125+
BooleanProperty boolProperty = new BooleanProperty(path, true);
126+
PropertyValue<Boolean> value = boolProperty.determineValue(this);
127+
return value.isValidInResource() ? value.getValue() : null;
128+
}
83129

84130
/**
85131
* Returns the value of the given path as a list if available.
86132
*
87133
* @param path the path to retrieve a list for
88134
* @return the value as a list, or null if not applicable or unavailable
135+
* @deprecated read the value with a {@link ch.jalu.configme.properties.ListProperty ListProperty},
136+
* or call {@link #getValue} and perform your own casts
89137
*/
90-
@Nullable List<?> getList(@NotNull String path);
138+
@Deprecated
139+
default @Nullable List<?> getList(@NotNull String path) {
140+
Object value = getValue(path);
141+
return value instanceof List<?> ? (List<?>) value : null;
142+
}
91143

92144
}

src/main/java/ch/jalu/configme/resource/YamlFileReader.java

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import ch.jalu.configme.exception.ConfigMeException;
44
import ch.jalu.configme.internal.PathUtils;
5-
65
import org.jetbrains.annotations.NotNull;
76
import org.jetbrains.annotations.Nullable;
87
import org.yaml.snakeyaml.Yaml;
@@ -17,7 +16,6 @@
1716
import java.nio.file.Path;
1817
import java.util.Collections;
1918
import java.util.LinkedHashSet;
20-
import java.util.List;
2119
import java.util.Map;
2220
import java.util.Set;
2321
import java.util.stream.Collectors;
@@ -54,7 +52,7 @@ public YamlFileReader(@NotNull Path path, @NotNull Charset charset) {
5452
}
5553

5654
@Override
57-
public @Nullable Object getObject(@NotNull String path) {
55+
public @Nullable Object getValue(@NotNull String path) {
5856
if (path.isEmpty()) {
5957
return root;
6058
}
@@ -70,40 +68,9 @@ public YamlFileReader(@NotNull Path path, @NotNull Charset charset) {
7068
return node;
7169
}
7270

73-
@Override
74-
public @Nullable String getString(@NotNull String path) {
75-
return getTypedObject(path, String.class);
76-
}
77-
78-
@Override
79-
public @Nullable Integer getInt(@NotNull String path) {
80-
Number n = getTypedObject(path, Number.class);
81-
return (n == null)
82-
? null
83-
: n.intValue();
84-
}
85-
86-
@Override
87-
public @Nullable Double getDouble(@NotNull String path) {
88-
Number n = getTypedObject(path, Number.class);
89-
return (n == null)
90-
? null
91-
: n.doubleValue();
92-
}
93-
94-
@Override
95-
public @Nullable Boolean getBoolean(@NotNull String path) {
96-
return getTypedObject(path, Boolean.class);
97-
}
98-
99-
@Override
100-
public @Nullable List<?> getList(@NotNull String path) {
101-
return getTypedObject(path, List.class);
102-
}
103-
10471
@Override
10572
public boolean contains(@NotNull String path) {
106-
return getObject(path) != null;
73+
return getValue(path) != null;
10774
}
10875

10976
@Override
@@ -118,7 +85,7 @@ public boolean contains(@NotNull String path) {
11885

11986
@Override
12087
public @NotNull Set<String> getChildKeys(@NotNull String path) {
121-
Object object = getObject(path);
88+
Object object = getValue(path);
12289
if (object instanceof Map) {
12390
String pathPrefix = path.isEmpty() ? "" : path + ".";
12491
return ((Map<String, Object>) object).keySet().stream()
@@ -196,23 +163,6 @@ private static boolean isLeafValue(@Nullable Object o) {
196163
return root;
197164
}
198165

199-
/**
200-
* Gets the object at the given path and safely casts it to the given class's type. Returns null
201-
* if no value is available or if it cannot be cast.
202-
*
203-
* @param path the path to retrieve
204-
* @param clazz the class to cast to
205-
* @param <T> the class type
206-
* @return cast value at the given path, null if not applicable
207-
*/
208-
protected <T> @Nullable T getTypedObject(@NotNull String path, @NotNull Class<T> clazz) {
209-
Object value = getObject(path);
210-
if (clazz.isInstance(value)) {
211-
return clazz.cast(value);
212-
}
213-
return null;
214-
}
215-
216166
private static @Nullable Object getEntryIfIsMap(@NotNull String key, @Nullable Object value) {
217167
if (value instanceof Map<?, ?>) {
218168
return ((Map<?, ?>) value).get(key);

src/test/java/ch/jalu/configme/beanmapper/BeanWithFinalFieldsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void shouldThrowForFinalField() {
2626
// given
2727
BeanProperty<BeanWithFinalField> property = new BeanProperty<>("", BeanWithFinalField.class, new BeanWithFinalField());
2828
PropertyReader reader = mock(PropertyReader.class);
29-
given(reader.getObject("")).willReturn(newMapWithName("t"));
29+
given(reader.getValue("")).willReturn(newMapWithName("t"));
3030

3131
// when
3232
ConfigMeException ex = assertThrows(ConfigMeException.class, () -> property.determineValue(reader));
@@ -41,7 +41,7 @@ void shouldNotThrowForFinalTransientField() {
4141
BeanProperty<BeanWithFinalTransientField> property =
4242
new BeanProperty<>("", BeanWithFinalTransientField.class, new BeanWithFinalTransientField());
4343
PropertyReader reader = mock(PropertyReader.class);
44-
given(reader.getObject("")).willReturn(newMapWithName("Zoran"));
44+
given(reader.getValue("")).willReturn(newMapWithName("Zoran"));
4545

4646
// when
4747
PropertyValue<BeanWithFinalTransientField> value = property.determineValue(reader);
@@ -56,7 +56,7 @@ void shouldNotThrowForFinalIgnoredField() {
5656
BeanProperty<BeanWithFinalIgnoredField> property =
5757
new BeanProperty<>("", BeanWithFinalIgnoredField.class, new BeanWithFinalIgnoredField());
5858
PropertyReader reader = mock(PropertyReader.class);
59-
given(reader.getObject("")).willReturn(newMapWithName("Goran"));
59+
given(reader.getValue("")).willReturn(newMapWithName("Goran"));
6060

6161
// when
6262
PropertyValue<BeanWithFinalIgnoredField> value = property.determineValue(reader);
@@ -71,7 +71,7 @@ void shouldNotThrowForOverriddenField() {
7171
BeanProperty<BeanWithFinalOverriddenField> property =
7272
new BeanProperty<>("", BeanWithFinalOverriddenField.class, new BeanWithFinalOverriddenField());
7373
PropertyReader reader = mock(PropertyReader.class);
74-
given(reader.getObject("")).willReturn(newMapWithName("Bojan"));
74+
given(reader.getValue("")).willReturn(newMapWithName("Bojan"));
7575

7676
// when
7777
PropertyValue<BeanWithFinalOverriddenField> value = property.determineValue(reader);

0 commit comments

Comments
 (0)