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

org.srplib.conversion.IfConverter Maven / Gradle / Ivy

package org.srplib.conversion;

/**
 * A converter encapsulating IF logic.
 *
 * 

Input value is converted to boolean using following rules: *

    *
  • if null then false
  • *
  • if Boolean then return as is
  • *
  • else Boolean.valueOf(value)
  • *
*

* * @author Anton Pechinsky */ public class IfConverter implements Converter { private O trueValue; private O falseValue; /** * An alternative to constructor. * * @param trueValue a value to be returned if expression is true. {@code null} is legal value. * @param falseValue a value to be returned if expression is false. {@code null} is legal value. * @param input value type * @param output value type * @return IfConverter */ public static IfConverter ifConverter(O trueValue, O falseValue) { return new IfConverter(trueValue, falseValue); } /** * Creates converter using specified true an false values. * * @param trueValue a value to be returned if expression is true. {@code null} is legal value. * @param falseValue a value to be returned if expression is false. {@code null} is legal value. */ public IfConverter(O trueValue, O falseValue) { this.trueValue = trueValue; this.falseValue = falseValue; } @Override public O convert(I input) { return asBoolean(input) ? trueValue : falseValue; } private boolean asBoolean(Object input) { if (input == null) { return false; } if (input instanceof Boolean) { return (Boolean) input; } return Boolean.valueOf(String.valueOf(input)); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy