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

io.guise.framework.converter.AbstractStringLiteralConverter Maven / Gradle / Ivy

There is a newer version: 0.5.3
Show newest version
/*
 * Copyright © 2005-2008 GlobalMentor, Inc. 
 *
 * 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 io.guise.framework.converter;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import static java.util.Objects.*;

import com.globalmentor.itu.TelephoneNumber;
import com.globalmentor.net.EmailAddress;

/**
 * An abstract implementation an object that can convert a value from and to a string.
 * @param  The value type this converter supports.
 * @author Garret Wilson
 */
public abstract class AbstractStringLiteralConverter extends AbstractConverter {

	/**
	 * {@inheritDoc}
	 * 

* This implementation returns the {@link Object#toString()} version of the value, if a value is given. *

*/ @Override public String convertValue(final V value) throws ConversionException { return value != null ? value.toString() : null; //convert the value to a string if there is a value } /** * Creates a default string literal converter for the value type represented by the given value class. Specific converters are available for the following * types: *
    *
  • char[]
  • *
  • java.lang.Boolean
  • *
  • java.util.Calendar
  • *
  • java.util.Date
  • *
  • java.util.Double
  • *
  • {@link EmailAddress}
  • *
  • java.lang.Float
  • *
  • java.lang.Integer
  • *
  • java.lang.Long
  • *
  • java.util.Locale
  • *
  • java.lang.Long
  • *
  • java.lang.String
  • *
  • {@link TelephoneNumber}
  • *
* If the given type is not recognized, a default one-way value-to-literal converter will be returned that uses a value's {@link Object#toString()} method for * generating values in the lexical space. * @param The type of value represented. * @param valueClass The class of the represented value. * @return The default converter for the value type represented by the given value class. * @throws NullPointerException if the given value class is null. */ @SuppressWarnings("unchecked") //we check the value class before generic casting public static Converter getInstance(final Class valueClass) { requireNonNull(valueClass, "Value class cannot be null."); if(char[].class.isAssignableFrom(valueClass)) { //char[] return (Converter)new CharArrayStringLiteralConverter(); } else if(Boolean.class.isAssignableFrom(valueClass)) { //Boolean return (Converter)new BooleanStringLiteralConverter(); } else if(Calendar.class.isAssignableFrom(valueClass)) { //Calendar return (Converter)new CalendarStringLiteralConverter(DateStringLiteralStyle.SHORT, TimeStringLiteralStyle.FULL); } else if(Date.class.isAssignableFrom(valueClass)) { //Date return (Converter)new DateStringLiteralConverter(DateStringLiteralStyle.SHORT, TimeStringLiteralStyle.FULL); } else if(Double.class.isAssignableFrom(valueClass)) { //Double return (Converter)new DoubleStringLiteralConverter(); } else if(EmailAddress.class.isAssignableFrom(valueClass)) { //EmailAddress return (Converter)new EmailAddressStringLiteralConverter(); } else if(Float.class.isAssignableFrom(valueClass)) { //Float return (Converter)new FloatStringLiteralConverter(); } else if(Integer.class.isAssignableFrom(valueClass)) { //Integer return (Converter)new IntegerStringLiteralConverter(); } else if(Locale.class.isAssignableFrom(valueClass)) { //Locale return (Converter)new LocaleStringLiteralConverter(LocaleStringLiteralStyle.NAME); } else if(Long.class.isAssignableFrom(valueClass)) { //Long return (Converter)new LongStringLiteralConverter(); } else if(String.class.isAssignableFrom(valueClass)) { //String return (Converter)new StringStringLiteralConverter(); } else if(TelephoneNumber.class.isAssignableFrom(valueClass)) { //TelephoneNumber return (Converter)new TelephoneNumberStringLiteralConverter(); } else { //if we don't recognize the value class return new DefaultStringLiteralConverter(valueClass); //return a default string literal converter } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy