Here you can find the source of isValidValue(String value, Class> type)
Parameter | Description |
---|---|
value | a parameter |
type | a parameter |
public static boolean isValidValue(String value, Class<?> type)
//package com.java2s; //License from project: Apache License import java.math.BigDecimal; import java.util.Calendar; import javax.xml.bind.DatatypeConverter; public class Main { /**//from ww w.j a v a 2 s .c o m * Given a string value and a Java type, returns true if the given * value is a legal assignment to an Attribute having the given Java type. * <code>null</code> or empty values are always legal. * @param value * @param type * @return */ public static boolean isValidValue(String value, Class<?> type) { if (null == value || value.length() == 0) return true; // missing values are always legal if (String.class == type) { return true; // string values are always legal (opaque strings) } else if (Integer.class == type) { try { DatatypeConverter.parseInteger(value); return true; } catch (NumberFormatException e) { } } else if (Long.class == type) { try { DatatypeConverter.parseLong(value); return true; } catch (NumberFormatException e) { } } else if (Short.class == type) { try { DatatypeConverter.parseShort(value); return true; } catch (NumberFormatException e) { } } else if (BigDecimal.class == type) { try { DatatypeConverter.parseDecimal(value); return true; } catch (NumberFormatException e) { } } else if (Double.class == type) { try { DatatypeConverter.parseDouble(value); return true; } catch (NumberFormatException e) { } } else if (Float.class == type) { try { DatatypeConverter.parseFloat(value); return true; } catch (NumberFormatException e) { } } else if (Calendar.class == type) { try { DatatypeConverter.parseDateTime(value); return true; } catch (IllegalArgumentException e) { try { DatatypeConverter.parseDate(value); return true; } catch (IllegalArgumentException e1) { } } } else if (Boolean.class == type) { try { DatatypeConverter.parseBoolean(value); return true; } catch (IllegalArgumentException e) { } } else { throw new IllegalArgumentException("Unsupported type: " + type); } return false; } }