/** * Returns the property value as a double. * * @throws DatastoreException if no such property * @throws ClassCastException if value is not a double */ @SuppressWarnings("unchecked") public double getDouble(String name) { return ((Value<Double>) getValue(name)).get(); }
@SuppressWarnings({"unchecked", "deprecation"}) @Override public Blob getBlob(String name) { Value<?> value = getValue(name); if (value.getMeaning() == 18 && value instanceof StringValue) { return new Blob(ByteString.copyFromUtf8(getString(name))); } return ((Value<Blob>) value).get(); }
@SuppressWarnings({"unchecked", "deprecation"}) @Test public void testToBuilder() throws Exception { Set<String> content = Collections.singleton("bla"); @SuppressWarnings("rawtypes") ValueBuilder builder = new TestBuilder(); builder.setMeaning(1).set(content).setExcludeFromIndexes(true); Value<?> value = builder.build(); builder = value.toBuilder(); assertEquals(1, value.getMeaning()); assertTrue(value.excludeFromIndexes()); assertEquals(ValueType.LIST, value.getType()); assertEquals(content, value.get()); assertEquals(value, builder.build()); } }
/** * Returns the property value as a blob. * * @throws DatastoreException if no such property * @throws ClassCastException if value is not a blob */ @SuppressWarnings("unchecked") public Blob getBlob(String name) { return ((Value<Blob>) getValue(name)).get(); }
@SuppressWarnings({"unchecked", "deprecation"}) @Override public Timestamp getTimestamp(String name) { Value<?> value = getValue(name); if (value.getMeaning() == 18 && value instanceof LongValue) { return Timestamp.ofTimeMicroseconds(getLong(name)); } return ((Value<Timestamp>) value).get(); }
/** * Returns the property value as a string. * * @throws DatastoreException if no such property * @throws ClassCastException if value is not a string */ @SuppressWarnings("unchecked") public String getString(String name) { return ((Value<String>) getValue(name)).get(); }
@Override public B mergeFrom(P other) { excludeFromIndexes = other.excludeFromIndexes(); meaning = other.getMeaning(); set(other.get()); return self(); }
/** * Returns the property value as a Timestamp. * * @throws DatastoreException if no such property * @throws ClassCastException if value is not a Timestamp */ @SuppressWarnings("unchecked") public Timestamp getTimestamp(String name) { return ((Value<Timestamp>) getValue(name)).get(); }
/** * Returns the property value as a LatLng. * * @throws DatastoreException if no such property. * @throws ClassCastException if value is not a LatLng. */ @SuppressWarnings("unchecked") public LatLng getLatLng(String name) { return ((Value<LatLng>) getValue(name)).get(); }
/** * Returns the property value as a list of values. * * @throws DatastoreException if no such property * @throws ClassCastException if value is not a list of values */ @SuppressWarnings("unchecked") public <T extends Value<?>> List<T> getList(String name) { return (List<T>) getValue(name).get(); }
/** * Returns the property value as a boolean. * * @throws DatastoreException if no such property * @throws ClassCastException if value is not a boolean */ @SuppressWarnings("unchecked") public boolean getBoolean(String name) { return ((Value<Boolean>) getValue(name)).get(); }
/** * Returns the property value as a Key. * * @throws DatastoreException if no such property * @throws ClassCastException if value is not a Key */ @SuppressWarnings("unchecked") public Key getKey(String name) { return ((Value<Key>) getValue(name)).get(); }
/** * Returns the property value as an entity. * * @throws DatastoreException if no such property * @throws ClassCastException if value is not an entity */ @SuppressWarnings("unchecked") public <K extends IncompleteKey> FullEntity<K> getEntity(String name) { return ((Value<FullEntity<K>>) getValue(name)).get(); }
/** * Returns the property value as long. * * @throws DatastoreException if no such property * @throws ClassCastException if value is not a long */ @SuppressWarnings("unchecked") public long getLong(String name) { return ((Value<Long>) getValue(name)).get(); }
@Test public void testGet() throws Exception { for (Map.Entry<ValueType, Value<?>> entry : typeToValue.entrySet()) { ValueType valueType = entry.getKey(); Value<?> value = entry.getValue(); assertEquals(TYPES.get(valueType)[1], value.get()); } TestBuilder builder = new TestBuilder(); Set<String> value = Collections.singleton("bla"); assertEquals(value, builder.set(value).build().get()); }
@Test public void testGetList() throws Exception { BaseEntity<Key> entity = builder.build(); List<? extends Value<?>> list = entity.getList("list1"); assertEquals(3, list.size()); assertEquals(NullValue.of(), list.get(0)); assertEquals("foo", list.get(1).get()); assertEquals(LAT_LNG, list.get(2).get()); list = entity.getList("list2"); assertEquals(2, list.size()); assertEquals(Long.valueOf(10), list.get(0).get()); assertEquals(Double.valueOf(2), list.get(1).get()); list = entity.getList("list3"); assertEquals(1, list.size()); assertEquals(Boolean.TRUE, list.get(0).get()); entity = builder.set("list1", ListValue.of(list)).build(); assertEquals(list, entity.getList("list1")); List<Value<?>> stringList = entity.getList("stringList"); assertEquals( ImmutableList.of(StringValue.of("s1"), StringValue.of("s2"), StringValue.of("s3")), stringList); List<Value<Double>> doubleList = entity.getList("doubleList"); assertEquals( ImmutableList.of(DoubleValue.of(12.3), DoubleValue.of(4.56), DoubleValue.of(.789)), doubleList); List<EntityValue> entityList = entity.getList("entityList"); assertEquals( ImmutableList.of(EntityValue.of(ENTITY), EntityValue.of(PARTIAL_ENTITY)), entityList); }
@Override protected Date toPojo(final Value<Timestamp> value) { return new Date(value.get().toSqlTimestamp().getTime()); }
@Override @SuppressWarnings("unchecked") protected Enum<E> loadValue(final Value<String> value, final LoadContext ctx, final Path path) throws SkipException { return Enum.valueOf((Class)tk.getTypeAsClass(), value.get()); }
@Override protected ReadableInstant loadValue(final Value<Timestamp> value, final LoadContext ctx, final Path path) throws SkipException { return TypeUtils.invoke(ctor, value.get().toSqlTimestamp().getTime()); }
@Override public Object load(final Value<FullEntity<?>> container, final LoadContext ctx, final Path path) throws SkipException { final IncompleteKey key = container.get().getKey(); final EntityMetadata<?> meta = registrar.getMetadataSafe(key.getKind()); return meta.getTranslator().load(container, ctx, path); }