Madan Jampani | 08706ce | 2015-04-01 14:49:28 -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 | */ |
| 16 | package org.onosproject.store.consistent.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | import static com.google.common.base.Preconditions.checkState; |
| 21 | |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import org.onosproject.store.service.Serializer; |
| 25 | import org.onosproject.store.service.SetBuilder; |
| 26 | |
| 27 | /** |
| 28 | * Default Set builder. |
| 29 | * |
| 30 | * @param <E> type for set elements |
| 31 | */ |
| 32 | public class DefaultSetBuilder<E> implements SetBuilder<E> { |
| 33 | |
| 34 | private Serializer serializer; |
| 35 | private String name; |
| 36 | private final Database database; |
| 37 | |
| 38 | public DefaultSetBuilder(Database database) { |
| 39 | this.database = checkNotNull(database); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public SetBuilder<E> withName(String name) { |
| 44 | checkArgument(name != null && !name.isEmpty()); |
| 45 | this.name = name; |
| 46 | return this; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public SetBuilder<E> withSerializer(Serializer serializer) { |
| 51 | checkArgument(serializer != null); |
| 52 | this.serializer = serializer; |
| 53 | return this; |
| 54 | } |
| 55 | |
| 56 | private boolean validInputs() { |
| 57 | return name != null && serializer != null; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public Set<E> build() { |
| 62 | checkState(validInputs()); |
| 63 | return new DefaultDistributedSet<>(name, database, serializer); |
| 64 | } |
| 65 | } |