All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bbottema.javareflection.valueconverter.converters.StringConverters Maven / Gradle / Ivy

Go to download

Java Reflection provides a small package with nifty reflection features that will help with finding constructors, methods and value conversions

There is a newer version: 4.1.0
Show newest version
package org.bbottema.javareflection.valueconverter.converters;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass;
import org.bbottema.javareflection.util.Function;
import org.bbottema.javareflection.util.Function.Functions;
import org.bbottema.javareflection.util.commonslang25.NumberUtils;
import org.bbottema.javareflection.valueconverter.IncompatibleTypeException;
import org.bbottema.javareflection.valueconverter.ValueFunction;
import org.bbottema.javareflection.valueconverter.ValueFunction.ValueFunctionImpl;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

import java.io.File;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * Conversions are as follows:
 * FIXME add non-Number conversions
 * 
    *
  1. Integer (or primitive int): Integer.parseInt(value)
  2. *
  3. Character: value.getCharAt(0)
  4. *
  5. Boolean: value equals {@code "true"} or {@code "1"}
  6. *
  7. Number: new BigDecimal(value) (simply attempt the widest number type)
  8. *
  9. Byte (or primitive byte): Byte.parseByte(value)
  10. *
  11. Short (or primitive short): Short.parseShort(value)
  12. *
  13. Long (or primitive long): Long.parseLong(value)
  14. *
  15. Float (or primitive float): Float.parseFloat(value)
  16. *
  17. Double (or primitive double): Double.parseDouble(value)
  18. *
  19. BigInteger: BigInteger.valueOf(Long.parseLong(value))
  20. *
  21. BigDecimal: new BigDecimal(value)
  22. *
*/ @Nullable @UtilityClass public final class StringConverters { public static final Collection> STRING_CONVERTERS = produceStringConverters(); private static Collection> produceStringConverters() { ArrayList> converters = new ArrayList<>(); converters.add(new ValueFunctionImpl<>(String.class, String.class, Functions.identity())); converters.add(new ValueFunctionImpl<>(String.class, Character.class, new StringToCharacterFunction())); converters.add(new ValueFunctionImpl<>(String.class, Boolean.class, new StringToBooleanFunction())); converters.add(new ValueFunctionImpl<>(String.class, Number.class, new StringToNumberFunction())); converters.add(new ValueFunctionImpl<>(String.class, Byte.class, new StringToByteFunction())); converters.add(new ValueFunctionImpl<>(String.class, Short.class, new StringToShortFunction())); converters.add(new ValueFunctionImpl<>(String.class, Long.class, new StringToLongFunction())); converters.add(new ValueFunctionImpl<>(String.class, Float.class, new StringToFloatFunction())); converters.add(new ValueFunctionImpl<>(String.class, Double.class, new StringToDoubleFunction())); converters.add(new ValueFunctionImpl<>(String.class, BigInteger.class, new StringToBigIntegerFunction())); converters.add(new ValueFunctionImpl<>(String.class, BigDecimal.class, new StringToBigDecimalFunction())); converters.add(new ValueFunctionImpl<>(String.class, File.class, new StringToFileFunction())); return converters; } /** * Creates a converter to convert a String to an Enum instance, by mapping to the enum's name using * {@link Enum#valueOf(Class, String)}. */ public static > ValueFunction produceStringToEnumConverter(Class targetEnumClass) { return new ValueFunctionImpl<>(String.class, targetEnumClass, new StringToEnumFunction<>(targetEnumClass)); } public static ValueFunction produceTypeToStringConverter(Class fromType) { return new ValueFunctionImpl<>(fromType, String.class, Functions.simpleToString()); } @RequiredArgsConstructor private static class StringToEnumFunction> implements Function { @NonNull private final Class targetEnumClass; @Override public T apply(String value) { // /CLOVER:OFF try { // /CLOVER:ON return Enum.valueOf(targetEnumClass, value); // /CLOVER:OFF } catch (final IllegalArgumentException | SecurityException e) { throw new IncompatibleTypeException(value, String.class, targetEnumClass, e); } // /CLOVER:ON } } private static class StringToCharacterFunction implements Function { @Override public Character apply(String value) { if (value.length() == 1) { return value.charAt(0); } throw new IncompatibleTypeException(value, String.class, Character.class); } } private static class StringToBooleanFunction implements Function { @Override public Boolean apply(String value) { if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("0")) { return false; } else if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("1")) { return true; } throw new IncompatibleTypeException(value, String.class, Boolean.class); } } private static class StringToNumberFunction implements Function { @Override public Number apply(String value) { if (NumberUtils.isNumber(value)) { return new BigDecimal(value); } throw new IncompatibleTypeException(value, String.class, Number.class); } } private static class StringToByteFunction implements Function { @Override public Byte apply(String value) { if (NumberUtils.isNumber(value)) { try { return Byte.parseByte(value); } catch (NumberFormatException e) { throw new IncompatibleTypeException(value, String.class, Byte.class, e); } } throw new IncompatibleTypeException(value, String.class, Byte.class); } } private static class StringToShortFunction implements Function { @Override public Short apply(String value) { if (NumberUtils.isNumber(value)) { try { return Short.parseShort(value); } catch (NumberFormatException e) { throw new IncompatibleTypeException(value, String.class, Short.class, e); } } throw new IncompatibleTypeException(value, String.class, Short.class); } } private static class StringToLongFunction implements Function { @Override public Long apply(String value) { if (NumberUtils.isNumber(value)) { return Long.parseLong(value); } throw new IncompatibleTypeException(value, String.class, Long.class); } } private static class StringToFloatFunction implements Function { @Override public Float apply(String value) { if (NumberUtils.isNumber(value)) { return Float.parseFloat(value); } throw new IncompatibleTypeException(value, String.class, Float.class); } } private static class StringToDoubleFunction implements Function { @Override public Double apply(String value) { if (NumberUtils.isNumber(value)) { return Double.parseDouble(value); } throw new IncompatibleTypeException(value, String.class, Double.class); } } private static class StringToBigIntegerFunction implements Function { @Override public BigInteger apply(String value) { if (NumberUtils.isNumber(value)) { return BigInteger.valueOf(Long.parseLong(value)); } throw new IncompatibleTypeException(value, String.class, BigInteger.class); } } private static class StringToBigDecimalFunction implements Function { @Override public BigDecimal apply(String value) { if (NumberUtils.isNumber(value)) { return new BigDecimal(value); } throw new IncompatibleTypeException(value, String.class, BigDecimal.class); } } private static class StringToFileFunction implements Function { private static final Logger LOGGER = getLogger(StringToBigDecimalFunction.class); @Override public File apply(String value) { File file = new File(value); if (!file.exists()) { LOGGER.debug("file not found for [" + value + "]"); throw new IncompatibleTypeException(value, String.class, File.class); } return file; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy