11package org .machinemc .scriptive .serialization ;
22
3+ import org .jetbrains .annotations .Contract ;
34import org .jetbrains .annotations .Nullable ;
45import org .jetbrains .annotations .Unmodifiable ;
56import 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