Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.service; |
| 17 | |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 18 | import java.util.concurrent.atomic.AtomicLong; |
| 19 | |
| 20 | /** |
| 21 | * Test implementation of atomic counter. |
| 22 | */ |
| 23 | public final class TestAtomicCounter implements AtomicCounter { |
| 24 | final AtomicLong value; |
| 25 | |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 26 | @Override |
| 27 | public String name() { |
| 28 | return null; |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public Type type() { |
| 33 | return Type.COUNTER; |
| 34 | } |
| 35 | |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 36 | 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 |
andrea | fd912ac | 2015-10-02 14:58:35 -0700 | [diff] [blame] | 61 | public void set(long value) { |
| 62 | this.value.set(value); |
| 63 | } |
| 64 | |
| 65 | @Override |
Aaron Kruglikov | 82fd632 | 2015-10-06 12:02:46 -0700 | [diff] [blame] | 66 | public boolean compareAndSet(long expectedValue, long updateValue) { |
| 67 | return value.compareAndSet(expectedValue, updateValue); |
| 68 | } |
| 69 | |
| 70 | @Override |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 71 | 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 Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 91 | public AtomicCounterBuilder withMeteringDisabled() { |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | @Override |
Ray Milkey | 24e60b3 | 2015-08-12 11:39:54 -0700 | [diff] [blame] | 96 | 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 | } |