ad.java

来自「OSGI这是一个中间件,与UPNP齐名,是用于移植到嵌入式平台之上」· Java 代码 · 共 727 行 · 第 1/2 页

JAVA
727
字号
      if(ARRAY_CLASSES[i-STRING].equals(val.getClass())) {	return i;      }    }    throw new IllegalArgumentException("Unsupported type " + val.getClass().getName());  }  /**   * Get type from primitive object.   *   * @param val an object of one of the boxed primitive java object classes.   * @return <tt>STRING...BOOLEAN</tt>   * @throws IllegalArgumentException if type cannot be derived.   */  public static int getPrimitiveType(Object val) {    if(val instanceof String) {      return STRING;    } else if(val instanceof Integer) {      return INTEGER;    } else if(val instanceof Double) {      return DOUBLE;    } else if(val instanceof Float) {      return FLOAT;    } else if(val instanceof Integer) {      return INTEGER;    } else if(val instanceof Long) {      return LONG;    } else if(val instanceof Boolean) {      return BOOLEAN;    } else if(val instanceof Short) {      return SHORT;    } else if(val instanceof Character) {      return CHARACTER;    } else if(BIGINTEGER_OBJECT.isAssignableFrom(val.getClass())) {      return BIGINTEGER;    } else if(BIGDECIMAL_OBJECT.isAssignableFrom(val.getClass())) {      return BIGDECIMAL;    } else if(val instanceof String) {      return STRING;    } else {      throw new IllegalArgumentException("Unsupported type " + val.getClass().getName());    }  }  /**   * Implementation of validation function.   *   * <p>   * Validation of primitive types is performed by trying to create an'   * object from the corresponding String constructor.   *</p>   * <p>   * Validation of arrays and vectors is performed by splitting   * the input string into comma-separated words.   * </p>   */  public String validate(String value) {    if(card == Integer.MIN_VALUE) {      return validateMany(value, type, Integer.MAX_VALUE);    } else if(card == Integer.MAX_VALUE) {      return validateMany(value, type, Integer.MAX_VALUE);    } else if(card < 0) {      return validateMany(value, type, -card);    } else if(card > 0) {      return validateMany(value, type, card);    } else {      return validateSingle(value, type);    }  }  /**   * Parse a string value to an object given a cardinality and type.   */  public static Object parse(String value, int card, int type) {    if(card < 0) {      return parseMany(value, type);    } else if(card > 0) {      Vector v = parseMany(value, type);      Object array = Array.newInstance(getPrimitiveClass(type), 				       v.size());      for(int i = 0; i < v.size(); i++) {	Array.set(array, i, v.elementAt(i));      }      return array;    } else {      return parseSingle(value, type);    }  }  static Vector parseMany(String value, 			  int type) {    String[] items = Text.splitwords(value, SEQUENCE_SEP, '\"');    //    System.out.println("AD.parseMany '" + value + "', item count=" + items.length);    Vector v = new Vector();    for(int i = 0; i < items.length; i++) {      v.addElement(parseSingle(items[i], type));    }    return v;  }  static String validateMany(String value, 			     int type,			     int maxItems) {    int n = 0;    String[] items = Text.splitwords(value, SEQUENCE_SEP, '\"');    if(maxItems == 0) {      if(items.length != 1) {	return "Expected one item, found " + items.length;      }    }        if(items.length > maxItems) {      return "Max # of items are " + maxItems + ", found " + items.length;    }    StringBuffer sb = new StringBuffer();    for(int i = 0; i < items.length; i++) {      String s = validateSingle(items[i], type);      if(s != null && !"".equals(s)) {	if(sb.length() != 0) {	  sb.append(", ");	}	sb.append(s);      }    }    return sb.toString();  }  static String validateSingle(String value, int type) {    try {      switch(type) {      case STRING: 	if(value == null) {	  throw new IllegalArgumentException("Strings cannot be null");	}	break;      case INTEGER: 	Integer.parseInt(value.trim());	break;      case LONG: 	Long.parseLong(value.trim());	break;      case BYTE: 	Byte.parseByte(value.trim());	break;      case SHORT: 	Short.parseShort(value.trim());	break;      case CHARACTER: 	if(value.length() != 1) {	  throw new IllegalArgumentException("Character strings must be of length 1");	}	break;      case DOUBLE: 	Double.parseDouble(value.trim());	break;      case FLOAT: 	Float.parseFloat(value.trim());	break;      case BOOLEAN:	if(!("true".equals(value.trim()) || "false".equals(value.trim()))) {	  throw new IllegalArgumentException("Booleans must be 'true' or 'false'");	}	break;      default:	break;      }    } catch (Exception e) {      e.printStackTrace();      return e.getMessage();    }    return "";  }  /**   * Parse a single value into an object given a type.   */  public static Object parseSingle(String value, int type) {    switch(type) {    case STRING:       return value;    case INTEGER:       return new Integer(value.trim());    case LONG:       return new Long(value.trim());    case BYTE:       return new Byte(value.trim());    case SHORT:       return new Short(value.trim());    case CHARACTER:       return new Character(value.charAt(0));    case DOUBLE:       return new Double(value.trim());    case FLOAT:       return new Float(value.trim());    case BOOLEAN:      return "true".equals(value.trim()) ? Boolean.TRUE : Boolean.FALSE;    default:      throw new IllegalArgumentException("Cannot parse '" + value + "' to type=" + type);    }  }  /**   * Get java class corresponding to AttributeDefinition type.   *   * @throws IllegalArgumentException if type is not supporte.d   */  public static Class getClass(int type) {    return OBJECT_CLASSES[type - STRING];  }  /**   * Get the primitive java class from a specificed type.   *   * @throws IllegalArgumentException if type is not supported.   */  public static Class getPrimitiveClass(int type) {    return PRIMITIVE_CLASSES[type - STRING];  }  /**   * Convert to human-readable string.   */  public String toString() {    StringBuffer sb = new StringBuffer();    sb.append("AD[");    sb.append("id=" + id);    sb.append(", type=" + type);    sb.append(", name=" + name);    sb.append(", desc=" + desc);    sb.append(", cardinality=" + card);    sb.append(", defValue=" + toString(defValue));    sb.append(", optLabels=" + toString(optLabels));    sb.append(", optValues=" + toString(optValues));    sb.append("]");    return sb.toString();  }  /**   * Convert a object to string  that can be parsed by <tt>parse</tt>   */  public static String toString(Object obj) {    if(obj.getClass().isArray()) {      return toStringFromArray(obj);    } else if(obj instanceof Vector) {      StringBuffer sb = new StringBuffer();      Vector v = (Vector)obj;      for(int i = 0; i < v.size(); i++) {	String s = (String)v.elementAt(i);	sb.append(escape(s));	if(i < v.size() - 1) {	  sb.append(SEQUENCE_SEP);	}      }      return sb.toString();    } else {      return obj.toString();    }  }  /**   * Escape a string so that it'll be parsed as one item, even   * if it contains SEQUENCE_SEP.   */  public static String escape(String s) {    boolean bNeedEscape = s.indexOf(SEQUENCE_SEP) != -1;    if(bNeedEscape) {      if(s.length() > 1 && s.startsWith("\"") && s.endsWith("\"")) {	bNeedEscape = false;      }    }    if(bNeedEscape) {      return "\"" + s + "\"";    } else {      return s;    }  }  public static String toStringFromArray(Object array) {    StringBuffer sb = new StringBuffer();    if(array == null) {      sb.append("null");    } else {      for(int i = 0; i < Array.getLength(array); i++) {	String s = escape(Array.get(array, i).toString());		sb.append(s);	if(i < Array.getLength(array) - 1) {	  sb.append(SEQUENCE_SEP);	}      }    }    return sb.toString();  }  public static String toString(Object[] values) {    StringBuffer sb = new StringBuffer();    if(values == null) {      sb.append("null");    } else {      for(int i = 0; i < values.length; i++) {	String s = escape(values[i].toString());		sb.append(s);		if(i < values.length - 1) {	  sb.append(SEQUENCE_SEP);	}      }    }    return sb.toString();  }  public static String toString(Vector values) {    StringBuffer sb = new StringBuffer();    if(values == null) {      sb.append("null");    } else {      for(int i = 0; i < values.size(); i++) {	sb.append(values.elementAt(i));	if(i < values.size() - 1) {	  sb.append(SEQUENCE_SEP);	}      }    }    return sb.toString();  }  public int compareTo(Object other) {    return id.compareTo(((AD)other).id);  }  public int hashCode() {    return id.hashCode();  }  public boolean equals(Object other) {    if(other == null || !(other instanceof AD)) {      return false;    }    return id.equals(((AD)other).id);  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?