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

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

package org.bbottema.javareflection.valueconverter.converters;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass;
import org.bbottema.javareflection.util.commonslang25.NumberUtils;
import org.bbottema.javareflection.util.Function;
import org.bbottema.javareflection.util.Function.Functions;
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 java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;

/**
 * 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())); 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)) { return Byte.parseByte(value); } throw new IncompatibleTypeException(value, String.class, Byte.class); } } private static class StringToShortFunction implements Function { @Override public Short apply(String value) { if (NumberUtils.isNumber(value)) { return Short.parseShort(value); } 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); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy