com.google.cloud.datastore.DoubleValue Java Examples

The following examples show how to use com.google.cloud.datastore.DoubleValue. 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: DoubleMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  return ((DoubleValue) input).get();
}
 
Example #2
Source File: FloatMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToModel_8() {
  double d = 999.9999;
  DoubleValue input = DoubleValue.of(d);
  FloatMapper mapper = new FloatMapper();
  float output = (float) mapper.toModel(input);
  System.out.printf("%s ---> %s%n", d, output);
  assertTrue(999.9999f == output);
}
 
Example #3
Source File: FloatMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToModel_7() {
  double d = 1.0000;
  DoubleValue input = DoubleValue.of(d);
  FloatMapper mapper = new FloatMapper();
  float output = (float) mapper.toModel(input);
  System.out.printf("%s ---> %s%n", d, output);
  assertTrue(1.0000f == output);
}
 
Example #4
Source File: FloatMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test(expected = MappingException.class)
public void testToModel_6() {
  double d = -Float.MAX_VALUE * 2d;
  DoubleValue input = DoubleValue.of(d);
  FloatMapper mapper = new FloatMapper();
  try {
    float output = (float) mapper.toModel(input);
  } catch (Exception e) {
    System.err.println(e);
    throw e;
  }
}
 
Example #5
Source File: FloatMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test(expected = MappingException.class)
public void testToModel_5() {
  double d = Float.MAX_VALUE * 2d;
  DoubleValue input = DoubleValue.of(d);
  FloatMapper mapper = new FloatMapper();
  try {
    float output = (float) mapper.toModel(input);
  } catch (Exception e) {
    System.err.println(e);
    throw e;
  }
}
 
Example #6
Source File: FloatMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToModel_4() {
  double d = -Float.MAX_VALUE;
  DoubleValue input = DoubleValue.of(d);
  FloatMapper mapper = new FloatMapper();
  float output = (float) mapper.toModel(input);
  System.out.printf("%s ---> %s%n", d, output);
  assertTrue(-Float.MAX_VALUE == output);
}
 
Example #7
Source File: FloatMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToModel_3() {
  double d = Float.MAX_VALUE;
  DoubleValue input = DoubleValue.of(d);
  FloatMapper mapper = new FloatMapper();
  float output = (float) mapper.toModel(input);
  System.out.printf("%s ---> %s%n", d, output);
  assertTrue(Float.MAX_VALUE == output);
}
 
Example #8
Source File: FloatMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToModel_2() {
  double d = 3.1415927;
  DoubleValue input = DoubleValue.of(d);
  FloatMapper mapper = new FloatMapper();
  float output = (float) mapper.toModel(input);
  System.out.printf("%s ---> %s%n", d, output);
  assertTrue(3.1415927f == output);
}
 
Example #9
Source File: FloatMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToModel_1() {
  double d = 0.000000;
  DoubleValue input = DoubleValue.of(d);
  FloatMapper mapper = new FloatMapper();
  float output = (float) mapper.toModel(input);
  System.out.printf("%s ---> %s%n", d, output);
  assertTrue(0.000000f == output);
}
 
Example #10
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> getProperties(Entity entity)
    throws IOException, ClassNotFoundException {
  if (entity == null) {
    return null;
  }
  ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
  for (String property : entity.getNames()) {
    // builder.put(property, entity.getValue(property));
    if (entity.getValue(property) instanceof StringValue) {
      builder.put(property, (String) entity.getString(property));
    } else if (entity.getValue(property) instanceof LongValue) {
      // This conversion is safe because of integer to long conversion above
      builder.put(property, new Long(entity.getLong(property)).intValue());
    } else if (entity.getValue(property) instanceof DoubleValue) {
      builder.put(property, (Double) entity.getDouble(property));
    } else if (entity.getValue(property) instanceof BooleanValue) {
      builder.put(property, (Boolean) entity.getBoolean(property));
    } else if (entity.getValue(property) instanceof TimestampValue) {
      builder.put(property, (Timestamp) entity.getTimestamp(property));
    } else {
      Blob blob = entity.getBlob(property);
      Object obj = null;
      try (ObjectInputStream in = new ObjectInputStream(blob.asInputStream())) {
        obj = in.readObject();
      }
      builder.put(property, obj); // BlobValue
    }
  }

  return builder.build();
}
 
