Madan Jampani | 63c659f | 2015-06-11 00:52:58 -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 | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 17 | |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 18 | import org.onosproject.store.service.DistributedQueue; |
| 19 | import org.onosproject.store.service.DistributedQueueBuilder; |
| 20 | import org.onosproject.store.service.Serializer; |
| 21 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 22 | import static com.google.common.base.Preconditions.checkArgument; |
| 23 | import static com.google.common.base.Preconditions.checkState; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Default implementation of a {@code DistributedQueueBuilder}. |
| 27 | * |
| 28 | * @param <E> queue entry type |
| 29 | */ |
| 30 | public class DefaultDistributedQueueBuilder<E> implements DistributedQueueBuilder<E> { |
| 31 | |
| 32 | private Serializer serializer; |
| 33 | private String name; |
| 34 | private boolean persistenceEnabled = true; |
| 35 | private final DatabaseManager databaseManager; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 36 | private boolean metering = true; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 37 | |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 38 | public DefaultDistributedQueueBuilder(DatabaseManager databaseManager) { |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 39 | this.databaseManager = databaseManager; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public DistributedQueueBuilder<E> withName(String name) { |
| 44 | checkArgument(name != null && !name.isEmpty()); |
| 45 | this.name = name; |
| 46 | return this; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public DistributedQueueBuilder<E> withSerializer(Serializer serializer) { |
| 51 | checkArgument(serializer != null); |
| 52 | this.serializer = serializer; |
| 53 | return this; |
| 54 | } |
| 55 | |
| 56 | @Override |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 57 | public DistributedQueueBuilder<E> withMeteringDisabled() { |
| 58 | metering = false; |
| 59 | return this; |
| 60 | } |
| 61 | |
| 62 | @Override |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 63 | public DistributedQueueBuilder<E> withPersistenceDisabled() { |
| 64 | persistenceEnabled = false; |
| 65 | return this; |
| 66 | } |
| 67 | |
| 68 | private boolean validInputs() { |
| 69 | return name != null && serializer != null; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public DistributedQueue<E> build() { |
| 74 | checkState(validInputs()); |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 75 | return new DefaultDistributedQueue<>( |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 76 | name, |
| 77 | persistenceEnabled ? databaseManager.partitionedDatabase : databaseManager.inMemoryDatabase, |
| 78 | serializer, |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 79 | metering); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 80 | } |
| 81 | } |