blob: 88c7d1486b753354f0db839eef58ee2349ee75a7 [file] [log] [blame]
Aaron Kruglikov92511f22015-10-12 14:39:04 -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 */
16
17package org.onosproject.persistence.impl;
18
19import org.mapdb.DB;
20import org.onosproject.persistence.PersistentMapBuilder;
21import org.onosproject.store.service.Serializer;
22
23import java.util.Map;
24
25import static com.google.common.base.Preconditions.checkArgument;
26import 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 */
31public 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}