### Question:
UniqueIndex extends AbstractAttributeIndex<A,O> implements OnHeapTypeIndex { protected UniqueIndex(Factory<ConcurrentMap<A,O>> indexMapFactory, Attribute<O, A> attribute) { super(attribute, new HashSet<Class<? extends Query>>() {{ add(Equal.class); add(In.class); }}); this.indexMapFactory = indexMapFactory; this.indexMap = indexMapFactory.create(); } 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);  }### Answer:
@Test public void testUniqueIndex() { IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>(); cars.addIndex(UniqueIndex.onAttribute(Car.CAR_ID)); cars.addIndex(HashIndex.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"))); Query<Car> query = equal(Car.CAR_ID, 2); ResultSet<Car> rs = cars.retrieve(query); Assert.assertEquals("should prefer unique index over hash index", UniqueIndex.INDEX_RETRIEVAL_COST, rs.getRetrievalCost()); Assert.assertEquals("should retrieve car 2", 2, rs.uniqueResult().carId); }