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

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

Go to download

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

There is a newer version: 2.9.1
Show newest version
/*******************************************************************************
 * Copyright 2014 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 an input String to Objects of a given type T
 *
 * 

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. *

It also provides a default implementation of {@link ObjectConversion#revert(Object)} that returns the result of input.toString() * * @param The object type resulting from conversions of String values. * * @author uniVocity Software Pty Ltd - [email protected] */ public abstract class ObjectConversion extends NullConversion { /** * Creates a Conversion from String to an Object 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 ObjectConversion() { super(null, null); } /** * Creates a Conversion from String to an Object with default values to return when the input is null. * * @param valueIfStringIsNull default value of type T to be returned when the input String is null. Used when {@link ObjectConversion#execute(String)} is invoked. * @param valueIfObjectIsNull default String value to be returned when an input of type T is null. Used when {@link ObjectConversion#revert(Object)} is invoked. */ public ObjectConversion(T valueIfStringIsNull, String valueIfObjectIsNull) { super(valueIfStringIsNull, valueIfObjectIsNull); } /** * Converts the given String to an instance of T * * @param input the input String to be converted to an object of type T * * @return the conversion result, or the value of {@link ObjectConversion#getValueIfStringIsNull()} if the input String is null. */ @Override public T execute(String input) { return super.execute(input); } /** * Creates an instance of T from a String representation. * * @param input The String to be converted to T * * @return an instance of T, converted from the String input. */ protected final T fromInput(String input) { return fromString(input); } /** * Creates an instance of T from a String representation. * * @param input The String to be converted to T * * @return an instance of T, converted from the String input. */ protected abstract T fromString(String input); /** * Converts a value of type T back to a String *

This is a general implementation that simply returns the result of input.toString() * * @param input the input of type T to be converted to a String * * @return the conversion result, or the value of {@link ObjectConversion#getValueIfObjectIsNull()} if the input object is null. */ @Override public String revert(T input) { return super.revert(input); } @Override protected final String undo(T input) { return String.valueOf(input); } /** * returns a default value of type T to be returned when the input String is null. Used when {@link ObjectConversion#execute(String)} is invoked. * * @return the default value of type T used when converting from a null input */ public T getValueIfStringIsNull() { return getValueOnNullInput(); } /** * returns default String value to be returned when an input of type T is null. Used when {@link ObjectConversion#revert(Object)} is invoked. * * @return the default String value used when converting from a null input */ public String getValueIfObjectIsNull() { return getValueOnNullOutput(); } /** * defines a default value of type T which should be returned when the input String is null. Used when {@link ObjectConversion#execute(String)} is invoked. * * @param valueIfStringIsNull the default value of type T when converting from a null input */ public void setValueIfStringIsNull(T valueIfStringIsNull) { setValueOnNullInput(valueIfStringIsNull); } /** * returns default value of type T which should be returned when the input String is null. Used when {@link ObjectConversion#revert(Object)} is invoked. * * @param valueIfObjectIsNull a default value of type T when converting from a null input */ public void setValueIfObjectIsNull(String valueIfObjectIsNull) { setValueOnNullOutput(valueIfObjectIsNull); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy