File tree Expand file tree Collapse file tree
src/main/java/ca/spottedleaf/yamlconfig/adapter Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package ca .spottedleaf .yamlconfig .adapter .generic ;
2+
3+ import ca .spottedleaf .yamlconfig .adapter .TypeAdapter ;
4+ import ca .spottedleaf .yamlconfig .adapter .TypeAdapterRegistry ;
5+ import java .lang .reflect .Type ;
6+
7+ public abstract class StringFormTypeAdapter <T > extends TypeAdapter <T , String > {
8+ @ Override
9+ public final T deserialize (TypeAdapterRegistry registry , Object input , Type type ) {
10+ final TypeAdapter <String , String > forString = (TypeAdapter <String , String >)registry .getAdapter (String .class );
11+
12+ return this .fromString (forString .deserialize (registry , input , type ));
13+ }
14+
15+ @ Override
16+ public final String serialize (final TypeAdapterRegistry registry , final T value , final Type type ) {
17+ final TypeAdapter <String , String > forString = (TypeAdapter <String , String >)registry .getAdapter (String .class );
18+
19+ return forString .serialize (registry , this .toString (value ), type );
20+ }
21+
22+ public abstract T fromString (final String value );
23+
24+ public abstract String toString (final T value );
25+ }
Original file line number Diff line number Diff line change 11package ca .spottedleaf .yamlconfig .adapter .type ;
22
3- import ca .spottedleaf .yamlconfig .adapter .TypeAdapter ;
4- import ca .spottedleaf .yamlconfig .adapter .TypeAdapterRegistry ;
3+ import ca .spottedleaf .yamlconfig .adapter .generic .StringFormTypeAdapter ;
54import ca .spottedleaf .yamlconfig .type .Duration ;
6- import java .lang .reflect .Type ;
75
8- public final class DurationTypeAdapter extends TypeAdapter <Duration , String > {
6+ public final class DurationTypeAdapter extends StringFormTypeAdapter <Duration > {
97
108 public static final DurationTypeAdapter INSTANCE = new DurationTypeAdapter ();
119
1210 @ Override
13- public Duration deserialize (final TypeAdapterRegistry registry , final Object input , final Type type ) {
14- if (!(input instanceof String string )) {
15- throw new IllegalArgumentException ("Not a string: " + input .getClass ());
16- }
17- return Duration .parse (string );
11+ public Duration fromString (final String value ) {
12+ return Duration .parse (value );
1813 }
1914
2015 @ Override
21- public String serialize (final TypeAdapterRegistry registry , final Duration value , final Type type ) {
16+ public String toString (final Duration value ) {
2217 return value .toString ();
2318 }
2419}
You can’t perform that action at this time.
0 commit comments