blob: 337c090bf06b10d6c39100ffee413bbd88add36a [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
Madan Jampania090a112016-01-18 16:38:17 -080026 @Override
27 public String name() {
28 return null;
29 }
30
31 @Override
32 public Type type() {
33 return Type.COUNTER;
34 }
35
Ray Milkey24e60b32015-08-12 11:39:54 -070036 private TestAtomicCounter() {
37 value = new AtomicLong();
38 }
39
40 @Override
41 public long incrementAndGet() {
42 return value.incrementAndGet();
43 }
44
45 @Override
46 public long getAndIncrement() {
47 return value.getAndIncrement();
48 }
49
50 @Override
51 public long getAndAdd(long delta) {
52 return value.getAndAdd(delta);
53 }
54
55 @Override
56 public long addAndGet(long delta) {
57 return value.addAndGet(delta);
58 }
59
60 @Override
andreafd912ac2015-10-02 14:58:35 -070061 public void set(long value) {
62 this.value.set(value);
63 }
64
65 @Override
Aaron Kruglikov82fd6322015-10-06 12:02:46 -070066 public boolean compareAndSet(long expectedValue, long updateValue) {
67 return value.compareAndSet(expectedValue, updateValue);
68 }
69
70 @Override
Ray Milkey24e60b32015-08-12 11:39:54 -070071 public long get() {
72 return value.get();
73 }
74
75 public static AtomicCounterBuilder builder() {
76 return new Builder();
77 }
78
79 public static class Builder implements AtomicCounterBuilder {
80 @Override
81 public AtomicCounterBuilder withName(String name) {
82 return this;
83 }
84
85 @Override
86 public AtomicCounterBuilder withPartitionsDisabled() {
87 return this;
88 }
89
90 @Override
Ray Milkey24e60b32015-08-12 11:39:54 -070091 public AtomicCounterBuilder withMeteringDisabled() {
92 return this;
93 }
94
95 @Override
Ray Milkey24e60b32015-08-12 11:39:54 -070096 public AsyncAtomicCounter buildAsyncCounter() {
97 throw new UnsupportedOperationException("Async Counter is not supported");
98 }
99
100 @Override
101 public AtomicCounter build() {
102 return new TestAtomicCounter();
103 }
104 }
105}