/** * Creates a {@link HashIndex} 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 HashIndex} * @throws IllegalArgumentException if the query type declares one or more invalid field paths * or invalid types given resolution of corresponding field paths */ public <Q> HashIndex<T, Q> usingBean(Class<Q> queryType) { Objects.requireNonNull(queryType); return new HashIndex<>(consumer, rootType, queryType); }
@Test public void test() { HashIndex<DataModel.Consumer.TypeA, Integer> hi = HashIndex .from(consumer, DataModel.Consumer.TypeA.class) .usingPath("i", int.class); List<DataModel.Consumer.TypeA> r = hi.findMatches(1) .sorted(Comparator.comparingInt(HollowObject::getOrdinal)).collect(toList()); Assert.assertEquals(100, r.size()); for (int i = 0; i < r.size(); i++) { Assert.assertEquals(i, r.get(i).getOrdinal()); } }
@Test(expected = IllegalArgumentException.class) public void testEmptyMatchPath() { HashIndex .from(consumer, DataModel.Consumer.References.class) .usingPath("", DataModel.Consumer.References.class); } }
@Test public void test() { HashIndex<DataModel.Consumer.References, Q> hi = HashIndex .from(consumer, DataModel.Consumer.References.class) .usingPath(path, type); List<DataModel.Consumer.References> r = hi.findMatches(value) .collect(toList()); Assert.assertEquals(1, r.size()); Assert.assertEquals(0, r.get(0).getOrdinal()); } }
@Test(expected = IllegalArgumentException.class) public void test() { HashIndex .from(consumer, DataModel.Consumer.Values.class) .usingPath(path, Object.class); } }
@Test public void test() { HashIndex<T, Q> hi = HashIndex .from(consumer, selectType) .usingPath(path, type); List<T> r = hi .findMatches(value) .collect(toList()); Assert.assertEquals(1, r.size()); Assert.assertTrue(selectType.isInstance(r.get(0))); Assert.assertEquals(0, r.get(0).getOrdinal()); } }
@Test(expected = IllegalArgumentException.class) public void testUnknownRootSelectType() { HashIndex .from(consumer, Unknown.class) .usingPath("values", DataModel.Consumer.Values.class); }
/** * Creates a {@link HashIndex} 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 HashIndex} * @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 */ public <Q> HashIndex<T, Q> usingPath(String queryFieldPath, Class<Q> queryFieldType) { Objects.requireNonNull(queryFieldPath); if (queryFieldPath.isEmpty()) { throw new IllegalArgumentException("queryFieldPath argument is an empty String"); } Objects.requireNonNull(queryFieldType); return new HashIndex<>(consumer, rootType, queryFieldPath, queryFieldType); }
@Test public void testMethods() { HashIndex<DataModel.Consumer.Values, ValueMethodsQuery> hi = HashIndex .from(consumer, DataModel.Consumer.Values.class) .usingBean(ValueMethodsQuery.class); List<DataModel.Consumer.Values> r = hi.findMatches(ValueMethodsQuery.create()) .collect(toList()); Assert.assertEquals(1, r.size()); Assert.assertEquals(0, r.get(0).getOrdinal()); } }
@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 testFields() { HashIndex<DataModel.Consumer.Values, ValueFieldsQuery> hi = HashIndex .from(consumer, DataModel.Consumer.Values.class) .usingBean(ValueFieldsQuery.class); List<DataModel.Consumer.Values> r = hi.findMatches(ValueFieldsQuery.create()) .collect(toList()); Assert.assertEquals(1, r.size()); Assert.assertEquals(0, r.get(0).getOrdinal()); }
@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)); } } }
consumer.triggerRefreshTo(v1); HashIndex<DataModel.Consumer.TypeA, Integer> hi = HashIndex.from(consumer, DataModel.Consumer.TypeA.class) .usingPath("i", int.class); consumer.addRefreshListener(hi); Assert.assertEquals(1L, hi.findMatches(1).count()); Assert.assertEquals(2L, hi.findMatches(1).count()); Assert.assertEquals(2L, hi.findMatches(1).count());
@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()); } }
@Test(expected = IllegalArgumentException.class) public void test() { HashIndex .from(consumer, DataModel.Consumer.Values.class) .selectField(path, GenericHollowObject.class) .usingPath("values._int", int.class); } }
@Test(expected = IllegalArgumentException.class) public void testUnknownSelectType() { HashIndex .from(consumer, DataModel.Consumer.References.class) .selectField("values", Unknown.class) .usingPath("values", DataModel.Consumer.Values.class); }
@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); }