Skip to content

Commit f2f477a

Browse files
committed
Fix ComponentProperties#getOr and ComponentProperties#getValueOr
1 parent 3526915 commit f2f477a

1 file changed

Lines changed: 12 additions & 15 deletions

File tree

scriptive-core/src/main/java/org/machinemc/scriptive/serialization/ComponentProperties.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.machinemc.scriptive.serialization;
22

3+
import org.jetbrains.annotations.Contract;
34
import org.jetbrains.annotations.Nullable;
45
import org.jetbrains.annotations.Unmodifiable;
56
import org.jetbrains.annotations.UnmodifiableView;
@@ -67,19 +68,16 @@ public <T> Optional<T> getValue(String key, Class<T> type) {
6768
* has different type.
6869
*
6970
* @param key key of the property
70-
* @param or the default property value
71+
* @param or the default property value. Cannot be null
7172
* @return property
7273
* @param <T> property type
7374
*/
75+
@Contract("_, null -> fail")
7476
@SuppressWarnings("unchecked")
7577
public <T extends ComponentProperty<?>> T getOr(String key, T or) {
76-
try {
77-
ComponentProperty<?> property = propertyMap.get(key);
78-
if (property == null) return or;
79-
return (T) property;
80-
} catch (ClassCastException exception) {
81-
return or;
82-
}
78+
ComponentProperty<?> property = propertyMap.get(key);
79+
if (!or.getClass().isInstance(property)) return or;
80+
return (T) property;
8381
}
8482

8583
/**
@@ -92,15 +90,14 @@ public <T extends ComponentProperty<?>> T getOr(String key, T or) {
9290
* @return property
9391
* @param <T> property value type
9492
*/
93+
@Contract("_, null -> fail")
9594
@SuppressWarnings("unchecked")
9695
public <T> T getValueOr(String key, T or) {
97-
try {
98-
ComponentProperty<?> property = propertyMap.get(key);
99-
if (property == null) return or;
100-
return (T) propertyMap.get(key).value();
101-
} catch (ClassCastException exception) {
102-
return or;
103-
}
96+
ComponentProperty<?> property = propertyMap.get(key);
97+
if (property == null) return or;
98+
Object value = property.value();
99+
if (!or.getClass().isInstance(value)) return or;
100+
return (T) value;
104101
}
105102

106103
/**

0 commit comments

Comments
 (0)