Java Code Examples for org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind#Decimal

The following examples show how to use org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind#Decimal . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private EdmPrimitiveTypeKind determineIntegerType(final String intValueAsString) throws UriParserSyntaxException {
  EdmPrimitiveTypeKind typeKind = null;
  try {
    final long value = Long.parseLong(intValueAsString);
    if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
      typeKind = EdmPrimitiveTypeKind.SByte;
    } else if (value >= 0 && value <= 255) {
      typeKind = EdmPrimitiveTypeKind.Byte;
    } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
      typeKind = EdmPrimitiveTypeKind.Int16;
    } else if (value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE) {
      typeKind = EdmPrimitiveTypeKind.Int32;
    } else {
      typeKind = EdmPrimitiveTypeKind.Int64;
    }
  } catch (final NumberFormatException e) {
    // The number cannot be formatted wrongly because the tokenizer already checked the format
    // but it is too large for Long and therefore too large for Edm.Int64.
    typeKind = EdmPrimitiveTypeKind.Decimal;
  }
  return typeKind;
}
 
Example 2
Source File: EdmTypeInfo.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public static EdmPrimitiveTypeKind determineTypeKind(final Object value) {
  if (value == null) {
    return null;
  } else if (value instanceof Boolean) {
    return EdmPrimitiveTypeKind.Boolean;
  } else if (value instanceof String) {
    return EdmPrimitiveTypeKind.String;
  } else if (value instanceof UUID) {
    return EdmPrimitiveTypeKind.Guid;
  } else if (value instanceof Long || value instanceof BigInteger) {
    return EdmPrimitiveTypeKind.Int64;
  } else if (value instanceof Integer) {
    return EdmPrimitiveTypeKind.Int32;
  } else if (value instanceof Short) {
    return EdmPrimitiveTypeKind.Int16;
  } else if (value instanceof Byte) {
    return EdmPrimitiveTypeKind.SByte;
  } else if (value instanceof BigDecimal) {
    return EdmPrimitiveTypeKind.Decimal;
  } else if (value instanceof Double) {
    return EdmPrimitiveTypeKind.Double;
  } else if (value instanceof Float) {
    return EdmPrimitiveTypeKind.Single;
  } else if (value instanceof Calendar || value instanceof Date || value instanceof java.sql.Timestamp) {
    return EdmPrimitiveTypeKind.DateTimeOffset;
  } else if (value instanceof java.sql.Date) {
    return EdmPrimitiveTypeKind.Date;
  } else if (value instanceof java.sql.Time) {
    return EdmPrimitiveTypeKind.TimeOfDay;
  } else if (value instanceof byte[] || value instanceof Byte[]) {
    return EdmPrimitiveTypeKind.Binary;
  }
  return null;
}
 
Example 3
Source File: JsonSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected void primitiveValue(final JsonGenerator jgen, final EdmTypeInfo typeInfo, final Object value)
    throws IOException, EdmPrimitiveTypeException {

  final EdmPrimitiveTypeKind kind = typeInfo == null ?
      EdmTypeInfo.determineTypeKind(value) :
      typeInfo.getPrimitiveTypeKind();

  if (value == null) {
    jgen.writeNull();
  } else if (kind == EdmPrimitiveTypeKind.Boolean) {
    jgen.writeBoolean((Boolean) value);
  } else if (kind == null) {
    if (serverMode) {
      throw new EdmPrimitiveTypeException("The primitive type could not be determined.");
    } else {
      jgen.writeString(value.toString()); // This might not be valid OData.
    }
  } else {
    // TODO: add facets
    final String serialized = EdmPrimitiveTypeFactory.getInstance(kind)
        .valueToString(value, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null);

    if (isIEEE754Compatible && (kind == EdmPrimitiveTypeKind.Int64 || kind == EdmPrimitiveTypeKind.Decimal)
        || !NUMBER_TYPES.contains(kind)) {
      jgen.writeString(serialized);
    } else {
      jgen.writeNumber(serialized);
    }
  }
}
 
Example 4
Source File: JsonDeserializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private EdmPrimitiveTypeKind guessPrimitiveTypeKind(final JsonNode node) {
  return node.isShort() ? EdmPrimitiveTypeKind.Int16 :
    node.isInt() ? EdmPrimitiveTypeKind.Int32 :
      node.isLong() ? EdmPrimitiveTypeKind.Int64 :
        node.isBoolean() ? EdmPrimitiveTypeKind.Boolean :
          node.isFloat() ? EdmPrimitiveTypeKind.Single :
            node.isDouble() ? EdmPrimitiveTypeKind.Double :
              node.isBigDecimal() ? EdmPrimitiveTypeKind.Decimal :
                EdmPrimitiveTypeKind.String;
}
 
