blob: 369707424894b92a107c2745ed5d8954e502c734 [file] [log] [blame]
Madan Jampanib5d72d52015-04-03 16:53:50 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampanib5d72d52015-04-03 16:53:50 -07003 *
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 Jampanie17d3282016-02-03 15:30:57 -080016package org.onosproject.store.primitives;
Madan Jampanib5d72d52015-04-03 16:53:50 -070017
Flavio Castro41b1f3a2015-07-31 13:51:32 -070018import org.onosproject.store.service.AsyncAtomicCounter;
19import org.onosproject.store.service.AtomicCounter;
20import org.onosproject.store.service.StorageException;
Madan Jampania090a112016-01-18 16:38:17 -080021import org.onosproject.store.service.Synchronous;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070022
Madan Jampanib5d72d52015-04-03 16:53:50 -070023import java.util.concurrent.CompletableFuture;
24import java.util.concurrent.ExecutionException;
25import java.util.concurrent.TimeUnit;
26import java.util.concurrent.TimeoutException;
27
Madan Jampanib5d72d52015-04-03 16:53:50 -070028/**
Madan Jampanie17d3282016-02-03 15:30:57 -080029 * Default implementation for a {@code AtomicCounter} backed by a {@link AsyncAtomicCounter}.
Madan Jampanib5d72d52015-04-03 16:53:50 -070030 */
Madan Jampania090a112016-01-18 16:38:17 -080031public class DefaultAtomicCounter extends Synchronous<AsyncAtomicCounter> implements AtomicCounter {
Madan Jampanib5d72d52015-04-03 16:53:50 -070032
Madan Jampanib5d72d52015-04-03 16:53:50 -070033 private final AsyncAtomicCounter asyncCounter;
Madan Jampanie17d3282016-02-03 15:30:57 -080034 private final long operationTimeoutMillis;
Madan Jampanib5d72d52015-04-03 16:53:50 -070035
Madan Jampanie17d3282016-02-03 15:30:57 -080036 public DefaultAtomicCounter(AsyncAtomicCounter asyncCounter, long operationTimeoutMillis) {
Madan Jampania090a112016-01-18 16:38:17 -080037 super(asyncCounter);
38 this.asyncCounter = asyncCounter;
Madan Jampanie17d3282016-02-03 15:30:57 -080039 this.operationTimeoutMillis = operationTimeoutMillis;
Madan Jampanib5d72d52015-04-03 16:53:50 -070040 }
41
42 @Override
43 public long incrementAndGet() {
44 return complete(asyncCounter.incrementAndGet());
45 }
46
47 @Override
Madan Jampani04aeb452015-05-02 16:12:24 -070048 public long getAndIncrement() {
49 return complete(asyncCounter.getAndIncrement());
50 }
51
52 @Override
53 public long getAndAdd(long delta) {
54 return complete(asyncCounter.getAndAdd(delta));
55 }
56
57 @Override
58 public long addAndGet(long delta) {
59 return complete(asyncCounter.getAndAdd(delta));
60 }
61
62 @Override
andreafd912ac2015-10-02 14:58:35 -070063 public void set(long value) {
64 complete(asyncCounter.set(value));
65 }
66
67 @Override
Aaron Kruglikov82fd6322015-10-06 12:02:46 -070068 public boolean compareAndSet(long expectedValue, long updateValue) {
69 return complete(asyncCounter.compareAndSet(expectedValue, updateValue));
70 }
71
72 @Override
Madan Jampanib5d72d52015-04-03 16:53:50 -070073 public long get() {
74 return complete(asyncCounter.get());
75 }
76
Madan Jampanie17d3282016-02-03 15:30:57 -080077 private <T> T complete(CompletableFuture<T> future) {
Madan Jampanib5d72d52015-04-03 16:53:50 -070078 try {
Madan Jampanie17d3282016-02-03 15:30:57 -080079 return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS);
Madan Jampanib5d72d52015-04-03 16:53:50 -070080 } catch (InterruptedException e) {
81 Thread.currentThread().interrupt();
82 throw new StorageException.Interrupted();
83 } catch (TimeoutException e) {
84 throw new StorageException.Timeout();
85 } catch (ExecutionException e) {
86 throw new StorageException(e.getCause());
87 }
88 }
89}