/** * Creates a {@link UniqueKeyIndex} for matching with field paths and types declared by * {@link FieldPath} annotated fields or methods on the given key type. * * @param keyType the key type * @param <Q> the key type * @return a {@code UniqueKeyIndex} * @throws IllegalArgumentException if the key type declares one or more invalid field paths * or invalid types given resolution of corresponding field paths * @throws IllegalArgumentException if the builder is bound to the primary key of the unique type and * the field paths declared by the key type are not the identical to those declared by the primary key */ public <Q> UniqueKeyIndex<T, Q> usingBean(Class<Q> keyType) { Objects.requireNonNull(keyType); return new UniqueKeyIndex<>(consumer, uniqueType, primaryTypeKey, keyType); }
UniqueKeyIndex( HollowConsumer consumer, Class<T> uniqueType, PrimaryKey primaryTypeKey, List<MatchFieldPathArgumentExtractor<Q>> matchFields) { this.consumer = consumer; this.api = consumer.getAPI(); this.uniqueTypeName = uniqueType.getSimpleName(); this.uniqueTypeExtractor = SelectFieldPathResultExtractor .from(consumer.getAPI().getClass(), consumer.getStateEngine(), uniqueType, "", uniqueType); if (primaryTypeKey != null) { matchFields = validatePrimaryKeyFieldPaths(consumer, uniqueTypeName, primaryTypeKey, matchFields); } this.matchFields = matchFields; this.matchFieldPaths = matchFields.stream() .map(mf -> mf.fieldPath.toString()) .toArray(String[]::new); this.hpki = new HollowPrimaryKeyIndex(consumer.getStateEngine(), uniqueTypeName, matchFieldPaths); }
public <T> void test(Class<T> keyType, T key) { UniqueKeyIndex<DataModel.Consumer.TypeWithPrimaryKey, T> pki = UniqueKeyIndex .from(consumer, DataModel.Consumer.TypeWithPrimaryKey.class) .bindToPrimaryKey() .usingBean(keyType); DataModel.Consumer.TypeWithPrimaryKey match = pki.findMatch(key); Assert.assertNotNull(match); Assert.assertEquals(0, match.getOrdinal()); }
@Test(expected = IllegalArgumentException.class) public void test() { UniqueKeyIndex .from(consumer, DataModel.Consumer.Values.class) .usingPath(path, Object.class); } }
@Test(expected = IllegalArgumentException.class) public void testUnknownRootSelectType() { UniqueKeyIndex .from(consumer, ErrorsTest.Unknown.class) .usingPath("values", DataModel.Consumer.Values.class); }
@Test(expected = IllegalArgumentException.class) public void testEmptyMatchPath() { UniqueKeyIndex .from(consumer, DataModel.Consumer.References.class) .usingPath("", DataModel.Consumer.References.class); }
/** * Creates a {@link UniqueKeyIndex} for matching with a single key field path and type. * * @param keyFieldPath the key field path * @param keyFieldType the key type * @param <Q> the key type * @return a {@code UniqueKeyIndex} * @throws IllegalArgumentException if the key field path is empty or invalid * @throws IllegalArgumentException if the key field type is invalid given resolution of the * key field path * @throws IllegalArgumentException if the builder is bound to the primary key of the unique type and * the field path declared by the key type is not identical to the keyFieldPath */ public <Q> UniqueKeyIndex<T, Q> usingPath(String keyFieldPath, Class<Q> keyFieldType) { Objects.requireNonNull(keyFieldPath); if (keyFieldPath.isEmpty()) { throw new IllegalArgumentException("keyFieldPath argument is an empty String"); } Objects.requireNonNull(keyFieldType); return new UniqueKeyIndex<>(consumer, uniqueType, primaryTypeKey, keyFieldPath, keyFieldType); } }
@Test public void testFields() { UniqueKeyIndex<DataModel.Consumer.Values, ValueFieldsQuery> hi = UniqueKeyIndex .from(consumer, DataModel.Consumer.Values.class) .usingBean(MatchOnValuesBeanTest.ValueFieldsQuery.class); DataModel.Consumer.Values r = hi.findMatch(MatchOnValuesBeanTest.ValueFieldsQuery.create()); Assert.assertNotNull(r); Assert.assertEquals(0, r.getOrdinal()); }
@Test(expected = IllegalArgumentException.class) public void testNoPrimaryKey() { UniqueKeyIndex .from(consumer, DataModel.Consumer.References.class) .bindToPrimaryKey() .usingPath("values._int", int.class); } }
@Test public void testMethods() { UniqueKeyIndex<DataModel.Consumer.Values, ValueMethodsQuery> hi = UniqueKeyIndex .from(consumer, DataModel.Consumer.Values.class) .usingBean(MatchOnValuesBeanTest.ValueMethodsQuery.class); DataModel.Consumer.Values r = hi.findMatch(MatchOnValuesBeanTest.ValueMethodsQuery.create()); Assert.assertNotNull(r); Assert.assertEquals(0, r.getOrdinal()); } }
consumer.triggerRefreshTo(v1); UniqueKeyIndex<DataModel.Consumer.TypeWithPrimaryKey, Key> uki = UniqueKeyIndex.from(consumer, DataModel.Consumer.TypeWithPrimaryKey.class) .bindToPrimaryKey() consumer.addRefreshListener(uki); Assert.assertNotNull(uki.findMatch(new Key(1, "1", 2))); Assert.assertNotNull(uki.findMatch(new Key(1, "1", 2))); Assert.assertNotNull(uki.findMatch(new Key(2, "1", 2))); Assert.assertNotNull(uki.findMatch(new Key(1, "1", 2))); Assert.assertNotNull(uki.findMatch(new Key(2, "1", 2))); Assert.assertNull(uki.findMatch(new Key(3, "1", 2)));