blob: eff8e77253f991971d4d6aba2af5f7ffffca0707 [file] [log] [blame]
sangyun-han888d4c52016-03-28 16:48:45 +09001/*
2 * Copyright 2016 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 */
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 */
31public class NewDefaultDistributedQueueBuilder<E> implements DistributedQueueBuilder<E> {
32
33 private final DistributedPrimitiveCreator primitiveCreator;
34 private String name;
35 private boolean persistenceEnabled = true;
36 private boolean metering = true;
37 private Serializer serializer;
38
39 public NewDefaultDistributedQueueBuilder(DistributedPrimitiveCreator primitiveCreator) {
40 this.primitiveCreator = primitiveCreator;
41 }
42
43 @Override
44 public DistributedQueueBuilder<E> withName(String name) {
45 checkArgument(name != null && !name.isEmpty());
46 this.name = name;
47 return this;
48 }
49
50 @Override
51 public DistributedQueueBuilder<E> withSerializer(Serializer serializer) {
52 checkArgument(serializer != null);
53 this.serializer = serializer;
54 return this;
55 }
56
57 @Override
58 public DistributedQueueBuilder<E> withMeteringDisabled() {
59 metering = false;
60 return this;
61 }
62
63 @Override
64 public DistributedQueueBuilder<E> withPersistenceDisabled() {
65 persistenceEnabled = false;
66 return this;
67 }
68
69 private boolean validInputs() {
70 return name != null && serializer != null;
71 }
72
73 @Override
74 public DistributedQueue<E> build() {
75 checkState(validInputs());
76 return primitiveCreator.newDistributedQueue(name, serializer);
77 }
78}