public static Object simpleValueFromStream(final Object iValue, final OType iType) {
    switch (iType) {
      case STRING:
        if (iValue instanceof String) {
          final String s = OStringSerializerHelper.getStringContent(iValue);
          return OStringSerializerHelper.decode(s);
        }
        return iValue.toString();

      case INTEGER:
        if (iValue instanceof Integer) return iValue;
        return new Integer(iValue.toString());

      case BOOLEAN:
        if (iValue instanceof Boolean) return iValue;
        return new Boolean(iValue.toString());

      case FLOAT:
        if (iValue instanceof Float) return iValue;
        return convertValue((String) iValue, iType);

      case DECIMAL:
        if (iValue instanceof BigDecimal) return iValue;
        return convertValue((String) iValue, iType);

      case LONG:
        if (iValue instanceof Long) return iValue;
        return convertValue((String) iValue, iType);

      case DOUBLE:
        if (iValue instanceof Double) return iValue;
        return convertValue((String) iValue, iType);

      case SHORT:
        if (iValue instanceof Short) return iValue;
        return convertValue((String) iValue, iType);

      case BYTE:
        if (iValue instanceof Byte) return iValue;
        return convertValue((String) iValue, iType);

      case BINARY:
        return OStringSerializerHelper.getBinaryContent(iValue);

      case DATE:
      case DATETIME:
        if (iValue instanceof Date) return iValue;
        return convertValue((String) iValue, iType);

      case LINK:
        if (iValue instanceof ORID) return iValue.toString();
        else if (iValue instanceof String) return new ORecordId((String) iValue);
        else return ((ORecord<?>) iValue).getIdentity().toString();
    }

    throw new IllegalArgumentException("Type " + iType + " is not simple type.");
  }
  /**
   * Parses a string returning the value with the closer type. Numbers by default are INTEGER if
   * haven't decimal separator, otherwise FLOAT. To treat all the number types numbers are postponed
   * with a character that tells the type: b=byte, s=short, l=long, f=float, d=double, t=date. If
   * starts with # it's a RecordID. Most of the code is equals to getType() but has been copied to
   * speed-up it.
   *
   * @param iUnusualSymbols Localized decimal number separators
   * @param iValue Value to parse
   * @return The closest type recognized
   */
  public static Object getTypeValue(final String iValue) {
    if (iValue == null) return null;

    if (iValue.length() == 0) return "";

    if (iValue.length() > 1)
      if (iValue.charAt(0) == '"' && iValue.charAt(iValue.length() - 1) == '"')
        // STRING
        return OStringSerializerHelper.decode(iValue.substring(1, iValue.length() - 1));
      else if (iValue.charAt(0) == OStringSerializerHelper.BINARY_BEGINEND
          && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.BINARY_BEGINEND)
        // STRING
        return OStringSerializerHelper.getBinaryContent(iValue);
      else if (iValue.charAt(0) == OStringSerializerHelper.COLLECTION_BEGIN
          && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.COLLECTION_END) {
        // COLLECTION
        final ArrayList<String> coll = new ArrayList<String>();
        OStringSerializerHelper.getCollection(iValue, 0, coll);
        return coll;
      } else if (iValue.charAt(0) == OStringSerializerHelper.MAP_BEGIN
          && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.MAP_END) {
        // MAP
        return OStringSerializerHelper.getMap(iValue);
      }

    if (iValue.charAt(0) == ORID.PREFIX)
      // RID
      return new ORecordId(iValue);

    boolean integer = true;
    char c;

    for (int index = 0; index < iValue.length(); ++index) {
      c = iValue.charAt(index);
      if (c < '0' || c > '9')
        if ((index == 0 && (c == '+' || c == '-'))) continue;
        else if (c == DECIMAL_SEPARATOR) integer = false;
        else {
          if (index > 0) {
            if (!integer && c == 'E') {
              // CHECK FOR SCIENTIFIC NOTATION
              if (index < iValue.length()) index++;
              if (iValue.charAt(index) == '-') continue;
            }

            final String v = iValue.substring(0, index);

            if (c == 'f') return new Float(v);
            else if (c == 'c') return new BigDecimal(v);
            else if (c == 'l') return new Long(v);
            else if (c == 'd') return new Double(v);
            else if (c == 'b') return new Byte(v);
            else if (c == 'a' || c == 't') return new Date(Long.parseLong(v));
            else if (c == 's') return new Short(v);
          }
          return iValue;
        }
    }

    if (integer) {
      try {
        return new Integer(iValue);
      } catch (NumberFormatException e) {
        return new Long(iValue);
      }
    } else return new BigDecimal(iValue);
  }
