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

com.univocity.parsers.conversions.NullConversion Maven / Gradle / Ivy

Go to download

univocity's open source parsers for processing different text formats using a consistent API

The newest version!
/*******************************************************************************
 * Copyright 2016 Univocity Software Pty Ltd
 *
 * 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 com.univocity.parsers.conversions;

/**
 * Default implementation for conversions from input Objects of type I to output Objects of type O
 *
 * 

Extending classes must implement a proper String to T conversion in {@link ObjectConversion#fromString(String)} *

This abstract class provides default results for conversions when the input is null. * * @author Univocity Software Pty Ltd - [email protected] * * @param The object type resulting from conversions of values of type O. * @param The object type resulting from conversions of values of type I. */ public abstract class NullConversion implements Conversion { private O valueOnNullInput; private I valueOnNullOutput; /** * Creates a Conversion from an object to another object of a different type, with default values to return when the input is null. * The default constructor assumes the output of a conversion should be null when input is null */ public NullConversion() { this(null, null); } /** * Creates a Conversion from an object to another object of a different type, with default values to return when the input is null. * * @param valueOnNullInput default value of type O to be returned when the input object I is null. Used when {@link NullConversion#execute(Object)} is invoked. * @param valueOnNullOutput default value of type I to be returned when an input of type I is null. Used when {@link NullConversion#revert(Object)} is invoked. */ public NullConversion(O valueOnNullInput, I valueOnNullOutput) { this.valueOnNullInput = valueOnNullInput; this.valueOnNullOutput = valueOnNullOutput; } /** * Converts the given instance of type I to an instance of O * * @param input the input value of type I to be converted to an object of type O * * @return the conversion result, or the value of {@link NullConversion#valueOnNullInput} if the input object is null. */ @Override public O execute(I input) { if (input == null) { return valueOnNullInput; } return fromInput(input); } /** * Creates an instance of O from a I object * * @param input The object of type I to be converted to O * * @return an instance of O, converted from the I input. */ protected abstract O fromInput(I input); /** * Converts a value of type O back to a value of type I * * @param input the input of type O to be converted to an output I * * @return the conversion result, or the value of {@link NullConversion#valueOnNullOutput} if the input object is null. */ @Override public I revert(O input) { if (input == null) { return valueOnNullOutput; } return undo(input); } /** * Converts a value of type O back to I. * @param input the input object to be converted to I * @return the conversion result */ protected abstract I undo(O input); /** * returns a default value of type O to be returned when the input of type I is null. Used when {@link NullConversion#execute(Object)} is invoked. * * @return the default value of type O used when converting from a null I */ public O getValueOnNullInput() { return valueOnNullInput; } /** * returns default instance of I to be returned when an input of type O is null. Used when {@link NullConversion#revert(Object)} is invoked. * * @return the default I instance used when converting from a null O */ public I getValueOnNullOutput() { return valueOnNullOutput; } /** * defines the default value of type O which should be returned when {@link NullConversion#execute(Object)} is invoked with a null I.. * * @param valueOnNullInput the default value of type T when converting from a null input */ public void setValueOnNullInput(O valueOnNullInput) { this.valueOnNullInput = valueOnNullInput; } /** * defines the default value of type I which should be returned when {@link NullConversion#revert(Object)} is invoked with a null O. * * @param valueOnNullOutput a default value of type I when converting from a null input */ public void setValueOnNullOutput(I valueOnNullOutput) { this.valueOnNullOutput = valueOnNullOutput; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy