Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 1 | package org.onosproject.store.consistent.impl; |
| 2 | |
| 3 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 4 | import org.onosproject.store.service.AtomicCounter; |
| 5 | import org.onosproject.store.service.AtomicCounterBuilder; |
| 6 | |
| 7 | import static com.google.common.base.Preconditions.checkArgument; |
| 8 | |
| 9 | /** |
| 10 | * Default implementation of AtomicCounterBuilder. |
| 11 | */ |
| 12 | public class DefaultAtomicCounterBuilder implements AtomicCounterBuilder { |
| 13 | |
| 14 | private String name; |
| 15 | private boolean partitionsEnabled = true; |
| 16 | private final Database partitionedDatabase; |
| 17 | private final Database inMemoryDatabase; |
| 18 | |
| 19 | public DefaultAtomicCounterBuilder(Database inMemoryDatabase, Database partitionedDatabase) { |
| 20 | this.inMemoryDatabase = inMemoryDatabase; |
| 21 | this.partitionedDatabase = partitionedDatabase; |
| 22 | } |
| 23 | |
| 24 | @Override |
| 25 | public AtomicCounterBuilder withName(String name) { |
| 26 | checkArgument(name != null && !name.isEmpty()); |
| 27 | this.name = name; |
| 28 | return this; |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public AtomicCounterBuilder withPartitionsDisabled() { |
| 33 | partitionsEnabled = false; |
| 34 | return this; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public AtomicCounter build() { |
| 39 | Database database = partitionsEnabled ? partitionedDatabase : inMemoryDatabase; |
| 40 | return new DefaultAtomicCounter(name, database); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public AsyncAtomicCounter buildAsyncCounter() { |
| 45 | Database database = partitionsEnabled ? partitionedDatabase : inMemoryDatabase; |
| 46 | return new DefaultAsyncAtomicCounter(name, database); |
| 47 | } |
| 48 | } |