Example #11
Source File: DoubleMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  return DoubleValue.newBuilder((double) input);
}
 
Example #12
Source File: CatchAllMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  Object javaValue;
  if (input instanceof NullValue) {
    javaValue = null;
  } else if (input instanceof StringValue) {
    javaValue = input.get();
  } else if (input instanceof LongValue) {
    javaValue = input.get();
  } else if (input instanceof DoubleValue) {
    javaValue = input.get();
  } else if (input instanceof BooleanValue) {
    javaValue = input.get();
  } else if (input instanceof KeyValue) {
    javaValue = new DefaultDatastoreKey(((KeyValue) input).get());
  } else if (input instanceof LatLngValue) {
    LatLng latLong = ((LatLngValue) input).get();
    javaValue = new GeoLocation(latLong.getLatitude(), latLong.getLongitude());
  } else if (input instanceof EntityValue) {
    EntityValue entityValue = (EntityValue) input;
    FullEntity<?> entity = entityValue.get();
    Map<String, Object> map = new HashMap<>();
    for (String property : entity.getNames()) {
      map.put(property, toModel(entity.getValue(property)));
    }
    javaValue = map;
  } else {
    throw new MappingException(String.format("Unsupported type %s", input.getClass().getName()));
  }
  return javaValue;
}
 
Example #13
Source File: CatchAllMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  ValueBuilder<?, ?, ?> builder;
  if (input == null) {
    builder = NullValue.newBuilder();
  } else if (input instanceof Long) {
    builder = LongValue.newBuilder((long) input);
  } else if (input instanceof Double) {
    builder = DoubleValue.newBuilder((double) input);
  } else if (input instanceof Boolean) {
    builder = BooleanValue.newBuilder((boolean) input);
  } else if (input instanceof String) {
    builder = StringValue.newBuilder((String) input);
  } else if (input instanceof DatastoreKey) {
    builder = KeyValue.newBuilder(((DatastoreKey) input).nativeKey());
  } else if (input instanceof GeoLocation) {
    GeoLocation geoLocation = (GeoLocation) input;
    builder = LatLngValue
        .newBuilder(LatLng.of(geoLocation.getLatitude(), geoLocation.getLongitude()));
  } else if (input instanceof Map) {
    @SuppressWarnings("unchecked")
    Map<String, ?> map = (Map<String, ?>) input;
    FullEntity.Builder<IncompleteKey> entityBuilder = FullEntity.newBuilder();
    for (Map.Entry<String, ?> entry : map.entrySet()) {
      String key = entry.getKey();
      entityBuilder.set(key, toDatastore(entry.getValue()).build());
    }
    builder = EntityValue.newBuilder(entityBuilder.build());
  } else {
    throw new MappingException(String.format("Unsupported type: %s", input.getClass().getName()));
  }
  return builder;
}
 
Example #14
Source File: FloatMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  DoubleValue value = (DoubleValue) input;
  Double d = value.get();
  if (d < -Float.MAX_VALUE || d > Float.MAX_VALUE) {
    throw new MappingException(String.format("Value %s is out of range for float type", d));
  }
  return d.floatValue();
}
 
Example #15
Source File: FloatMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  return DoubleValue.newBuilder((float) input);
}
 
Example #16
Source File: BigDecimalMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  return BigDecimal.valueOf(((DoubleValue) input).get());
}
 
Example #17
Source File: BigDecimalMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  return DoubleValue.newBuilder(((BigDecimal) input).doubleValue());
}
 
