blob: 45808ba0ccd4a91f91fd423dd9aa469595120fa2 [file] [log] [blame]
Madan Jampani762246d2015-07-21 15:40:59 -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 */
Madan Jampanif4c88502016-01-21 12:35:36 -080016package org.onosproject.store.primitives.impl;
Madan Jampani762246d2015-07-21 15:40:59 -070017
Madan Jampanidfde6ba2016-01-13 21:36:09 -080018import java.util.concurrent.CompletableFuture;
19import java.util.concurrent.TimeUnit;
Madan Jampania090a112016-01-18 16:38:17 -080020
Madan Jampanidfde6ba2016-01-13 21:36:09 -080021import org.onosproject.store.service.AsyncAtomicValue;
Madan Jampani762246d2015-07-21 15:40:59 -070022import org.onosproject.store.service.AtomicValue;
Madan Jampani762246d2015-07-21 15:40:59 -070023import org.onosproject.store.service.AtomicValueEventListener;
Madan Jampanidfde6ba2016-01-13 21:36:09 -080024import org.onosproject.store.service.StorageException;
Madan Jampania090a112016-01-18 16:38:17 -080025import org.onosproject.store.service.Synchronous;
Madan Jampani762246d2015-07-21 15:40:59 -070026
Madan Jampanidfde6ba2016-01-13 21:36:09 -080027import com.google.common.util.concurrent.Futures;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070028
Madan Jampani762246d2015-07-21 15:40:59 -070029/**
Madan Jampanidfde6ba2016-01-13 21:36:09 -080030 * Default implementation of {@link AtomicValue}.
Madan Jampani762246d2015-07-21 15:40:59 -070031 *
32 * @param <V> value type
33 */
Madan Jampania090a112016-01-18 16:38:17 -080034public class DefaultAtomicValue<V> extends Synchronous<AsyncAtomicValue<V>> implements AtomicValue<V> {
Madan Jampani762246d2015-07-21 15:40:59 -070035
Madan Jampanidfde6ba2016-01-13 21:36:09 -080036 private static final int OPERATION_TIMEOUT_MILLIS = 5000;
37 private final AsyncAtomicValue<V> asyncValue;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070038
Madan Jampanidfde6ba2016-01-13 21:36:09 -080039 public DefaultAtomicValue(AsyncAtomicValue<V> asyncValue) {
Madan Jampania090a112016-01-18 16:38:17 -080040 super(asyncValue);
Madan Jampanidfde6ba2016-01-13 21:36:09 -080041 this.asyncValue = asyncValue;
Madan Jampani762246d2015-07-21 15:40:59 -070042 }
43
44 @Override
45 public boolean compareAndSet(V expect, V update) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080046 return complete(asyncValue.compareAndSet(expect, update));
Madan Jampani762246d2015-07-21 15:40:59 -070047 }
48
49 @Override
50 public V get() {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080051 return complete(asyncValue.get());
Madan Jampani762246d2015-07-21 15:40:59 -070052 }
53
54 @Override
55 public V getAndSet(V value) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080056 return complete(asyncValue.getAndSet(value));
Madan Jampani762246d2015-07-21 15:40:59 -070057 }
58
59 @Override
60 public void set(V value) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080061 complete(asyncValue.set(value));
Madan Jampani762246d2015-07-21 15:40:59 -070062 }
63
64 @Override
65 public void addListener(AtomicValueEventListener<V> listener) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080066 complete(asyncValue.addListener(listener));
Madan Jampani762246d2015-07-21 15:40:59 -070067 }
68
69 @Override
70 public void removeListener(AtomicValueEventListener<V> listener) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080071 complete(asyncValue.removeListener(listener));
Madan Jampani762246d2015-07-21 15:40:59 -070072 }
73
Madan Jampanidfde6ba2016-01-13 21:36:09 -080074 private static <V> V complete(CompletableFuture<V> future) {
75 return Futures.getChecked(future, StorageException.class, OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
Madan Jampani762246d2015-07-21 15:40:59 -070076 }
Madan Jampanidfde6ba2016-01-13 21:36:09 -080077}