blob: 8c577df994f7218af7c12d841bb9103853936b23 [file] [log] [blame]
Ray Milkey24e60b32015-08-12 11:39:54 -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.service;
17
Ray Milkey24e60b32015-08-12 11:39:54 -070018import java.util.concurrent.atomic.AtomicLong;
19
20/**
21 * Test implementation of atomic counter.
22 */
23public final class TestAtomicCounter implements AtomicCounter {
24 final AtomicLong value;
25
26 private TestAtomicCounter() {
27 value = new AtomicLong();
28 }
29
30 @Override
31 public long incrementAndGet() {
32 return value.incrementAndGet();
33 }
34
35 @Override
36 public long getAndIncrement() {
37 return value.getAndIncrement();
38 }
39
40 @Override
41 public long getAndAdd(long delta) {
42 return value.getAndAdd(delta);
43 }
44
45 @Override
46 public long addAndGet(long delta) {
47 return value.addAndGet(delta);
48 }
49
50 @Override
andreafd912ac2015-10-02 14:58:35 -070051 public void set(long value) {
52 this.value.set(value);
53 }
54
55 @Override
Aaron Kruglikov82fd6322015-10-06 12:02:46 -070056 public boolean compareAndSet(long expectedValue, long updateValue) {
57 return value.compareAndSet(expectedValue, updateValue);
58 }
59
60 @Override
Ray Milkey24e60b32015-08-12 11:39:54 -070061 public long get() {
62 return value.get();
63 }
64
65 public static AtomicCounterBuilder builder() {
66 return new Builder();
67 }
68
69 public static class Builder implements AtomicCounterBuilder {
70 @Override
71 public AtomicCounterBuilder withName(String name) {
72 return this;
73 }
74
75 @Override
76 public AtomicCounterBuilder withPartitionsDisabled() {
77 return this;
78 }
79
80 @Override
Ray Milkey24e60b32015-08-12 11:39:54 -070081 public AtomicCounterBuilder withMeteringDisabled() {
82 return this;
83 }
84
85 @Override
Ray Milkey24e60b32015-08-12 11:39:54 -070086 public AsyncAtomicCounter buildAsyncCounter() {
87 throw new UnsupportedOperationException("Async Counter is not supported");
88 }
89
90 @Override
91 public AtomicCounter build() {
92 return new TestAtomicCounter();
93 }
94 }
95}