Example #18
Source File: GqlDatastoreQueryTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Test
public void compoundNameConventionTest() {

	String gql = "SELECT * FROM "
			+ "|org.springframework.cloud.gcp.data.datastore."
			+ "repository.query.GqlDatastoreQueryTests$Trade|"
			+ " WHERE price=:#{#tag6 * -1} AND price<>:#{#tag6 * -1} OR "
			+ "price<>:#{#tag7 * -1} AND " + "( action=@tag0 AND ticker=@tag1 ) OR "
			+ "( trader_id=@tag2 AND price<@tag3 ) OR ( price>=@tag4 AND id<>NULL AND "
			+ "trader_id=NULL AND trader_id LIKE %@tag5 AND price=TRUE AND price=FALSE AND "
			+ "price>@tag6 AND price<=@tag7 AND trade_ref = @tag8) ORDER BY id DESC LIMIT 3;";

	String entityResolvedGql = "SELECT * FROM trades"
			+ " WHERE price=@SpELtag1 AND price<>@SpELtag2 OR price<>@SpELtag3 AND "
			+ "( action=@tag0 AND ticker=@tag1 ) OR "
			+ "( trader_id=@tag2 AND price<@tag3 ) OR ( price>=@tag4 AND id<>NULL AND "
			+ "trader_id=NULL AND trader_id LIKE %@tag5 AND price=TRUE AND price=FALSE AND "
			+ "price>@tag6 AND price<=@tag7 AND trade_ref = @tag8) ORDER BY id DESC LIMIT 3";


	Trade trade = new Trade();
	trade.id = "tradeId1";

	Object[] paramVals = new Object[] { "BUY", "abcd",
			// this is an array param of the non-natively supported type and will need conversion
			new int[] { 1, 2 },
			new double[] { 8.88, 9.99 },
			3, // this parameter is a simple int, which is not a directly supported type and uses
				// conversions
			"blahblah", 1.11, 2.22, trade };

	String[] paramNames = new String[] { "tag0", "tag1", "tag2", "tag3", "tag4",
			"tag5", "tag6", "tag7", "tag8" };

	buildParameters(paramVals, paramNames);

	KeyFactory keyFactory = new KeyFactory("proj");
	keyFactory.setKind("kind");
	Key key = keyFactory.newKey("tradeid1-key");

	doReturn(key).when(this.datastoreTemplate).getKey(any());

	EvaluationContext evaluationContext = new StandardEvaluationContext();
	for (int i = 0; i < paramVals.length; i++) {
		evaluationContext.setVariable(paramNames[i], paramVals[i]);
	}
	when(this.evaluationContextProvider.getEvaluationContext(any(), any()))
			.thenReturn(evaluationContext);

	GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, false, false);

	doAnswer((invocation) -> {
		GqlQuery statement = invocation.getArgument(0);

		assertThat(statement.getQueryString()).isEqualTo(entityResolvedGql);

		Map<String, Value> paramMap = statement.getNamedBindings();

		assertThat(paramMap.get("tag0").get()).isEqualTo(paramVals[0]);
		assertThat(paramMap.get("tag1").get()).isEqualTo(paramVals[1]);

		// custom conversion is expected to have been used in this param
		assertThat((long) ((LongValue) (((List) paramMap.get("tag2").get()).get(0))).get()).isEqualTo(1L);
		assertThat((long) ((LongValue) (((List) paramMap.get("tag2").get()).get(1))).get()).isEqualTo(2L);

		double actual = ((DoubleValue) (((List) paramMap.get("tag3").get()).get(0))).get();
		assertThat(actual).isEqualTo(((double[]) paramVals[3])[0], DELTA);

		actual = ((DoubleValue) (((List) paramMap.get("tag3").get()).get(1))).get();
		assertThat(actual).isEqualTo(((double[]) paramVals[3])[1], DELTA);

		// 3L is expected even though 3 int was the original param due to custom conversions
		assertThat(paramMap.get("tag4").get()).isEqualTo(3L);
		assertThat(paramMap.get("tag5").get()).isEqualTo(paramVals[5]);
		assertThat(paramMap.get("tag6").get()).isEqualTo(paramVals[6]);
		assertThat(paramMap.get("tag7").get()).isEqualTo(paramVals[7]);

		assertThat((double) paramMap.get("SpELtag1").get()).isEqualTo(-1 * (double) paramVals[6],
				DELTA);
		assertThat((double) paramMap.get("SpELtag2").get()).isEqualTo(-1 * (double) paramVals[6],
				DELTA);
		assertThat((double) paramMap.get("SpELtag3").get()).isEqualTo(-1 * (double) paramVals[7],
				DELTA);

		assertThat(((KeyValue) paramMap.get("tag8")).get()).isSameAs(key);

		return null;
	}).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));

	doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());

	gqlDatastoreQuery.execute(paramVals);

	verify(this.datastoreTemplate, times(1))
			.queryKeysOrEntities(any(), eq(Trade.class));
}