Jian Li | fc5ca93 | 2015-12-10 12:11:57 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | fc5ca93 | 2015-12-10 12:11:57 -0800 | [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 | */ |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
Jian Li | fc5ca93 | 2015-12-10 12:11:57 -0800 | [diff] [blame] | 17 | |
| 18 | |
| 19 | import org.onosproject.store.service.Serializer; |
| 20 | import org.onosproject.store.service.TransactionContext; |
| 21 | import org.onosproject.store.service.TransactionalMap; |
| 22 | import 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 | */ |
| 30 | public 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 | } |