@Nullable @Override public Attribute<?> apply(@Nullable TypedName<?> input) { assert input != null; return Attribute.create((TypedName) input, get(input)); } });
/** * Add a filter condition to the builder. * @param name The attribute to filter. * @param obj The object to test for equality. * @param <A> The attribute type. * @return The query builder (for chaining). */ public <A> EntityQueryBuilder addFilterField(TypedName<A> name, @Nonnull A obj) { return this.addFilterField(Attribute.create(name, obj)); }
@Override public Iterator<Attribute<?>> iterator() { return (Iterator) IntStream.range(0, attributeNames.size()) .mapToObj(i -> { if (i == 0) { return Attribute.create(CommonAttributes.ENTITY_ID, getId()); } Object val = attributeValues[i-1]; if (val == null) { return null; } else { return Attribute.create((TypedName) attributeNames.getAttribute(i), val); } }) .filter(Predicates.notNull()) .iterator(); }
@Override public Iterator<Attribute<?>> iterator() { return (Iterator) IntStream.range(0, layout.attributes.size()) .mapToObj(i -> { Object val = layout.getters.get(i).get(AbstractBeanEntity.this); if (val == null) { return null; } else { return Attribute.create((TypedName) layout.attributes.getAttribute(i), val); } }) .filter(Predicates.notNull()) .iterator(); }
@Override public <T> EntityBuilder setAttribute(TypedName<T> name, T val) { Preconditions.checkNotNull(name, "attribute"); Preconditions.checkNotNull(val, "value"); Preconditions.checkArgument(name.getRawType().isInstance(val), "value %s not of type %s", val, name.getType()); if (name == CommonAttributes.ENTITY_ID) { return setId(((Long) val).longValue()); } else { attributes.put(name.getName(), Attribute.create(name, val)); return this; } }
@Test public void testTwoAttrFilter() { EntityQuery<Entity> q = EntityQuery.newBuilder(CommonTypes.RATING) .addFilterField(CommonAttributes.ITEM_ID, 42L) .addFilterField(CommonAttributes.USER_ID, 29L) .build(); assertThat(q.getEntityType(), equalTo(CommonTypes.RATING)); assertThat(q.getFilterFields(), (Matcher) containsInAnyOrder(Attribute.create(CommonAttributes.ITEM_ID, 42L), Attribute.create(CommonAttributes.USER_ID, 29L))); assertThat(q.getSortKeys(), hasSize(0)); assertThat(q.getViewType(), equalTo(Entity.class)); }
@Test public void testSingleAttrFilter() { EntityQuery<Entity> q = EntityQuery.newBuilder(CommonTypes.RATING) .addFilterField(CommonAttributes.ITEM_ID, 42L) .build(); assertThat(q.getEntityType(), equalTo(CommonTypes.RATING)); assertThat(q.getFilterFields(), (Matcher) contains(Attribute.create(CommonAttributes.ITEM_ID, 42L))); assertThat(q.getSortKeys(), hasSize(0)); assertThat(q.getViewType(), equalTo(Entity.class)); }
@Nullable @Override public Attribute<?> apply(@Nullable TypedName<?> input) { assert input != null; return Attribute.create((TypedName) input, get(input)); } });
/** * Add a filter condition to the builder. * @param name The attribute to filter. * @param obj The object to test for equality. * @param <A> The attribute type. * @return The query builder (for chaining). */ public <A> EntityQueryBuilder addFilterField(TypedName<A> name, @Nonnull A obj) { return this.addFilterField(Attribute.create(name, obj)); }
@Override public Iterator<Attribute<?>> iterator() { return (Iterator) IntStream.range(0, attributeNames.size()) .mapToObj(i -> { if (i == 0) { return Attribute.create(CommonAttributes.ENTITY_ID, getId()); } Object val = attributeValues[i-1]; if (val == null) { return null; } else { return Attribute.create((TypedName) attributeNames.getAttribute(i), val); } }) .filter(Predicates.notNull()) .iterator(); }
@Override public Iterator<Attribute<?>> iterator() { return (Iterator) IntStream.range(0, layout.attributes.size()) .mapToObj(i -> { Object val = layout.getters.get(i).get(AbstractBeanEntity.this); if (val == null) { return null; } else { return Attribute.create((TypedName) layout.attributes.getAttribute(i), val); } }) .filter(Predicates.notNull()) .iterator(); }
@Override public <T> EntityBuilder setAttribute(TypedName<T> name, T val) { Preconditions.checkNotNull(name, "attribute"); Preconditions.checkNotNull(val, "value"); Preconditions.checkArgument(name.getRawType().isInstance(val), "value %s not of type %s", val, name.getType()); if (name == CommonAttributes.ENTITY_ID) { return setId(((Long) val).longValue()); } else { attributes.put(name.getName(), Attribute.create(name, val)); return this; } }