sangyun-han | 888d4c5 | 2016-03-28 16:48:45 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
sangyun-han | 888d4c5 | 2016-03-28 16:48:45 +0900 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; |
| 19 | import org.onosproject.store.service.DistributedQueue; |
| 20 | import org.onosproject.store.service.DistributedQueueBuilder; |
| 21 | import org.onosproject.store.service.Serializer; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | import static com.google.common.base.Preconditions.checkState; |
| 25 | |
| 26 | /** |
| 27 | * Default implementation of a {@code DistributedQueueBuilder}. |
| 28 | * |
| 29 | * @param <E> queue entry type |
| 30 | */ |
Madan Jampani | 832686d | 2016-04-04 21:57:26 -0700 | [diff] [blame] | 31 | public class DefaultDistributedQueueBuilder<E> implements DistributedQueueBuilder<E> { |
sangyun-han | 888d4c5 | 2016-03-28 16:48:45 +0900 | [diff] [blame] | 32 | |
| 33 | private final DistributedPrimitiveCreator primitiveCreator; |
| 34 | private String name; |
sangyun-han | 888d4c5 | 2016-03-28 16:48:45 +0900 | [diff] [blame] | 35 | private Serializer serializer; |
| 36 | |
Madan Jampani | 832686d | 2016-04-04 21:57:26 -0700 | [diff] [blame] | 37 | public DefaultDistributedQueueBuilder(DistributedPrimitiveCreator primitiveCreator) { |
sangyun-han | 888d4c5 | 2016-03-28 16:48:45 +0900 | [diff] [blame] | 38 | this.primitiveCreator = primitiveCreator; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public DistributedQueueBuilder<E> withName(String name) { |
| 43 | checkArgument(name != null && !name.isEmpty()); |
| 44 | this.name = name; |
| 45 | return this; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public DistributedQueueBuilder<E> withSerializer(Serializer serializer) { |
| 50 | checkArgument(serializer != null); |
| 51 | this.serializer = serializer; |
| 52 | return this; |
| 53 | } |
| 54 | |
sangyun-han | 888d4c5 | 2016-03-28 16:48:45 +0900 | [diff] [blame] | 55 | private boolean validInputs() { |
| 56 | return name != null && serializer != null; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public DistributedQueue<E> build() { |
| 61 | checkState(validInputs()); |
| 62 | return primitiveCreator.newDistributedQueue(name, serializer); |
| 63 | } |
| 64 | } |