Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 1 | package org.onosproject.store.consistent.impl; |
| 2 | |
| 3 | import static com.google.common.base.Preconditions.checkArgument; |
| 4 | import static com.google.common.base.Preconditions.checkState; |
| 5 | |
| 6 | import org.onosproject.store.service.AsyncConsistentMap; |
| 7 | import org.onosproject.store.service.ConsistentMap; |
| 8 | import org.onosproject.store.service.ConsistentMapBuilder; |
| 9 | import org.onosproject.store.service.Serializer; |
| 10 | |
| 11 | /** |
| 12 | * Default Consistent Map builder. |
| 13 | * |
| 14 | * @param <K> type for map key |
| 15 | * @param <V> type for map value |
| 16 | */ |
| 17 | public class DefaultConsistentMapBuilder<K, V> implements ConsistentMapBuilder<K, V> { |
| 18 | |
| 19 | private Serializer serializer; |
| 20 | private String name; |
| 21 | private boolean partitionsEnabled = true; |
| 22 | private final Database partitionedDatabase; |
| 23 | private final Database inMemoryDatabase; |
| 24 | |
| 25 | public DefaultConsistentMapBuilder(Database inMemoryDatabase, Database partitionedDatabase) { |
| 26 | this.inMemoryDatabase = inMemoryDatabase; |
| 27 | this.partitionedDatabase = partitionedDatabase; |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public ConsistentMapBuilder<K, V> withName(String name) { |
| 32 | checkArgument(name != null && !name.isEmpty()); |
| 33 | this.name = name; |
| 34 | return this; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public ConsistentMapBuilder<K, V> withSerializer(Serializer serializer) { |
| 39 | checkArgument(serializer != null); |
| 40 | this.serializer = serializer; |
| 41 | return this; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public ConsistentMapBuilder<K, V> withPartitionsDisabled() { |
| 46 | partitionsEnabled = false; |
| 47 | return this; |
| 48 | } |
| 49 | |
| 50 | private boolean validInputs() { |
| 51 | return name != null && serializer != null; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public ConsistentMap<K, V> build() { |
| 56 | checkState(validInputs()); |
| 57 | return new DefaultConsistentMap<>( |
| 58 | name, |
| 59 | partitionsEnabled ? partitionedDatabase : inMemoryDatabase, |
| 60 | serializer); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public AsyncConsistentMap<K, V> buildAsyncMap() { |
| 65 | checkState(validInputs()); |
| 66 | return new DefaultAsyncConsistentMap<>( |
| 67 | name, |
| 68 | partitionsEnabled ? partitionedDatabase : inMemoryDatabase, |
| 69 | serializer); |
| 70 | } |
| 71 | } |