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 | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 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 | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 56 | return new DefaultAtomicCounter(buildAsyncCounter()); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public AsyncAtomicCounter buildAsyncCounter() { |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 61 | validateInputs(); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 62 | Database database = partitionsEnabled ? partitionedDatabase : inMemoryDatabase; |
Madan Jampani | a29c677 | 2015-08-17 13:17:07 -0700 | [diff] [blame] | 63 | return new DefaultAsyncAtomicCounter(name, database, metering); |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 67 | public AtomicCounterBuilder withMeteringDisabled() { |
| 68 | metering = false; |
| 69 | return this; |
| 70 | } |
| 71 | |
Madan Jampani | f4d58f3 | 2015-06-05 17:38:22 -0700 | [diff] [blame] | 72 | private void validateInputs() { |
Madan Jampani | a29c677 | 2015-08-17 13:17:07 -0700 | [diff] [blame] | 73 | checkState(name != null, "name must be specified"); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 74 | } |
| 75 | } |