blob: 7e05367aca441f1b3111c349c052e6172aabb7df [file] [log] [blame]
sangyun-han888d4c52016-03-28 16:48:45 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
sangyun-han888d4c52016-03-28 16:48:45 +09003 *
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 */
16package org.onosproject.store.primitives.impl;
17
18import org.onosproject.store.primitives.DistributedPrimitiveCreator;
19import org.onosproject.store.service.DistributedQueue;
20import org.onosproject.store.service.DistributedQueueBuilder;
21import org.onosproject.store.service.Serializer;
22
23import static com.google.common.base.Preconditions.checkArgument;
24import 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 Jampani832686d2016-04-04 21:57:26 -070031public class DefaultDistributedQueueBuilder<E> implements DistributedQueueBuilder<E> {
sangyun-han888d4c52016-03-28 16:48:45 +090032
33 private final DistributedPrimitiveCreator primitiveCreator;
34 private String name;
sangyun-han888d4c52016-03-28 16:48:45 +090035 private Serializer serializer;
36
Madan Jampani832686d2016-04-04 21:57:26 -070037 public DefaultDistributedQueueBuilder(DistributedPrimitiveCreator primitiveCreator) {
sangyun-han888d4c52016-03-28 16:48:45 +090038 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-han888d4c52016-03-28 16:48:45 +090055 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}