Exemplo n.º 1
0
 // TODO: make more setValue methods each with the corresponding type.
 public void setValue(String value) throws NumberFormatException {
   switch (mType) {
     case STRING:
       mValue = value;
       break;
     case ADDRESS:
       try {
         mValue = InetAddress.getByName(value).getHostAddress();
       } catch (UnknownHostException uhe) {
         throw new NumberFormatException("invalid IP address: " + value);
       }
       break;
     case PORT:
       int i = Integer.parseInt(value);
       if (i < 0 || i > 65535) throw new NumberFormatException("port must be between 0 and 65535");
       break;
     case BOOLEAN:
       value = value.toLowerCase();
       if (value.equals("true") || value.equals("false")) mValue = value;
       else throw new NumberFormatException("boolean must be true or false");
       break;
     case ENUM:
       // TODO: handle integer enums
       ArrayList<String> valid = ((ArrayList<String>) mAttributes.get("enums"));
       if (!valid.contains(value)) {
         String valid_line = "";
         for (String v : valid) {
           valid_line += " " + v;
         }
         Logger.warning("expected: (" + valid_line + ") got: " + value);
         throw new NumberFormatException("invalid choice");
       }
       mValue = value;
       break;
     case PATH:
       // TODO:
       mValue = value;
       break;
   }
 }