Madan Jampani | 619453b | 2015-07-22 23:47:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 16 | package org.onosproject.store.consistent.impl; |
| 17 | |
| 18 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 19 | import org.onosproject.store.service.AtomicCounter; |
| 20 | import org.onosproject.store.service.AtomicCounterBuilder; |
| 21 | |
| 22 | import static com.google.common.base.Preconditions.checkArgument; |
Madan Jampani | a29c677 | 2015-08-17 13:17:07 -0700 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkState; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Default implementation of AtomicCounterBuilder. |
| 27 | */ |
| 28 | public class DefaultAtomicCounterBuilder implements AtomicCounterBuilder { |
| 29 | |
| 30 | private String name; |
| 31 | private boolean partitionsEnabled = true; |
| 32 | private final Database partitionedDatabase; |
| 33 | private final Database inMemoryDatabase; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 34 | private boolean metering = true; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 35 | |
| 36 | public DefaultAtomicCounterBuilder(Database inMemoryDatabase, Database partitionedDatabase) { |
| 37 | this.inMemoryDatabase = inMemoryDatabase; |
| 38 | this.partitionedDatabase = partitionedDatabase; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public AtomicCounterBuilder withName(String name) { |
| 43 | checkArgument(name != null && !name.isEmpty()); |
| 44 | this.name = name; |
| 45 | return this; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public AtomicCounterBuilder withPartitionsDisabled() { |
| 50 | partitionsEnabled = false; |
| 51 | return this; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public AtomicCounter build() { |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 56 | validateInputs(); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 57 | Database database = partitionsEnabled ? partitionedDatabase : inMemoryDatabase; |
Madan Jampani | a29c677 | 2015-08-17 13:17:07 -0700 | [diff] [blame] | 58 | return new DefaultAtomicCounter(name, database, metering); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public AsyncAtomicCounter buildAsyncCounter() { |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 63 | validateInputs(); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 64 | Database database = partitionsEnabled ? partitionedDatabase : inMemoryDatabase; |
Madan Jampani | a29c677 | 2015-08-17 13:17:07 -0700 | [diff] [blame] | 65 | return new DefaultAsyncAtomicCounter(name, database, metering); |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 69 | public AtomicCounterBuilder withMeteringDisabled() { |
| 70 | metering = false; |
| 71 | return this; |
| 72 | } |
| 73 | |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 74 | private void validateInputs() { |
Madan Jampani | a29c677 | 2015-08-17 13:17:07 -0700 | [diff] [blame] | 75 | checkState(name != null, "name must be specified"); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 76 | } |
| 77 | } |