Example 5
Source File: EdmConstantExpressionImpl.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void build() {
  if (csdlExp.getType() == CsdlConstantExpression.ConstantExpressionType.EnumMember) {
    if (csdlExp.getValue() == null) {
      throw new EdmException("Expression value must not be null");
    }
    final List<String> localEnumValues = new ArrayList<>();
    for (String split : csdlExp.getValue().split(" ")) {
      final String[] enumSplit = split.split("/");
      if (enumSplit.length != 2) {
        throw new EdmException("Enum expression value must consist of enumTypeName/EnumMember.");
      }
      enumTypeName = enumSplit[0];
      localEnumValues.add(enumSplit[1]);
    }
    enumMembers = Collections.unmodifiableList(localEnumValues);
  } else {
    EdmPrimitiveTypeKind kind;
    switch (csdlExp.getType()) {
    case Binary:
      kind = EdmPrimitiveTypeKind.Binary;
      break;
    case Bool:
      kind = EdmPrimitiveTypeKind.Boolean;
      break;
    case Date:
      kind = EdmPrimitiveTypeKind.Date;
      break;
    case DateTimeOffset:
      kind = EdmPrimitiveTypeKind.DateTimeOffset;
      break;
    case Decimal:
      kind = EdmPrimitiveTypeKind.Decimal;
      break;
    case Duration:
      kind = EdmPrimitiveTypeKind.Duration;
      break;
    case Float:
      kind = EdmPrimitiveTypeKind.Single;
      break;
    case Guid:
      kind = EdmPrimitiveTypeKind.Guid;
      break;
    case Int:
      kind = EdmPrimitiveTypeKind.Int32;
      break;
    case TimeOfDay:
      kind = EdmPrimitiveTypeKind.TimeOfDay;
      break;
    case String:
    default:
      kind = EdmPrimitiveTypeKind.String;
    }
    type = EdmPrimitiveTypeFactory.getInstance(kind);
    try {
      primitive = type.valueOfString(csdlExp.getValue(), null, null, null, null, null, type.getDefaultType());
    } catch (EdmPrimitiveTypeException e) {
      throw new IllegalArgumentException(e);
    }
  }
  built = true;
}
 
Example 6
Source File: ODataJsonDeserializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private boolean matchIEEENumberCase(final JsonNode node, final EdmPrimitiveTypeKind primKind) {
  return (isIEEE754Compatible ? node.isTextual() : node.isNumber())
      && (primKind == EdmPrimitiveTypeKind.Int64 || primKind == EdmPrimitiveTypeKind.Decimal);
}
 
Example 7
Source File: MethodImpl.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public EdmType getType() {
  EdmPrimitiveTypeKind kind = null;
  switch (method) {
  case CONTAINS:
  case STARTSWITH:
  case ENDSWITH:
  case SUBSTRINGOF:
    kind = EdmPrimitiveTypeKind.Boolean;
    break;
  case LENGTH:
  case INDEXOF:
    kind = EdmPrimitiveTypeKind.Int32;
    break;
  case SUBSTRING:
  case TOLOWER:
  case TOUPPER:
  case TRIM:
  case CONCAT:
    kind = EdmPrimitiveTypeKind.String;
    break;
  case YEAR:
  case MONTH:
  case DAY:
  case HOUR:
  case MINUTE:
  case SECOND:
    kind = EdmPrimitiveTypeKind.Int32;
    break;
  case FRACTIONALSECONDS:
  case TOTALSECONDS:
    kind = EdmPrimitiveTypeKind.Decimal;
    break;
  case DATE:
    kind = EdmPrimitiveTypeKind.Date;
    break;
  case TIME:
    kind = EdmPrimitiveTypeKind.TimeOfDay;
    break;
  case TOTALOFFSETMINUTES:
    kind = EdmPrimitiveTypeKind.Int32;
    break;
  case MINDATETIME:
  case MAXDATETIME:
  case NOW:
    kind = EdmPrimitiveTypeKind.DateTimeOffset;
    break;
  case ROUND:
  case FLOOR:
  case CEILING:
    // Needs to be refined if Decimal must be distinguished from Double.
    kind = EdmPrimitiveTypeKind.Double;
    break;
  case GEODISTANCE:
  case GEOLENGTH:
    kind = EdmPrimitiveTypeKind.Double;
    break;
  case GEOINTERSECTS:
    kind = EdmPrimitiveTypeKind.Boolean;
    break;
  case CAST:
    return ((TypeLiteral) parameters.get(parameters.size() - 1)).getType();
  case ISOF:
    kind = EdmPrimitiveTypeKind.Boolean;
    break;
  }
  return new ODataImpl().createPrimitiveTypeInstance(kind);
}