private static Object convertValue(NetType type, String value) throws InvalidValueException { if (value == null) return null; if (type instanceof NetRange) { NetRange nrange = (NetRange) type; Integer cvalue; try { cvalue = new Integer(value); } catch (NumberFormatException ex) { throw new InvalidValueException(ex); } int ival = ((Integer) cvalue).intValue(); if (ival < nrange.getMin()) throw new InvalidValueException(); if (ival > nrange.getMax()) throw new InvalidValueException(); return cvalue; } else if (type instanceof NetEnum) { NetEnum nenum = (NetEnum) type; boolean found = false; for (String eval : nenum.getValues()) { if (eval.equals(value)) { found = true; break; } } if (!found) throw new InvalidValueException(); return value; } else throw new InvalidValueException(); // bad type }
private static void checkValue(NetType type, Object value) throws InvalidValueException { if (value == null) return; if (type instanceof NetRange) { NetRange nrange = (NetRange) type; if (!(value instanceof Integer)) throw new InvalidValueException(); int ival = ((Integer) value).intValue(); if (ival < nrange.getMin()) throw new InvalidValueException(); if (ival > nrange.getMax()) throw new InvalidValueException(); } else if (type instanceof NetEnum) { NetEnum nenum = (NetEnum) type; if (!(value instanceof String)) throw new InvalidValueException(); String sval = (String) value; boolean found = false; for (String eval : nenum.getValues()) { if (eval.equals(sval)) { found = true; break; } } if (!found) throw new InvalidValueException(); } else throw new InvalidValueException(); // bad type }