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

org.joda.convert.factory.NumericObjectArrayStringConverterFactory Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
/*
 *  Copyright 2010-present Stephen Colebourne
 *
 *  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.joda.convert.factory;

import java.util.Arrays;

import org.joda.convert.StringConverter;
import org.joda.convert.StringConverterFactory;
import org.joda.convert.TypedStringConverter;

/**
 * Factory for {@code StringConverter} providing support for numeric object arrays
 * as a comma separated list, using '-' for null.
 * 

* To use, simply register the instance with a {@code StringConvert} instance. *

* This class is immutable and thread-safe. * * @since 1.5 */ public final class NumericObjectArrayStringConverterFactory implements StringConverterFactory { /** * Singleton instance. */ public static final StringConverterFactory INSTANCE = new NumericObjectArrayStringConverterFactory(); /** * Restricted constructor. */ private NumericObjectArrayStringConverterFactory() { } //----------------------------------------------------------------------- /** * Finds a converter by type. * * @param cls the type to lookup, not null * @return the converter, null if not found * @throws RuntimeException (or subclass) if source code is invalid */ @Override public StringConverter findConverter(Class cls) { if (cls.isArray()) { if (cls == Long[].class) { return LongArrayStringConverter.INSTANCE; } if (cls == Integer[].class) { return IntArrayStringConverter.INSTANCE; } if (cls == Short[].class) { return ShortArrayStringConverter.INSTANCE; } if (cls == Double[].class) { return DoubleArrayStringConverter.INSTANCE; } if (cls == Float[].class) { return FloatArrayStringConverter.INSTANCE; } } return null; } //----------------------------------------------------------------------- @Override public String toString() { return getClass().getSimpleName(); } //----------------------------------------------------------------------- enum LongArrayStringConverter implements TypedStringConverter { INSTANCE { @Override public String convertToString(Long[] array) { if (array.length == 0) { return ""; } StringBuilder buf = new StringBuilder(array.length * 8); buf.append(array[0] != null ? array[0] : "-"); for (int i = 1; i < array.length; i++) { buf.append(',').append(array[i] != null ? array[i] : "-"); } return buf.toString(); } @Override public Long[] convertFromString(Class cls, String str) { if (str.length() == 0) { return EMPTY; } int count = 0; int base = 0; int sep = str.indexOf(','); Long[] array = new Long[str.length() / 2 + 1]; while (sep >= 0) { String split = str.substring(base, sep); array[count++] = split.equals("-") ? null : new Long(split); base = sep + 1; sep = str.indexOf(',', sep + 1); } String split = str.substring(base, str.length()); array[count++] = split.equals("-") ? null : new Long(split); return Arrays.copyOf(array, count); } @Override public Class getEffectiveType() { return Long[].class; } }; private static final Long[] EMPTY = new Long[0]; } //----------------------------------------------------------------------- enum IntArrayStringConverter implements TypedStringConverter { INSTANCE { @Override public String convertToString(Integer[] array) { if (array.length == 0) { return ""; } StringBuilder buf = new StringBuilder(array.length * 6); buf.append(array[0] != null ? array[0] : "-"); for (int i = 1; i < array.length; i++) { buf.append(',').append(array[i] != null ? array[i] : "-"); } return buf.toString(); } @Override public Integer[] convertFromString(Class cls, String str) { if (str.length() == 0) { return EMPTY; } int count = 0; int base = 0; int sep = str.indexOf(','); Integer[] array = new Integer[str.length() / 2 + 1]; while (sep >= 0) { String split = str.substring(base, sep); array[count++] = split.equals("-") ? null : new Integer(split); base = sep + 1; sep = str.indexOf(',', sep + 1); } String split = str.substring(base, str.length()); array[count++] = split.equals("-") ? null : new Integer(split); return Arrays.copyOf(array, count); } @Override public Class getEffectiveType() { return Integer[].class; } }; private static final Integer[] EMPTY = new Integer[0]; } //----------------------------------------------------------------------- enum ShortArrayStringConverter implements TypedStringConverter { INSTANCE { @Override public String convertToString(Short[] array) { if (array.length == 0) { return ""; } StringBuilder buf = new StringBuilder(array.length * 3); buf.append(array[0] != null ? array[0] : "-"); for (int i = 1; i < array.length; i++) { buf.append(',').append(array[i] != null ? array[i] : "-"); } return buf.toString(); } @Override public Short[] convertFromString(Class cls, String str) { if (str.length() == 0) { return EMPTY; } int count = 0; int base = 0; int sep = str.indexOf(','); Short[] array = new Short[str.length() / 2 + 1]; while (sep >= 0) { String split = str.substring(base, sep); array[count++] = split.equals("-") ? null : new Short(split); base = sep + 1; sep = str.indexOf(',', sep + 1); } String split = str.substring(base, str.length()); array[count++] = split.equals("-") ? null : new Short(split); return Arrays.copyOf(array, count); } @Override public Class getEffectiveType() { return Short[].class; } }; private static final Short[] EMPTY = new Short[0]; } //----------------------------------------------------------------------- enum DoubleArrayStringConverter implements TypedStringConverter { INSTANCE { @Override public String convertToString(Double[] array) { if (array.length == 0) { return ""; } StringBuilder buf = new StringBuilder(array.length * 8); buf.append(array[0] != null ? array[0] : "-"); for (int i = 1; i < array.length; i++) { buf.append(',').append(array[i] != null ? array[i] : "-"); } return buf.toString(); } @Override public Double[] convertFromString(Class cls, String str) { if (str.length() == 0) { return EMPTY; } int count = 0; int base = 0; int sep = str.indexOf(','); Double[] array = new Double[str.length() / 2 + 1]; while (sep >= 0) { String split = str.substring(base, sep); array[count++] = split.equals("-") ? null : new Double(split); base = sep + 1; sep = str.indexOf(',', sep + 1); } String split = str.substring(base, str.length()); array[count++] = split.equals("-") ? null : new Double(split); return Arrays.copyOf(array, count); } @Override public Class getEffectiveType() { return Double[].class; } }; private static final Double[] EMPTY = new Double[0]; } //----------------------------------------------------------------------- enum FloatArrayStringConverter implements TypedStringConverter { INSTANCE { @Override public String convertToString(Float[] array) { if (array.length == 0) { return ""; } StringBuilder buf = new StringBuilder(array.length * 8); buf.append(array[0] != null ? array[0] : "-"); for (int i = 1; i < array.length; i++) { buf.append(',').append(array[i] != null ? array[i] : "-"); } return buf.toString(); } @Override public Float[] convertFromString(Class cls, String str) { if (str.length() == 0) { return EMPTY; } int count = 0; int base = 0; int sep = str.indexOf(','); Float[] array = new Float[str.length() / 2 + 1]; while (sep >= 0) { String split = str.substring(base, sep); array[count++] = split.equals("-") ? null : new Float(split); base = sep + 1; sep = str.indexOf(',', sep + 1); } String split = str.substring(base, str.length()); array[count++] = split.equals("-") ? null : new Float(split); return Arrays.copyOf(array, count); } @Override public Class getEffectiveType() { return Float[].class; } }; private static final Float[] EMPTY = new Float[0]; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy