blob: 11fcb8088137a45f61b73795aea04ede602e1b80 [file] [log] [blame]
Jian Lifc5ca932015-12-10 12:11:57 -08001/*
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
18
19import org.onosproject.store.service.Serializer;
20import org.onosproject.store.service.TransactionContext;
21import org.onosproject.store.service.TransactionalMap;
22import org.onosproject.store.service.TransactionalSet;
23
24/**
25 * Default TransactionalSet implementation that provides a repeatable reads
26 * transaction isolation level.
27 *
28 * @param <E> element type.
29 */
30public class DefaultTransactionalSet<E> implements TransactionalSet<E> {
31
32 private TransactionalMap<E, Boolean> map;
33
34 // dummy value to associate with an Object in the backing map
35 private static final Boolean PRESENT = new Boolean(true);
36
37 public DefaultTransactionalSet(
38 String name,
39 TransactionContext txContext,
40 Serializer serializer) {
41 map = txContext.getTransactionalMap(name, serializer);
42 }
43
44 @Override
45 public boolean add(E e) {
46 return map.put(e, PRESENT) == null;
47 }
48
49 @Override
50 public boolean remove(E e) {
51 return map.remove(e) != null;
52 }
53
54 @Override
55 public boolean contains(E e) {
56 return map.get(e) != null;
57 }
58}