/** * Creates a {@link HashIndexSelect} for matching with field paths and types declared by * {@link FieldPath} annotated fields or methods on the given query type. * * @param queryType the query type * @param <Q> the query type * @return a {@code HashIndexSelect} * @throws IllegalArgumentException if the query type declares one or more invalid field paths * or invalid types given resolution of corresponding field paths * @throws IllegalArgumentException if the select field path is invalid, or the select field type * is invalid given resolution of the select field path. */ public <Q> HashIndexSelect<T, S, Q> usingBean(Class<Q> queryType) { Objects.requireNonNull(queryType); return new HashIndexSelect<>(consumer, rootType, selectFieldType, selectFieldPath, queryType); }
@Test public void testWithSelect() { HashIndexSelect<DataModel.Consumer.TypeA, DataModel.Consumer.HString, Integer> hi = HashIndex .from(consumer, DataModel.Consumer.TypeA.class) .selectField("s", DataModel.Consumer.HString.class) .usingPath("i", int.class); List<String> r = hi.findMatches(1) .sorted(Comparator.comparingInt(HollowObject::getOrdinal)) .map(DataModel.Consumer.HString::getValue) .collect(toList()); Assert.assertEquals(100, r.size()); for (int i = 0; i < r.size(); i++) { Assert.assertEquals("TypeA" + i, r.get(i)); } }
@Test public void testWithSelectGenericHollowObject() { HashIndexSelect<DataModel.Consumer.TypeA, GenericHollowObject, Integer> hi = HashIndex .from(consumer, DataModel.Consumer.TypeA.class) .selectField("s", GenericHollowObject.class) .usingPath("i", int.class); List<String> r = hi.findMatches(1) .sorted(Comparator.comparingInt(HollowObject::getOrdinal)) .map(gho -> gho.getString("value")) .collect(toList()); Assert.assertEquals(100, r.size()); for (int i = 0; i < r.size(); i++) { Assert.assertEquals("TypeA" + i, r.get(i)); } } }
@Test public void test() { HashIndexSelect<DataModel.Consumer.References, S, Integer> hi = HashIndex .from(consumer, DataModel.Consumer.References.class) .selectField(path, type) .usingPath("values._int", int.class); List<S> r = hi.findMatches(1) .collect(toList()); Assert.assertEquals(1, r.size()); Assert.assertTrue(type.isInstance(r.get(0))); Assert.assertEquals(0, r.get(0).getOrdinal()); } }
/** * Creates a {@link HashIndexSelect} for matching with a single query field path and type. * * @param queryFieldPath the query field path * @param queryFieldType the query type * @param <Q> the query type * @return a {@code HashIndexSelect} * @throws IllegalArgumentException if the query field path is empty or invalid * @throws IllegalArgumentException if the query field type is invalid given resolution of the * query field path * @throws IllegalArgumentException if the select field path is invalid, or the select field type * is invalid given resolution of the select field path. */ public <Q> HashIndexSelect<T, S, Q> usingPath(String queryFieldPath, Class<Q> queryFieldType) { Objects.requireNonNull(queryFieldPath); if (queryFieldPath.isEmpty()) { throw new IllegalArgumentException("selectFieldPath argument is an empty String"); } Objects.requireNonNull(queryFieldType); return new HashIndexSelect<>(consumer, rootType, selectFieldType, selectFieldPath, queryFieldPath, queryFieldType); } }
@Test public void testWithSelectRootTypeGenericHollowObject() { HashIndexSelect<DataModel.Consumer.TypeA, GenericHollowObject, Integer> hi = HashIndex .from(consumer, DataModel.Consumer.TypeA.class) .selectField("", GenericHollowObject.class) .usingPath("i", int.class); boolean r = hi.findMatches(1) .sorted(Comparator.comparingInt(HollowObject::getOrdinal)) .mapToInt(gho -> gho.getInt("i")) .allMatch(i -> i == 1); Assert.assertTrue(r); }