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

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

package org.bbottema.javareflection.valueconverter.converters;

import lombok.experimental.UtilityClass;
import org.bbottema.javareflection.util.Function;
import org.bbottema.javareflection.util.Function.Functions;
import org.bbottema.javareflection.valueconverter.ValueFunction;
import org.bbottema.javareflection.valueconverter.ValueFunction.ValueFunctionImpl;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Attempts to convert a Boolean to the target datatype.
 * 

* Conversions are as follows: *

    *
  1. Boolean (or boolean): value
  2. *
  3. String: value.toString()
  4. *
  5. Number (or primitive number): 0 or 1
  6. *
*/ @UtilityClass public final class BooleanConverters { public static final Collection> BOOLEAN_CONVERTERS = produceBooleanConverters(); private static Collection> produceBooleanConverters() { ArrayList> converters = new ArrayList<>(); converters.add(new ValueFunctionImpl<>(boolean.class, Boolean.class, Functions.identity())); converters.add(new ValueFunctionImpl<>(Boolean.class, boolean.class, Functions.identity())); converters.add(new ValueFunctionImpl<>(boolean.class, boolean.class, Functions.identity())); converters.add(new ValueFunctionImpl<>(Boolean.class, Boolean.class, Functions.identity())); converters.add(new ValueFunctionImpl<>(Boolean.class, String.class, Functions.simpleToString())); converters.add(new ValueFunctionImpl<>(Boolean.class, Number.class, new BooleanToNumberFunction())); return converters; } private static class BooleanToNumberFunction implements Function { @Override public Number apply(Boolean value) { return value ? 1 : 0; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy