@@ -54,20 +54,40 @@ public Function createFunction(Constructor constructor) {
5454 }
5555
5656 /**
57- * A Supplier implementation that uses reflection to create new instances
58- * of a class via its constructor. This class handles constructor accessibility
59- * and instantiation errors appropriately.
57+ * Creates a Function that can instantiate objects using the given constructor.
58+ * @param constructor the constructor to use for object instantiation
59+ * @return a Function that creates new instances using the provided constructor
6060 */
61- static final class ConstructorSupplier implements Supplier {
62- private final Constructor constructor ;
61+ public IntFunction createIntFunction (Constructor constructor ) {
62+ return new ConstructorIntFunction (constructor );
63+ }
6364
64- /**
65- * Creates a ConstructorSupplier for the given constructor.
66- * Automatically makes the constructor accessible.
67- *
68- * @param constructor the constructor to use for instantiation
69- */
70- public ConstructorSupplier (Constructor constructor ) {
65+ /**
66+ * Creates a Function that can instantiate objects using the given constructor.
67+ * @param constructor the constructor to use for object instantiation
68+ * @return a Function that creates new instances using the provided constructor
69+ */
70+ public LongFunction createLongFunction (Constructor constructor ) {
71+ return new ConstructorLongFunction (constructor );
72+ }
73+
74+ /**
75+ * Creates a Function that can instantiate objects using the given constructor.
76+ * @param constructor the constructor to use for object instantiation
77+ * @return a Function that creates new instances using the provided constructor
78+ */
79+ public DoubleFunction createDoubleFunction (Constructor constructor ) {
80+ return new ConstructorDoubleFunction (constructor );
81+ }
82+
83+ /**
84+ * Base class for Constructor-based Function implementations.
85+ * Handles constructor accessibility and instantiation errors.
86+ */
87+ abstract static class ConstructorFunctionBase {
88+ protected final Constructor constructor ;
89+
90+ public ConstructorFunctionBase (Constructor constructor ) {
7191 this .constructor = constructor ;
7292 setAccessible ();
7393 }
@@ -103,6 +123,17 @@ protected JSONException errorOnSetAccessible(Exception e) {
103123 protected JSONException errorOnNewInstance (Exception e ) {
104124 return new JSONException (constructor .toString ().concat (" newInstance error" ), e );
105125 }
126+ }
127+
128+ /**
129+ * A Supplier implementation that uses reflection to create new instances
130+ * of a class via its constructor. This class handles constructor accessibility
131+ * and instantiation errors appropriately.
132+ */
133+ static final class ConstructorSupplier extends ConstructorFunctionBase implements Supplier {
134+ public ConstructorSupplier (Constructor constructor ) {
135+ super (constructor );
136+ }
106137
107138 /**
108139 * Creates a new instance of the class using the constructor.
@@ -120,54 +151,91 @@ public Object get() {
120151 }
121152 }
122153
123- static final class ConstructorFunction implements Function {
124- private final Constructor constructor ;
125-
154+ static final class ConstructorFunction extends ConstructorFunctionBase implements Function {
126155 /**
127156 * Creates a ConstructorSupplier for the given constructor.
128157 * Automatically makes the constructor accessible.
129158 *
130159 * @param constructor the constructor to use for instantiation
131160 */
132161 public ConstructorFunction (Constructor constructor ) {
133- this .constructor = constructor ;
134- setAccessible ();
162+ super (constructor );
135163 }
136164
137- /**
138- * Makes the constructor accessible, handling any security exceptions
139- * that might occur during the process.
140- */
141- protected void setAccessible () {
165+ @ Override
166+ public Object apply (Object arg ) {
142167 try {
143- constructor .setAccessible ( true );
168+ return constructor .newInstance ( arg );
144169 } catch (Exception e ) {
145- throw new JSONException ( e . getMessage (), e );
170+ throw errorOnNewInstance ( e );
146171 }
147172 }
173+ }
148174
175+ /**
176+ * A Supplier implementation that uses reflection to create new instances
177+ * of a class via its constructor. This class handles constructor accessibility
178+ * and instantiation errors appropriately.
179+ */
180+ static final class ConstructorIntFunction extends ConstructorFunctionBase implements IntFunction {
149181 /**
150- * Creates a specific JSON exception for constructor accessibility errors.
182+ * Creates a ConstructorSupplier for the given constructor.
183+ * Automatically makes the constructor accessible.
151184 *
152- * @param e the original exception that occurred
153- * @return a JSONException with detailed error information
185+ * @param constructor the constructor to use for instantiation
154186 */
155- protected JSONException errorOnSetAccessible ( Exception e ) {
156- return new JSONException (constructor . toString (). concat ( " setAccessible error" ), e );
187+ public ConstructorIntFunction ( Constructor constructor ) {
188+ super (constructor );
157189 }
158190
191+ @ Override
192+ public Object apply (int arg ) {
193+ try {
194+ return constructor .newInstance (arg );
195+ } catch (Exception e ) {
196+ throw errorOnNewInstance (e );
197+ }
198+ }
199+ }
200+
201+ /**
202+ * A Supplier implementation that uses reflection to create new instances
203+ * of a class via its constructor. This class handles constructor accessibility
204+ * and instantiation errors appropriately.
205+ */
206+ static final class ConstructorLongFunction extends ConstructorFunctionBase implements LongFunction {
159207 /**
160- * Creates a specific JSON exception for constructor instantiation errors.
208+ * Creates a ConstructorSupplier for the given constructor.
209+ * Automatically makes the constructor accessible.
161210 *
162- * @param e the original exception that occurred
163- * @return a JSONException with detailed error information
211+ * @param constructor the constructor to use for instantiation
164212 */
165- protected JSONException errorOnNewInstance ( Exception e ) {
166- return new JSONException (constructor . toString (). concat ( " newInstance error" ), e );
213+ public ConstructorLongFunction ( Constructor constructor ) {
214+ super (constructor );
167215 }
168216
169217 @ Override
170- public Object apply (Object arg ) {
218+ public Object apply (long arg ) {
219+ try {
220+ return constructor .newInstance (arg );
221+ } catch (Exception e ) {
222+ throw errorOnNewInstance (e );
223+ }
224+ }
225+ }
226+
227+ /**
228+ * A Supplier implementation that uses reflection to create new instances
229+ * of a class via its constructor. This class handles constructor accessibility
230+ * and instantiation errors appropriately.
231+ */
232+ static final class ConstructorDoubleFunction extends ConstructorFunctionBase implements DoubleFunction {
233+ public ConstructorDoubleFunction (Constructor constructor ) {
234+ super (constructor );
235+ }
236+
237+ @ Override
238+ public Object apply (double arg ) {
171239 try {
172240 return constructor .newInstance (arg );
173241 } catch (Exception e ) {
0 commit comments