77import java .util .Set ;
88import java .util .HashSet ;
99import java .util .Collection ;
10- import java .util .function .Function ;
11- import java .util .function .Supplier ;
1210
1311/**
1412 * The {@code JSONBuilder} class provides a configurable mechanism for
@@ -42,46 +40,96 @@ public class JSONBuilder {
4240 * <li>{@code String.class} -> Identity function</li>
4341 * </ul>
4442 */
45- private static final Map <Class <?>, Function < Object , ?>> classMapping = new HashMap <>();
43+ private static final Map <Class <?>, TypeConverter < ?>> classMapping = new HashMap <>();
4644
4745 /**
4846 * A mapping from collection interface types to suppliers that produce
4947 * instances of concrete collection implementations.
5048 *
51- * <p>Examples of default mappings:
52- * <ul>
53- * <li>{@code List.class} -> {@code ArrayList::new}</li>
54- * <li>{@code Set.class} -> {@code HashSet::new}</li>
55- * <li>{@code Map.class} -> {@code HashMap::new}</li>
56- * </ul>
5749 */
58- private static final Map <Class <?>, Supplier <?>> collectionMapping = new HashMap <>();
50+ private static final Map <Class <?>, InstanceCreator <?>> collectionMapping = new HashMap <>();
5951
6052 // Static initializer block to populate default mappings
6153 static {
62- classMapping .put (int .class , s -> ((Number ) s ).intValue ());
63- classMapping .put (Integer .class , s -> ((Number ) s ).intValue ());
64- classMapping .put (double .class , s -> ((Number ) s ).doubleValue ());
65- classMapping .put (Double .class , s -> ((Number ) s ).doubleValue ());
66- classMapping .put (float .class , s -> ((Number ) s ).floatValue ());
67- classMapping .put (Float .class , s -> ((Number ) s ).floatValue ());
68- classMapping .put (long .class , s -> ((Number ) s ).longValue ());
69- classMapping .put (Long .class , s -> ((Number ) s ).longValue ());
70- classMapping .put (boolean .class , s -> s );
71- classMapping .put (Boolean .class , s -> s );
72- classMapping .put (String .class , s -> s );
54+ classMapping .put (int .class , new TypeConverter <Integer >() {
55+ public Integer convert (Object input ) {
56+ return ((Number ) input ).intValue ();
57+ }
58+ });
59+ classMapping .put (Integer .class , new TypeConverter <Integer >() {
60+ public Integer convert (Object input ) {
61+ return ((Number ) input ).intValue ();
62+ }
63+ });
64+ classMapping .put (double .class , new TypeConverter <Double >() {
65+ public Double convert (Object input ) {
66+ return ((Number ) input ).doubleValue ();
67+ }
68+ });
69+ classMapping .put (Double .class , new TypeConverter <Double >() {
70+ public Double convert (Object input ) {
71+ return ((Number ) input ).doubleValue ();
72+ }
73+ });
74+ classMapping .put (float .class , new TypeConverter <Float >() {
75+ public Float convert (Object input ) {
76+ return ((Number ) input ).floatValue ();
77+ }
78+ });
79+ classMapping .put (Float .class , new TypeConverter <Float >() {
80+ public Float convert (Object input ) {
81+ return ((Number ) input ).floatValue ();
82+ }
83+ });
84+ classMapping .put (long .class , new TypeConverter <Long >() {
85+ public Long convert (Object input ) {
86+ return ((Number ) input ).longValue ();
87+ }
88+ });
89+ classMapping .put (Long .class , new TypeConverter <Long >() {
90+ public Long convert (Object input ) {
91+ return ((Number ) input ).longValue ();
92+ }
93+ });
94+ classMapping .put (boolean .class , new TypeConverter <Boolean >() {
95+ public Boolean convert (Object input ) {
96+ return (Boolean ) input ;
97+ }
98+ });
99+ classMapping .put (Boolean .class , new TypeConverter <Boolean >() {
100+ public Boolean convert (Object input ) {
101+ return (Boolean ) input ;
102+ }
103+ });
104+ classMapping .put (String .class , new TypeConverter <String >() {
105+ public String convert (Object input ) {
106+ return (String ) input ;
107+ }
108+ });
73109
74- collectionMapping .put (List .class , ArrayList ::new );
75- collectionMapping .put (Set .class , HashSet ::new );
76- collectionMapping .put (Map .class , HashMap ::new );
110+ collectionMapping .put (List .class , new InstanceCreator <List >() {
111+ public List create () {
112+ return new ArrayList ();
113+ }
114+ });
115+ collectionMapping .put (Set .class , new InstanceCreator <Set >() {
116+ public Set create () {
117+ return new HashSet ();
118+ }
119+ });
120+ collectionMapping .put (Map .class , new InstanceCreator <Map >() {
121+ public Map create () {
122+ return new HashMap ();
123+ }
124+ });
77125 }
78126
79127 /**
80128 * Returns the current class-to-function mapping used for type conversions.
81129 *
82130 * @return a map of classes to functions that convert an {@code Object} to that class
83131 */
84- public Map <Class <?>, Function < Object , ?>> getClassMapping () {
132+ public Map <Class <?>, TypeConverter < ?>> getClassMapping () {
85133 return this .classMapping ;
86134 }
87135
@@ -90,7 +138,7 @@ public class JSONBuilder {
90138 *
91139 * @return a map of collection interface types to suppliers of concrete implementations
92140 */
93- public Map <Class <?>, Supplier <?>> getCollectionMapping () {
141+ public Map <Class <?>, InstanceCreator <?>> getCollectionMapping () {
94142 return this .collectionMapping ;
95143 }
96144
@@ -103,7 +151,7 @@ public Map<Class<?>, Supplier<?>> getCollectionMapping() {
103151 * @param clazz the target class for which the conversion function is to be set
104152 * @param function a function that takes an {@code Object} and returns an instance of {@code clazz}
105153 */
106- public void setClassMapping (Class <?> clazz , Function < Object , ?> function ) {
154+ public void setClassMapping (Class <?> clazz , TypeConverter < ?> function ) {
107155 classMapping .put (clazz , function );
108156 }
109157
@@ -116,7 +164,7 @@ public void setClassMapping(Class<?> clazz, Function<Object, ?> function) {
116164 * @param clazz the collection interface class (e.g., {@code List.class})
117165 * @param function a supplier that creates a new instance of a concrete implementation
118166 */
119- public void setCollectionMapping (Class <?> clazz , Supplier <?> function ) {
167+ public void setCollectionMapping (Class <?> clazz , InstanceCreator <?> function ) {
120168 collectionMapping .put (clazz , function );
121169 }
122170}
0 commit comments