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

cz.jalasoft.util.converter.string.StringConverters Maven / Gradle / Ivy

The newest version!
package cz.jalasoft.util.converter.string;

import cz.jalasoft.util.converter.ConversionException;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created by honzales on 18.4.15.
 */
public interface StringConverters {

    static StringConverter identity() {
        return new StringConverter() {
            @Override
            public String convert(String from) throws ConversionException {
                return from;
            }

            @Override
            public Class targetType() {
                return String.class;
            }
        };
    }

    static StringConverter toInteger() {
        return new StringConverter() {
            @Override
            public Integer convert(String from) throws ConversionException {
                try {
                    return Integer.valueOf(from);
                } catch (NumberFormatException exc) {
                    throw new ConversionException(from, String.class, Integer.class);
                }
            }

            @Override
            public Class targetType() {
                return Integer.class;
            }
        };
    }

    static StringConverter toLong() {
        return new StringConverter() {
            @Override
            public Long convert(String from) throws ConversionException {
                try {
                    return Long.parseLong(from);
                } catch (NumberFormatException exc) {
                    throw new ConversionException(from, String.class, Long.class);
                }
            }

            @Override
            public Class targetType() {
                return Long.class;
            }
        };
    }

    static StringConverter toDouble() {
        return new StringConverter() {
            @Override
            public Double convert(String from) throws ConversionException {
                try {
                    return Double.valueOf(from);
                } catch (NumberFormatException exc) {
                    throw new ConversionException(from, String.class, Double.class);
                }
            }

            @Override
            public Class targetType() {
                return Double.class;
            }
        };
    }

    static StringConverter toDoublePrimitive() {
        return new StringConverter() {
            @Override
            public Double convert(String from) throws ConversionException {
                try {
                    return Double.parseDouble(from);
                } catch (NumberFormatException exc) {
                    throw new ConversionException(from, String.class, Double.class);
                }
            }

            @Override
            public Class targetType() {
                return double.class;
            }
        };
    }

    static StringConverter toIntPrimitive() {
        return new StringConverter() {
            @Override
            public Integer convert(String from) throws ConversionException {
                try {
                    return Integer.parseInt(from);
                } catch (NumberFormatException exc) {
                    throw new ConversionException(from, String.class, Integer.class);
                }
            }

            @Override
            public Class targetType() {
                return int.class;
            }
        };
    }

    static StringConverter toIntegerOr(final StringConverter alternative) {
        return new StringConverter() {
            @Override
            public Integer convert(String from) throws ConversionException {
                try {
                    return Integer.parseInt(from);
                } catch (NumberFormatException exc) {
                    return alternative.convert(from);
                }
            }

            @Override
            public Class targetType() {
                return Integer.class;
            }
        };
    }

    static StringConverter toLocalDateTime(String pattern) {
        return new StringConverter() {
            @Override
            public LocalDateTime convert(String from) throws ConversionException {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
                return LocalDateTime.parse(from, formatter);
            }

            @Override
            public Class targetType() {
                return LocalDateTime.class;
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy