@Override public Entity parse(String line) { tokenizer.reset(line); lineNo += 1; EntityBuilder builder = newEntityBuilder() .setId(lineNo + baseId); // since ID is already set, a subsequent ID column will properly override for (TypedName column: fileColumns) { String value = tokenizer.nextToken(); if (value != null && column != null) { Object parsed; try { parsed = column.parseString(value); } catch (IllegalArgumentException e) { throw new DataAccessException("line " + lineNo + ": error parsing column " + column, e); } builder.setAttribute(column, parsed); } } return builder.build(); } }
@Override void invoke(EntityBuilder eb, int position) { if (!attrStore.isNull(position)) { eb.setDoubleAttribute(attrName, attrStore.getDouble(position)); } } }
public Entity likeBatch(long uid, long iid, int count) { return Entities.newBuilder(EntityType.forName("like-batch")) .setId(++entityId) .setLongAttribute(CommonAttributes.USER_ID, uid) .setLongAttribute(CommonAttributes.ITEM_ID, iid) .setAttribute(CommonAttributes.COUNT, count) .build(); } }
builder.setAttribute(attr); return viewClass.cast(builder.build()); } else { throw new IllegalArgumentException("entity type " + e.getClass() + " cannot be projected to " + viewClass);
/** * Set an attribute in the entity. * @param name The name of the attribute to set. * @param val The attribute value. * @return The entity builder (for chaining). * @throws NoSuchAttributeException if the specified attribute is not supported by this entity. */ public EntityBuilder setLongAttribute(TypedName<Long> name, long val) { return setAttribute(name, val); }
/** * Create a new basic entity builder. * @param id The entity ID. * @return The entity builder. */ public static EntityBuilder newBuilder(EntityType type, long id) { return newBuilder(type).setId(id); }
@Test public void testBareEntity() { EntityBuilder eb = Entities.newBuilder(CommonTypes.USER, 42); Entity e = eb.build(); assertThat(e, notNullValue()); assertThat(e.getType(), equalTo(CommonTypes.USER)); assertThat(e.getId(), equalTo(42L)); assertThat(e.getTypedAttributeNames(), contains((TypedName) CommonAttributes.ENTITY_ID)); assertThat(e.getAttributeNames(), contains("id")); assertThat(e.get(CommonAttributes.ENTITY_ID), equalTo(42L)); assertThat(e.get("id"), equalTo((Object) 42L)); }
@Override void invoke(EntityBuilder eb, int position) { if (!attrStore.isNull(position)) { eb.setLongAttribute(attrName, attrStore.getLong(position)); } } }
@Test public void testQueryRejectOneEntityFluent() { Entity e = Entities.newBuilder(LIKE, 1) .setAttribute(CommonAttributes.USER_ID, 42L) .setAttribute(CommonAttributes.ITEM_ID, 39L) .build(); EntityCollectionDAO dao = EntityCollectionDAO.create(e); List<Entity> results = dao.query(LIKE) .withAttribute(CommonAttributes.USER_ID, 39L) .get(); assertThat(results, hasSize(0)); }
@Test public void testBareSetters() { EntityBuilder eb = Entities.newBuilder(CommonTypes.USER) .setId(42); Entity e = eb.build(); assertThat(e, notNullValue()); assertThat(e.getType(), equalTo(CommonTypes.USER)); assertThat(e.getId(), equalTo(42L)); assertThat(e.getTypedAttributeNames(), contains((TypedName) CommonAttributes.ENTITY_ID)); assertThat(e.getAttributeNames(), contains("id")); assertThat(e.hasAttribute("user"), equalTo(false)); assertThat(e.hasAttribute(CommonAttributes.USER_ID), equalTo(false)); }
/** * Set an attribute in the entity. * @param name The name of the attribute to set. * @param val The attribute value. * @return The entity builder (for chaining). * @throws NoSuchAttributeException if the specified attribute is not supported by this entity. */ public EntityBuilder setDoubleAttribute(TypedName<Double> name, double val) { return setAttribute(name, val); }
/** * Create a new basic entity builder. * @param id The entity ID. * @return The entity builder. */ public static EntityBuilder newBuilder(EntityType type, long id) { return newBuilder(type).setId(id); }
@Override void invoke(EntityBuilder eb, int position) { if (!attrStore.isNull(position)) { eb.setLongAttribute(attrName, attrStore.getLong(position)); } } }
JsonNode idNode = node.get("$id"); if (idNode != null) { eb.setId(node.get("$id").asLong()); } else { if (!warned) { warned = true; eb.setId(-lineNo); eb.setAttribute(attr, mapper.convertValue(fn, attr.getJacksonType())); } else { if (fn.isIntegralNumber()) { eb.setAttribute(TypedName.create(name, Long.class), fn.asLong()); } else if (fn.isFloatingPointNumber()) { eb.setAttribute(TypedName.create(name, Double.class), fn.asDouble()); } else if (fn.isTextual()) { eb.setAttribute(TypedName.create(name, String.class), fn.asText()); } else { eb.setAttribute(TypedName.create(name, JsonNode.class), fn); return eb.build(); } else { throw new IllegalArgumentException("line " + lineNo + ": not an object");
@Test public void testQuerySortEntities() { Entity e1 = Entities.newBuilder(LIKE, 1) .setAttribute(CommonAttributes.USER_ID, 42L) .setAttribute(CommonAttributes.ITEM_ID, 39L) .build(); Entity e2 = Entities.newBuilder(LIKE, 2) .setAttribute(CommonAttributes.USER_ID, 42L) .setAttribute(CommonAttributes.ITEM_ID, 28L) .build(); EntityCollectionDAO dao = EntityCollectionDAO.create(e1, e2); EntityQuery<Entity> query = EntityQuery.newBuilder(LIKE) .addSortKey(CommonAttributes.ITEM_ID) .build(); List<Entity> results = ObjectStreams.makeList(dao.streamEntities(query)); assertThat(results, contains(e2, e1)); }
@Override void invoke(EntityBuilder eb, int position) { Object obj = attrStore.get(position); if (obj != null) { eb.setAttribute((TypedName) attrName, obj); } } }
@Override void invoke(EntityBuilder eb, int position) { if (!attrStore.isNull(position)) { eb.setDoubleAttribute(attrName, attrStore.getDouble(position)); } } }
@Test public void testNullProject() { Entity e = Entities.newBuilder(CommonTypes.RATING) .setId(10) .setAttribute(CommonAttributes.USER_ID, 15L) .setAttribute(CommonAttributes.ITEM_ID, 25L) .setAttribute(CommonAttributes.RATING, 3.5) .setAttribute(CommonAttributes.TIMESTAMP, 2308010L) .build(); Entity e2 = Entities.project(e, Entity.class); assertThat(e2, sameInstance(e)); }