org.bbottema.javareflection.valueconverter.converters.BooleanConverters Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-reflection Show documentation
Show all versions of java-reflection Show documentation
Java Reflection provides a small package with nifty reflection features that will help with finding constructors, methods and value conversions
/*
* Copyright (C) ${project.inceptionYear} Benny Bottema ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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:
*
* - Boolean (or boolean):
value
* - String:
value.toString()
* - Number (or primitive number): 0 or 1
*
*/
@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;
}
}
}