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.PersistentMapBuilder; |
| 21 | import org.onosproject.store.service.Serializer; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * Default builder for persistent maps stored in the mapDB local database via the persistence service. |
| 30 | */ |
| 31 | public class DefaultPersistentMapBuilder<K, V> implements PersistentMapBuilder<K, V> { |
| 32 | |
| 33 | private final DB localDB; |
| 34 | |
| 35 | private String name = null; |
| 36 | |
| 37 | private Serializer serializer = null; |
| 38 | |
| 39 | |
| 40 | public DefaultPersistentMapBuilder(DB localDB) { |
| 41 | checkNotNull(localDB, "The local database cannot be null."); |
| 42 | this.localDB = localDB; |
| 43 | } |
| 44 | |
| 45 | public PersistentMapBuilder<K, V> withName(String name) { |
| 46 | this.name = PersistenceManager.MAP_PREFIX + checkNotNull(name); |
| 47 | return this; |
| 48 | } |
| 49 | |
| 50 | public PersistentMapBuilder<K, V> withSerializer(Serializer serializer) { |
| 51 | checkArgument(this.serializer == null); |
| 52 | checkNotNull(serializer); |
| 53 | this.serializer = serializer; |
| 54 | return this; |
| 55 | } |
| 56 | |
| 57 | public Map<K, V> build() { |
| 58 | checkNotNull(name, "The name must be assigned."); |
| 59 | checkNotNull(serializer, "The key serializer must be assigned."); |
| 60 | |
| 61 | return new PersistentMap<K, V>(serializer, localDB, name); |
| 62 | } |
| 63 | } |