@Nullable @Override public Entity lookup(long id) { if (idSet.contains(id)) { return Entities.create(entityType, id); } else { return null; } }
/** * Create an ordering (comparator) over entities from this sort key. * @return An ordering over entities. */ @SuppressWarnings("unchecked") public Ordering<Entity> ordering() { Ordering<Entity> ord; if (comparator != null) { ord = Ordering.from(comparator) .onResultOf((Function) Entities.attributeValueFunction(attribute)); } else { ord = Ordering.natural() .onResultOf((Function) Entities.attributeValueFunction(attribute)); } if (SortOrder.DESCENDING.equals(order)) { ord = ord.reverse(); } return ord; }
/** * 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); }
/** * Get the user's test ratings. * Summarizes the user's ratings from the history. * * @return The user's ratings for the test items. */ public Long2DoubleMap getTestRatings() { if (testRatings == null) { Predicate<Entity> predicate = Entities.typePredicate(CommonTypes.RATING); Function<Entity, Rating> targetViewClass = Entities.projection(Rating.class); ImmutableList<Rating> list = FluentIterable.from(testHistory).filter(predicate).transform(targetViewClass).toList(); testRatings = Ratings.userRatingVector(list); } return testRatings; } }
@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)); }
@Test public void testAttributeMatch() { EntityQuery<Entity> q = EntityQuery.newBuilder(CommonTypes.RATING) .addFilterField(CommonAttributes.USER_ID, 42L) .build(); assertThat(q.matches(Entities.create(CommonTypes.RATING, 42L)), equalTo(false)); assertThat(q.matches(Entities.create(CommonTypes.USER, 42L)), equalTo(false)); assertThat(q.matches(Entities.newBuilder(CommonTypes.RATING, 42L) .setAttribute(CommonAttributes.USER_ID, 42L) .build()), equalTo(true)); assertThat(q.matches(Entities.newBuilder(CommonTypes.RATING, 42L) .setAttribute(CommonAttributes.USER_ID, 78L) .build()), equalTo(false)); } }
@Nullable @Override public <E extends Entity> E lookupEntity(EntityType type, long id, Class<E> view) { Entity entity = lookupEntity(type, id); if (entity == null) { return null; } else { return Entities.project(entity, view); } }
@Override public IdBox<List<E>> readObject() { while (iter.hasNext()) { Map.Entry<Long, List<Entity>> entry = iter.next(); Stream<Entity> data = entry.getValue() .stream() .filter(query); Ordering<Entity> ord = query.getOrdering(); if (ord != null) { data = data.sorted(ord); } List<E> list = data.map(Entities.projection(query.getViewType())) .collect(Collectors.toList()); if (!list.isEmpty()) { return IdBox.create(entry.getKey(), list); } } // we're done return null; } };
@Test public void testGroupOneEntity() { Entity e = Entities.newBuilder(LIKE, 1) .setAttribute(CommonAttributes.USER_ID, 42L) .setAttribute(CommonAttributes.ITEM_ID, 39L) .build(); EntityCollectionDAO dao = EntityCollectionDAO.create(e); EntityQuery<Entity> query = EntityQuery.newBuilder(LIKE) .build(); List<IdBox<List<Entity>>> results = ObjectStreams.makeList(dao.streamEntityGroups(query, CommonAttributes.USER_ID)); assertThat(results, hasSize(1)); IdBox<List<Entity>> box = results.get(0); assertThat(box.getId(), equalTo(42L)); assertThat(box.getValue(), contains(Entities.copyBuilder(e).build())); }
public MapEntityCollectionBuilder(EntityType type) { this.type = type; store = KeyedObjectMap.newBuilder(Entities.idKeyExtractor()); indexBuilders = new HashMap<>(); }
@Override public void describeTo(DescriptionWriter writer) { for (EntityType etype: Ordering.natural() .onResultOf(Entities.entityTypeNameFunction()) .sortedCopy(storage.keySet())) { writer.putField(etype.getName(), storage.get(etype)); } } }
@Test public void testConvertToRating() { 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(); Rating rating = Entities.project(e, Rating.class); assertThat(rating.getId(), equalTo(10L)); assertThat(rating.getUserId(), equalTo(15L)); assertThat(rating.getValue(), equalTo(3.5)); assertThat(rating.getTimestamp(), equalTo(2308010L)); assertThat(rating.equals(e), equalTo(true)); assertThat(e.equals(rating), equalTo(true)); } }
/** * Create a projection function that maps entities to a new view. * @param viewClass The target view class type. * @param <E> The entity type. * @return A function that will project entities. */ @SuppressWarnings("unchecked") public static <E extends Entity> Function<Entity,E> projection(final Class<E> viewClass) { if (viewClass.equals(Entity.class)) { return (Function) Functions.identity(); } else { return n -> project(n, viewClass); } }
: ObjectStreams.transform(baseStream, Entities.projection(query.getViewType())); List<SortKey> sort = query.getSortKeys(); List<SortKey> dataKeys = data.getSortKeys();
public MapEntityCollectionBuilder(EntityType type) { this.type = type; store = KeyedObjectMap.newBuilder(Entities.idKeyExtractor()); indexBuilders = new HashMap<>(); }
@Override public void describeTo(DescriptionWriter writer) { for (EntityType etype: Ordering.natural() .onResultOf(Entities.entityTypeNameFunction()) .sortedCopy(storage.keySet())) { writer.putField(etype.getName(), storage.get(etype)); } } }
public TestUser build() { return new TestUser(Entities.create(CommonTypes.USER, userId), ImmutableList.copyOf(trainEvents), ImmutableList.copyOf(testEvents)); } }
/** * Create a new entity builder that is initialized with a copy of an entity. * @param e The entity. * @return An entity builder initialized to build a copy of {@code e}. */ public static EntityBuilder copyBuilder(Entity e) { EntityBuilder eb = newBuilder(e.getType(), e.getId()); for (TypedName a: e.getTypedAttributeNames()) { eb.setAttribute(a, e.get(a)); } return eb; }
@Nullable @Override public <E extends Entity> E lookupEntity(EntityType type, long id, Class<E> view) { Entity entity = lookupEntity(type, id); if (entity == null) { return null; } else { return Entities.project(entity, view); } }
@Override public IdBox<List<E>> readObject() { while (iter.hasNext()) { Map.Entry<Long, List<Entity>> entry = iter.next(); Stream<Entity> data = entry.getValue() .stream() .filter(query); Ordering<Entity> ord = query.getOrdering(); if (ord != null) { data = data.sorted(ord); } List<E> list = data.map(Entities.projection(query.getViewType())) .collect(Collectors.toList()); if (!list.isEmpty()) { return IdBox.create(entry.getKey(), list); } } // we're done return null; } };