blob: 566953f0245ca3ccd7c88f7004ab6d7d3f202048 [file] [log] [blame]
Madan Jampani08706ce2015-04-01 14:49:28 -07001/*
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 */
16package org.onosproject.store.consistent.impl;
17
18import static com.google.common.base.Preconditions.checkArgument;
19import static com.google.common.base.Preconditions.checkNotNull;
20import static com.google.common.base.Preconditions.checkState;
21
22import java.util.Set;
23
24import org.onosproject.store.service.Serializer;
25import org.onosproject.store.service.SetBuilder;
26
27/**
28 * Default Set builder.
29 *
30 * @param <E> type for set elements
31 */
32public class DefaultSetBuilder<E> implements SetBuilder<E> {
33
34 private Serializer serializer;
35 private String name;
36 private final Database database;
Madan Jampani02b7fb82015-05-01 13:01:20 -070037 private boolean readOnly;
Madan Jampani08706ce2015-04-01 14:49:28 -070038
39 public DefaultSetBuilder(Database database) {
40 this.database = checkNotNull(database);
41 }
42
43 @Override
44 public SetBuilder<E> withName(String name) {
45 checkArgument(name != null && !name.isEmpty());
46 this.name = name;
47 return this;
48 }
49
50 @Override
51 public SetBuilder<E> withSerializer(Serializer serializer) {
52 checkArgument(serializer != null);
53 this.serializer = serializer;
54 return this;
55 }
56
Madan Jampani02b7fb82015-05-01 13:01:20 -070057 @Override
58 public SetBuilder<E> withUpdatesDisabled() {
59 readOnly = true;
60 return this;
61 }
62
Madan Jampani08706ce2015-04-01 14:49:28 -070063 private boolean validInputs() {
64 return name != null && serializer != null;
65 }
66
67 @Override
68 public Set<E> build() {
69 checkState(validInputs());
Madan Jampani02b7fb82015-05-01 13:01:20 -070070 return new DefaultDistributedSet<>(name, database, serializer, readOnly);
Madan Jampani08706ce2015-04-01 14:49:28 -070071 }
72}