/** * Create a new builder for a keyed object map. * @param ex The key extractor. * @param <T> The keyed object type. * @return A builder for a keyed object map. */ public static <T> KeyedObjectMapBuilder<T> newBuilder(KeyExtractor<? super T> ex) { return new KeyedObjectMapBuilder<>(ex); }
@Override public EntityCollectionBuilder add(Entity e, boolean replace) { Preconditions.checkState(store != null, "build() already called"); Preconditions.checkArgument(e.getType().equals(type)); if (!replace && store.containsKey(e.getId())) { return this; } store.add(e); hasher.putInt(e.hashCode()); for (EntityIndexBuilder ib: indexBuilders.values()) { ib.add(e); } return this; }
public KeyedObjectMapBuilder<T> add(T... items) { for (T item: items) { add(item); } return this; }
@Override public MapEntityCollection build() { Preconditions.checkState(store != null, "build() already called"); ImmutableMap.Builder<String,EntityIndex> indexes = ImmutableMap.builder(); for (Map.Entry<String,EntityIndexBuilder> e: indexBuilders.entrySet()) { indexes.put(e.getKey(), e.getValue().build()); } KeyedObjectMap<Entity> map = store.build(); ImmutableMap<String, EntityIndex> idxMap = indexes.build(); logger.debug("built collection of {} entities with type {} and {} indexes", map.size(), type, idxMap.size()); store = null; indexBuilders = null; return new MapEntityCollection(type, map, idxMap, hasher.hash()); } }
@Override public Collection<Entity> entities() { return store.objects(); }
public KeyedObjectMapBuilder<T> addAll(Iterable<? extends T> items) { for (T item: items) { add(item); } return this; }
@Override public <T> EntityCollectionBuilder addIndex(TypedName<T> attribute) { Preconditions.checkState(indexBuilders != null, "build() already called"); if (indexBuilders.containsKey(attribute.getName())) { // already have that index return this; } EntityIndexBuilder ib = EntityIndexBuilder.create(attribute); indexBuilders.put(attribute.getName(), ib); for (Entity e: store.build()) { ib.add(e); } return this; }
@Override public Collection<Entity> entities() { return store.objects(); }
@Override public EntityIndex build() { Preconditions.checkState(entityLists != null, "build() already called"); // arrange compact storage of the index KeyedObjectMapBuilder<IdBox<ImmutableList<Entity>>> bld = KeyedObjectMap.newBuilder(); for (Long2ObjectMap.Entry<ImmutableList.Builder<Entity>> entry: entityLists.long2ObjectEntrySet()) { long value = entry.getLongKey(); bld.add(IdBox.create(value, entry.getValue().build())); entry.setValue(null); } entityLists = null; return new LongEntityIndex(bld.build()); } }
@Override public EntityCollectionBuilder add(Entity e, boolean replace) { Preconditions.checkState(store != null, "build() already called"); Preconditions.checkArgument(e.getType().equals(type)); if (!replace && store.containsKey(e.getId())) { return this; } store.add(e); hasher.putInt(e.hashCode()); for (EntityIndexBuilder ib: indexBuilders.values()) { ib.add(e); } return this; }
public KeyedObjectMapBuilder<T> add(T... items) { for (T item: items) { add(item); } return this; }
@Override public MapEntityCollection build() { Preconditions.checkState(store != null, "build() already called"); ImmutableMap.Builder<String,EntityIndex> indexes = ImmutableMap.builder(); for (Map.Entry<String,EntityIndexBuilder> e: indexBuilders.entrySet()) { indexes.put(e.getKey(), e.getValue().build()); } KeyedObjectMap<Entity> map = store.build(); ImmutableMap<String, EntityIndex> idxMap = indexes.build(); logger.debug("built collection of {} entities with type {} and {} indexes", map.size(), type, idxMap.size()); store = null; indexBuilders = null; return new MapEntityCollection(type, map, idxMap, hasher.hash()); } }
/** * Create a new builder for a keyed object map over a self-keying type. * @param <T> The keyed object type. * @return A builder for a keyed object map. */ public static <T extends KeyedObject> KeyedObjectMapBuilder<T> newBuilder() { return new KeyedObjectMapBuilder<>(Keys.selfExtractor()); }
public KeyedObjectMapBuilder<T> addAll(Iterable<? extends T> items) { for (T item: items) { add(item); } return this; }
@Override public <T> EntityCollectionBuilder addIndex(TypedName<T> attribute) { Preconditions.checkState(indexBuilders != null, "build() already called"); if (indexBuilders.containsKey(attribute.getName())) { // already have that index return this; } EntityIndexBuilder ib = EntityIndexBuilder.create(attribute); indexBuilders.put(attribute.getName(), ib); for (Entity e: store.build()) { ib.add(e); } return this; }
/** * Create a new builder for a keyed object map. * @param ex The key extractor. * @param <T> The keyed object type. * @return A builder for a keyed object map. */ public static <T> KeyedObjectMapBuilder<T> newBuilder(KeyExtractor<? super T> ex) { return new KeyedObjectMapBuilder<>(ex); }
@Override public EntityIndex build() { Preconditions.checkState(entityLists != null, "build() already called"); // arrange compact storage of the index KeyedObjectMapBuilder<IdBox<ImmutableList<Entity>>> bld = KeyedObjectMap.newBuilder(); for (Long2ObjectMap.Entry<ImmutableList.Builder<Entity>> entry: entityLists.long2ObjectEntrySet()) { long value = entry.getLongKey(); bld.add(IdBox.create(value, entry.getValue().build())); entry.setValue(null); } entityLists = null; return new LongEntityIndex(bld.build()); } }
/** * Create a new builder for a keyed object map over a self-keying type. * @param <T> The keyed object type. * @return A builder for a keyed object map. */ public static <T extends KeyedObject> KeyedObjectMapBuilder<T> newBuilder() { return new KeyedObjectMapBuilder<>(Keys.selfExtractor()); }