blob: 494445ea317fb1059ad575b80e7c3300e331412f [file] [log] [blame]
Jian Lifc5ca932015-12-10 12:11:57 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Lifc5ca932015-12-10 12:11:57 -08003 *
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 */
Madan Jampanif4c88502016-01-21 12:35:36 -080016package org.onosproject.store.primitives.impl;
Jian Lifc5ca932015-12-10 12:11:57 -080017
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
Sho SHIMIZUee5d1212016-08-12 17:16:38 -070035 private static final Boolean PRESENT = Boolean.TRUE;
Jian Lifc5ca932015-12-10 12:11:57 -080036
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}