Ejemplo n.º 3
0
  /**
   * Convert types between numbers based on the iTargetClass parameter.
   *
   * @param iValue Value to convert
   * @param iTargetClass Expected class
   * @return The converted value or the original if no conversion was applied
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  public static Object convert(final Object iValue, final Class<?> iTargetClass) {
    if (iValue == null) return null;

    if (iValue.getClass().equals(iTargetClass))
      // SAME TYPE: DON'T CONVERT IT
      return iValue;

    if (iTargetClass.isAssignableFrom(iValue.getClass()))
      // COMPATIBLE TYPES: DON'T CONVERT IT
      return iValue;

    try {
      if (iValue instanceof OBinary && iTargetClass.isAssignableFrom(byte[].class))
        return ((OBinary) iValue).toByteArray();
      else if (byte[].class.isAssignableFrom(iTargetClass)) {
        return OStringSerializerHelper.getBinaryContent(iValue);
      } else if (byte[].class.isAssignableFrom(iValue.getClass())) {
        return iValue;
      } else if (iTargetClass.isEnum()) {
        if (iValue instanceof Number)
          return ((Class<Enum>) iTargetClass).getEnumConstants()[((Number) iValue).intValue()];
        return Enum.valueOf((Class<Enum>) iTargetClass, iValue.toString());
      } else if (iTargetClass.equals(Byte.TYPE) || iTargetClass.equals(Byte.class)) {
        if (iValue instanceof Byte) return iValue;
        else if (iValue instanceof String) return Byte.parseByte((String) iValue);
        else return ((Number) iValue).byteValue();

      } else if (iTargetClass.equals(Short.TYPE) || iTargetClass.equals(Short.class)) {
        if (iValue instanceof Short) return iValue;
        else if (iValue instanceof String) return Short.parseShort((String) iValue);
        else return ((Number) iValue).shortValue();

      } else if (iTargetClass.equals(Integer.TYPE) || iTargetClass.equals(Integer.class)) {
        if (iValue instanceof Integer) return iValue;
        else if (iValue instanceof String) return Integer.parseInt((String) iValue);
        else return ((Number) iValue).intValue();

      } else if (iTargetClass.equals(Long.TYPE) || iTargetClass.equals(Long.class)) {
        if (iValue instanceof Long) return iValue;
        else if (iValue instanceof String) return Long.parseLong((String) iValue);
        else return ((Number) iValue).longValue();

      } else if (iTargetClass.equals(Float.TYPE) || iTargetClass.equals(Float.class)) {
        if (iValue instanceof Float) return iValue;
        else if (iValue instanceof String) return Float.parseFloat((String) iValue);
        else return ((Number) iValue).floatValue();

      } else if (iTargetClass.equals(BigDecimal.class)) {
        if (iValue instanceof BigDecimal) return iValue;
        else if (iValue instanceof String) return new BigDecimal((String) iValue);
        else if (iValue instanceof Number) return new BigDecimal(iValue.toString());

      } else if (iTargetClass.equals(Double.TYPE) || iTargetClass.equals(Double.class)) {
        if (iValue instanceof Double) return iValue;
        else if (iValue instanceof String) return Double.parseDouble((String) iValue);
        else return ((Number) iValue).doubleValue();

      } else if (iTargetClass.equals(Boolean.TYPE) || iTargetClass.equals(Boolean.class)) {
        if (iValue instanceof Boolean) return ((Boolean) iValue).booleanValue();
        else if (iValue instanceof String)
          return ((String) iValue).equalsIgnoreCase("true") ? Boolean.TRUE : Boolean.FALSE;
        else if (iValue instanceof Number) return ((Number) iValue).intValue() != 0;

      } else if (iValue instanceof Collection<?> && Set.class.isAssignableFrom(iTargetClass)) {
        final Set<Object> set = new HashSet<Object>();
        set.addAll((Collection<? extends Object>) iValue);
        return set;

      } else if (iTargetClass.equals(Date.class)) {
        if (iValue instanceof Number) return new Date(((Number) iValue).longValue());
        if (iValue instanceof String) {
          try {
            return ODatabaseRecordThreadLocal.INSTANCE
                .get()
                .getStorage()
                .getConfiguration()
                .getDateTimeFormatInstance()
                .parse((String) iValue);
          } catch (ParseException e) {
            return ODatabaseRecordThreadLocal.INSTANCE
                .get()
                .getStorage()
                .getConfiguration()
                .getDateFormatInstance()
                .parse((String) iValue);
          }
        }
      } else if (iTargetClass.equals(String.class)) return iValue.toString();
    } catch (Exception e) {
      OLogManager.instance()
          .debug(
              OType.class, "Error in conversion of value '%s' to type '%s'", iValue, iTargetClass);
      return null;
    }

    return iValue;
  }