Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fernandojunior/4cd2fdf49f6e7db5701019516ab436a2 to your computer and use it in GitHub Desktop.
Save fernandojunior/4cd2fdf49f6e7db5701019516ab436a2 to your computer and use it in GitHub Desktop.
Type Parser Java
package com.riofrotas.core.util;
import java.lang.reflect.Array;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Set;
/**
* Disponibiliza metodos para fazer parsing de um valor em String para outro
* tipo. <br>
*
* Exemplo de uso: <br>
*
* // criando novo parser <br>
* Parser x = new Parser("15.5"); //<br>
* // fazendo parsing <br>
* x.getDouble();
*
* https://github.com/drapostolos/type-parser
*
* @author Fernando Felix do Nascimento Junior
*
*/
public class TypeParser {
private String value;
public TypeParser(String value) {
this.value = value;
}
public String getValue() {
return value;
}
/**
* Verifica se o valor for nulo
*
* @return
*/
public boolean isNull() {
if (value == null || value.equals("") || value.equals("null"))
return true;
return false;
}
/**
* Lanca excessao se o valor do parametro for nulo
*
* @throws ParseException
*/
public void throwIfNull() throws ParseException {
if (isNull())
throw new ParseException("Value is null", 0);
}
public Character getNullableCharacter() throws ParseException {
if (!isNull() && value.length() == 1)
return value.charAt(0);
if (!isNull() && value.length() > 1)
throw new ParseException("The length of value is not equal to 1.", 0);
return null;
}
public char getCharacter() throws ParseException {
throwIfNull();
return getNullableCharacter();
}
public String getNullableString() {
return value;
}
public String getString() throws ParseException {
throwIfNull();
return value;
}
public Short getNullableShort() {
if (!isNull())
return Short.parseShort(value);
return null;
}
public short getShort() throws ParseException {
throwIfNull();
return getNullableShort();
}
public Integer getNullableInteger() {
if (!isNull())
return Integer.parseInt(value);
return null;
}
public int getInteger() throws ParseException {
throwIfNull();
return getNullableInteger();
}
public Long getNullableLong() {
if (!isNull())
return Long.parseLong(value);
return null;
}
public long getLong() throws ParseException {
throwIfNull();
return getNullableLong();
}
public Float getNullableFloat() {
if (!isNull())
return (float) Conversor.parseDouble(value);
return null;
}
public float getFloat() throws ParseException {
throwIfNull();
return getNullableFloat();
}
public Double getNullableDouble() {
if (isNull())
return null;
if (Locale.getDefault().getLanguage().equalsIgnoreCase("pt"))
return Conversor.parseDouble(value);
else
return Double.parseDouble(value);
}
public double getDouble() throws ParseException {
throwIfNull();
return getNullableDouble();
}
public Boolean getNullableBoolean(String reference) {
if (isNull())
return null;
if (value.equals(reference))
return true;
else
return false;
}
public Boolean getNullableBoolean() {
return getNullableBoolean("true");
}
public boolean getBoolean(String reference) throws ParseException {
throwIfNull();
return getNullableBoolean(reference);
}
public boolean getBoolean() throws ParseException {
throwIfNull();
return getNullableBoolean();
}
public Date getNullableDate(String format) throws ParseException {
if (!isNull())
return DateUtil.stringToDate(value, format);
return null;
}
public Date getDate(String format) throws ParseException {
throwIfNull();
return getNullableDate(format);
}
public Date getNullableDate() throws ParseException {
if (!isNull())
return DateUtil.stringToDate(value);
return null;
}
public Date getDate() throws ParseException {
throwIfNull();
return getNullableDate();
}
/**
* TODO revisar <br>
* Corverte um determinado objeto para outro tipo conforme a classe passada
* como parametro
*
* @param c
* A classe base
* @param value
* O objeto a ser convertido
* @return O objeto convertido
* @throws ParseException
*/
@SuppressWarnings("unchecked")
public static <T> T parseValue(Class<T> c, Object value) throws ParseException {
if (value == null || value.equals(""))
return null;
else if (value.getClass().equals(c))
return (T) value;
else if (value.equals(c))
return (T) value;
else if (c.equals(Boolean.class))
value = Boolean.parseBoolean(value.toString());
else if (c.equals(Byte.class))
value = Byte.parseByte(value.toString());
else if (c.equals(Short.class))
value = Short.parseShort(value.toString());
else if (c.equals(Integer.class))
value = Integer.parseInt(value.toString());
else if (c.equals(Long.class))
value = Long.parseLong(value.toString());
else if (c.equals(Float.class))
value = Float.parseFloat(value.toString());
else if (c.equals(Double.class))
value = Double.parseDouble(value.toString());
else if (c.equals(Character.class))
value = value.toString().charAt(0);
else if (c.equals(String.class))
value = value.toString();
else if (c.equals(Date.class) && value.getClass().equals(String.class))
value = DateUtil.stringToDate(value.toString());
return parseValue(c, value);
}
/**
*
* TODO revisar <br>
*
* @param c
* @param values
* @return
* @throws ParseException
*/
public static <T> Object[] parseArray(Class<T> c, Object[] values) throws ParseException {
Object[] array = (Object[]) Array.newInstance(c, values.length);
ArrayList<T> tmp = new ArrayList<T>();
for (Object v : values)
tmp.add(parseValue(c, v));
return tmp.toArray(array);
}
/**
*
* @param values
* @return
*/
public static Object[] removeDuplicates(Object[] values) {
Set<Object> tmp = new LinkedHashSet<Object>();
for (Object o : values)
tmp.add(o);
return tmp.toArray();
}
/**
* @param collection
* @return
*/
@SuppressWarnings("unchecked")
public static <E> E[] toArray(Collection<E> collection) {
return (E[]) collection.toArray();
}
/**
*
*
* @param array
* @return
*/
public static <E> Set<E> asSet(E[] array) {
return new LinkedHashSet<E>(Arrays.asList(array));
}
/**
* TODO revisar <br>
*
* @param collection
*/
public static <E> void removeEmptyValues(Collection<E> collection) {
removeEmptyValues(collection.iterator());
}
/**
* TODO revisar <br>
*
* @param iterator
*/
private static <E> void removeEmptyValues(Iterator<E> iterator) {
for (Iterator<E> i = iterator; i.hasNext();) {
E e = i.next();
if (e == null || e.equals("") || e.equals("null"))
i.remove();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment