blob: 20bfd5f90779165954d816e9e729e6777c92d155 [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 */
16package org.onosproject.store.consistent.impl;
17
Madan Jampanidfde6ba2016-01-13 21:36:09 -080018import java.util.concurrent.CompletableFuture;
19import java.util.concurrent.TimeUnit;
20import org.onosproject.store.service.AsyncAtomicValue;
Madan Jampani762246d2015-07-21 15:40:59 -070021import org.onosproject.store.service.AtomicValue;
Madan Jampani762246d2015-07-21 15:40:59 -070022import org.onosproject.store.service.AtomicValueEventListener;
Madan Jampanidfde6ba2016-01-13 21:36:09 -080023import org.onosproject.store.service.StorageException;
Madan Jampani762246d2015-07-21 15:40:59 -070024
Madan Jampanidfde6ba2016-01-13 21:36:09 -080025import com.google.common.util.concurrent.Futures;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070026
Madan Jampani762246d2015-07-21 15:40:59 -070027/**
Madan Jampanidfde6ba2016-01-13 21:36:09 -080028 * Default implementation of {@link AtomicValue}.
Madan Jampani762246d2015-07-21 15:40:59 -070029 *
30 * @param <V> value type
31 */
32public class DefaultAtomicValue<V> implements AtomicValue<V> {
33
Madan Jampanidfde6ba2016-01-13 21:36:09 -080034 private static final int OPERATION_TIMEOUT_MILLIS = 5000;
35 private final AsyncAtomicValue<V> asyncValue;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070036
Madan Jampanidfde6ba2016-01-13 21:36:09 -080037 public DefaultAtomicValue(AsyncAtomicValue<V> asyncValue) {
38 this.asyncValue = asyncValue;
Madan Jampani762246d2015-07-21 15:40:59 -070039 }
40
41 @Override
42 public boolean compareAndSet(V expect, V update) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080043 return complete(asyncValue.compareAndSet(expect, update));
Madan Jampani762246d2015-07-21 15:40:59 -070044 }
45
46 @Override
47 public V get() {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080048 return complete(asyncValue.get());
Madan Jampani762246d2015-07-21 15:40:59 -070049 }
50
51 @Override
52 public V getAndSet(V value) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080053 return complete(asyncValue.getAndSet(value));
Madan Jampani762246d2015-07-21 15:40:59 -070054 }
55
56 @Override
57 public void set(V value) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080058 complete(asyncValue.set(value));
Madan Jampani762246d2015-07-21 15:40:59 -070059 }
60
61 @Override
62 public void addListener(AtomicValueEventListener<V> listener) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080063 complete(asyncValue.addListener(listener));
Madan Jampani762246d2015-07-21 15:40:59 -070064 }
65
66 @Override
67 public void removeListener(AtomicValueEventListener<V> listener) {
Madan Jampanidfde6ba2016-01-13 21:36:09 -080068 complete(asyncValue.removeListener(listener));
Madan Jampani762246d2015-07-21 15:40:59 -070069 }
70
Madan Jampanidfde6ba2016-01-13 21:36:09 -080071 private static <V> V complete(CompletableFuture<V> future) {
72 return Futures.getChecked(future, StorageException.class, OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
Madan Jampani762246d2015-07-21 15:40:59 -070073 }
Madan Jampanidfde6ba2016-01-13 21:36:09 -080074}