Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [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 | |
| 17 | package org.onosproject.persistence.impl; |
| 18 | |
| 19 | import org.mapdb.DB; |
| 20 | import org.onosproject.persistence.PersistentSetBuilder; |
| 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.checkNotNull; |
| 25 | |
| 26 | /** |
| 27 | * Default builder for persistent sets stored in the mapDB local database via the persistence service.. |
| 28 | */ |
| 29 | public class DefaultPersistentSetBuilder<E> implements PersistentSetBuilder<E> { |
| 30 | |
| 31 | private final DB localDB; |
| 32 | |
| 33 | private String name = null; |
| 34 | |
| 35 | private Serializer serializer = null; |
| 36 | |
| 37 | public DefaultPersistentSetBuilder(DB localDB) { |
| 38 | this.localDB = checkNotNull(localDB, "The local database cannot be null."); |
| 39 | } |
| 40 | |
| 41 | public PersistentSetBuilder<E> withName(String name) { |
| 42 | this.name = PersistenceManager.SET_PREFIX + checkNotNull(name); |
| 43 | return this; |
| 44 | } |
| 45 | |
| 46 | public PersistentSetBuilder<E> withSerializer(Serializer serializer) { |
| 47 | checkArgument(this.serializer == null); |
| 48 | checkNotNull(serializer); |
| 49 | this.serializer = serializer; |
| 50 | return this; |
| 51 | } |
| 52 | |
| 53 | public PersistentSet<E> build() { |
| 54 | checkNotNull(name, "The name must be assigned."); |
| 55 | checkNotNull(serializer, "The serializer must be assigned."); |
| 56 | |
| 57 | return new PersistentSet<E>(serializer, localDB, name); |
| 58 | } |
| 59 | } |