UniqueIndex extends AbstractAttributeIndex<A,O> implements OnHeapTypeIndex { public static <A, O> UniqueIndex<A, O> onAttribute(Attribute<O, A> attribute) { return onAttribute(new DefaultIndexMapFactory<A, O>(), attribute); } protected  UniqueIndex(Factory<ConcurrentMap<A,O>> indexMapFactory, Attribute<O, A> attribute); @Override boolean supportsQuery(Query<O> query, QueryOptions queryOptions); @Override boolean isMutable(); @Override boolean isQuantized(); @Override Index<O> getEffectiveIndex(); @Override ResultSet<O> retrieve(final Query<O> query, final QueryOptions queryOptions); @Override boolean addAll(ObjectSet<O> objectSet, QueryOptions queryOptions); @Override boolean removeAll(ObjectSet<O> objectSet, QueryOptions queryOptions); @Override void init(ObjectStore<O> objectStore, QueryOptions queryOptions); @Override void destroy(QueryOptions queryOptions); @Override void clear(QueryOptions queryOptions); static UniqueIndex<A, O> onAttribute(Attribute<O, A> attribute); static UniqueIndex<A, O> onAttribute(Factory<ConcurrentMap<A, O>> indexMapFactory, Attribute<O, A> attribute);  }
@Test(expected = UniqueIndex.UniqueConstraintViolatedException.class) public void testDuplicateObjectDetection_SimpleAttribute() { IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>(); cars.addIndex(UniqueIndex.onAttribute(Car.CAR_ID)); cars.add(new Car(1, "ford focus", "great condition, low mileage", Arrays.asList("spare tyre", "sunroof"))); cars.add(new Car(2, "ford taurus", "dirty and unreliable, flat tyre", Arrays.asList("spare tyre", "radio"))); cars.add(new Car(3, "honda civic", "has a flat tyre and high mileage", Arrays.asList("radio"))); cars.add(new Car(2, "some other car", "foo", Arrays.asList("bar"))); }
@Test(expected = UniqueIndex.UniqueConstraintViolatedException.class) public void testDuplicateObjectDetection_MultiValueAttribute() { IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>(); cars.addIndex(UniqueIndex.onAttribute(Car.FEATURES)); cars.add(new Car(1, "ford focus", "foo", Arrays.asList("spare tyre", "sunroof"))); cars.add(new Car(2, "ford taurus", "bar", Arrays.asList("radio", "cd player"))); cars.add(new Car(3, "honda civic", "baz", Arrays.asList("cd player", "bluetooth"))); }