1616 */
1717package org .apache .commons .vfs2 ;
1818
19+ import java .nio .charset .Charset ;
1920import java .time .Duration ;
2021import java .util .Objects ;
2122import java .util .function .Function ;
@@ -96,7 +97,7 @@ protected boolean getBoolean(final FileSystemOptions fileSystemOptions, final St
9697 */
9798 protected Boolean getBoolean (final FileSystemOptions fileSystemOptions , final String name ,
9899 final Boolean defaultValue ) {
99- return getParam (fileSystemOptions , name , defaultValue , Boolean ::valueOf );
100+ return getParam (fileSystemOptions , name , defaultValue , Boolean ::valueOf , Boolean . class );
100101 }
101102
102103 /**
@@ -136,7 +137,7 @@ protected byte getByte(final FileSystemOptions fileSystemOptions, final String n
136137 * @since 2.0
137138 */
138139 protected Byte getByte (final FileSystemOptions fileSystemOptions , final String name , final Byte defaultValue ) {
139- return getParam (fileSystemOptions , name , defaultValue , Byte ::valueOf );
140+ return getParam (fileSystemOptions , name , defaultValue , Byte ::valueOf , Byte . class );
140141 }
141142
142143 /**
@@ -188,6 +189,32 @@ protected Character getCharacter(final FileSystemOptions fileSystemOptions, fina
188189 return value ;
189190 }
190191
192+ /**
193+ * Gets a named option as a Charset.
194+ *
195+ * @param fileSystemOptions file system options to query, may be null.
196+ * @param name the option name
197+ * @return the option in {@code opts} or system properties, otherwise null
198+ * @see #getCharset(FileSystemOptions, String, Charset)
199+ * @since 2.11.0
200+ */
201+ protected Charset getCharset (final FileSystemOptions fileSystemOptions , final String name ) {
202+ return getCharset (fileSystemOptions , name , null );
203+ }
204+
205+ /**
206+ * Gets a named option as a Charset.
207+ *
208+ * @param fileSystemOptions file system options to query, may be null.
209+ * @param name the option name
210+ * @param defaultValue value to return if option is not present
211+ * @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
212+ * @since 2.11.0
213+ */
214+ protected Charset getCharset (final FileSystemOptions fileSystemOptions , final String name , final Charset defaultValue ) {
215+ return getParam (fileSystemOptions , name , defaultValue , Charset ::forName , Charset .class );
216+ }
217+
191218 /**
192219 * Gets the target of this configuration.
193220 *
@@ -235,7 +262,7 @@ protected double getDouble(final FileSystemOptions fileSystemOptions, final Stri
235262 */
236263 protected Double getDouble (final FileSystemOptions fileSystemOptions , final String name ,
237264 final Double defaultValue ) {
238- return getParam (fileSystemOptions , name , defaultValue , Double ::valueOf );
265+ return getParam (fileSystemOptions , name , defaultValue , Double ::valueOf , Double . class );
239266 }
240267
241268 /**
@@ -262,7 +289,7 @@ protected Duration getDuration(final FileSystemOptions fileSystemOptions, final
262289 */
263290 protected Duration getDuration (final FileSystemOptions fileSystemOptions , final String name ,
264291 final Duration defaultValue ) {
265- return getParam (fileSystemOptions , name , defaultValue , Duration ::parse );
292+ return getParam (fileSystemOptions , name , defaultValue , Duration ::parse , Duration . class );
266293 }
267294
268295 /**
@@ -289,7 +316,7 @@ protected Integer getDurationInteger(final FileSystemOptions fileSystemOptions,
289316 */
290317 protected Integer getDurationInteger (final FileSystemOptions fileSystemOptions , final String name ,
291318 final Duration defaultValue ) {
292- return DurationUtils .toMillisInt (getParam (fileSystemOptions , name , defaultValue , Duration ::parse ));
319+ return DurationUtils .toMillisInt (getParam (fileSystemOptions , name , defaultValue , Duration ::parse , Duration . class ));
293320 }
294321
295322 /**
@@ -375,7 +402,7 @@ protected float getFloat(final FileSystemOptions fileSystemOptions, final String
375402 * @since 2.0
376403 */
377404 protected Float getFloat (final FileSystemOptions fileSystemOptions , final String name , final Float defaultValue ) {
378- return getParam (fileSystemOptions , name , defaultValue , Float ::valueOf );
405+ return getParam (fileSystemOptions , name , defaultValue , Float ::valueOf , Float . class );
379406 }
380407
381408 /**
@@ -419,7 +446,7 @@ protected int getInteger(final FileSystemOptions fileSystemOptions, final String
419446 */
420447 protected Integer getInteger (final FileSystemOptions fileSystemOptions , final String name ,
421448 final Integer defaultValue ) {
422- return getParam (fileSystemOptions , name , defaultValue , Integer ::valueOf );
449+ return getParam (fileSystemOptions , name , defaultValue , Integer ::valueOf , Integer . class );
423450 }
424451
425452 /**
@@ -462,7 +489,7 @@ protected long getLong(final FileSystemOptions fileSystemOptions, final String n
462489 * @since 2.0
463490 */
464491 protected Long getLong (final FileSystemOptions fileSystemOptions , final String name , final Long defaultValue ) {
465- return getParam (fileSystemOptions , name , defaultValue , Long ::valueOf );
492+ return getParam (fileSystemOptions , name , defaultValue , Long ::valueOf , Long . class );
466493 }
467494
468495 /**
@@ -486,22 +513,21 @@ protected <T> T getParam(final FileSystemOptions fileSystemOptions, final String
486513 * @param name get option with this name
487514 * @param defaultValue value to use if the system property value is null.
488515 * @param function Builds an instance of T from a system property String value.
516+ * @param returnType TODO
489517 * @return the named option or null
490518 * @since 2.8.0
491519 */
492- private <T > T getParam (final FileSystemOptions fileSystemOptions , final String name , final T defaultValue ,
493- final Function < String , T > function ) {
520+ private <T > T getParam (final FileSystemOptions fileSystemOptions , final String name , final T defaultValue , final Function < String , T > function ,
521+ final Class < T > returnType ) {
494522 T value = getParam (fileSystemOptions , name );
495523 if (value == null ) {
496524 final String str = getProperty (name );
497525 if (str == null ) {
498526 return defaultValue ;
499527 }
500- if (function != null ) {
501- value = function .apply (str );
502- }
528+ value = function .apply (str );
503529 }
504- return value ;
530+ return returnType == null ? null : returnType . isInstance ( value ) ? value : function . apply ( Objects . toString ( value )) ;
505531 }
506532
507533 /**
@@ -580,7 +606,7 @@ protected short getShort(final FileSystemOptions fileSystemOptions, final String
580606 * @since 2.0
581607 */
582608 protected Short getShort (final FileSystemOptions fileSystemOptions , final String name , final Short defaultValue ) {
583- return getParam (fileSystemOptions , name , defaultValue , Short ::valueOf );
609+ return getParam (fileSystemOptions , name , defaultValue , Short ::valueOf , Short . class );
584610 }
585611
586612 /**
@@ -607,7 +633,7 @@ protected String getString(final FileSystemOptions fileSystemOptions, final Stri
607633 */
608634 protected String getString (final FileSystemOptions fileSystemOptions , final String name ,
609635 final String defaultValue ) {
610- return getParam (fileSystemOptions , name , defaultValue , String ::valueOf );
636+ return getParam (fileSystemOptions , name , defaultValue , String ::valueOf , String . class );
611637 }
612638
613639 /**
0 commit comments