2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.utils;
20 import android.os.Build;
22 import java.util.Observable;
23 import java.util.concurrent.atomic.AtomicInteger;
24 import java.util.function.IntBinaryOperator;
25 import java.util.function.IntUnaryOperator;
27 import androidx.annotation.RequiresApi;
29 public class ObservableAtomicInteger extends Observable {
30 private AtomicInteger holder;
31 ObservableAtomicInteger() {
33 holder = new AtomicInteger();
35 public ObservableAtomicInteger(int initialValue) {
37 holder.set(initialValue);
42 public void set(int newValue) {
43 // debug("atomic", "set");
47 private void forceNotify() {
49 // debug("atomic", String.format("notifying %d observers", countObservers()));
52 // public void lazySet(int newValue) {
53 // holder.lazySet(newValue);
56 public int getAndSet(int newValue) {
57 int result = holder.getAndSet(newValue);
61 public boolean compareAndSet(int expect, int update) {
62 boolean result = holder.compareAndSet(expect, update);
63 if (result) forceNotify();
66 // public boolean weakCompareAndSet(int expect, int update) {
67 // boolean result = holder.weakCompareAndSet(expect, update);
68 // if (result) forceNotify();
71 public int getAndIncrement() {
72 int result = holder.getAndIncrement();
76 public int getAndDecrement() {
77 int result = holder.getAndDecrement();
81 public int getAndAdd(int delta) {
82 int result = holder.getAndAdd(delta);
86 public int incrementAndGet() {
87 // debug("atomic", "incrementAndGet");
88 int result = holder.incrementAndGet();
92 public int decrementAndGet() {
93 // debug("atomic", "decrementAndGet");
94 int result = holder.decrementAndGet();
98 public int addAndGet(int delta) {
99 int result = holder.addAndGet(delta);
103 @RequiresApi(Build.VERSION_CODES.N)
104 public int getAndUpdate(IntUnaryOperator updateFunction) {
105 int result = holder.getAndUpdate(updateFunction);
109 @RequiresApi(api = Build.VERSION_CODES.N)
110 public int updateAndGet(IntUnaryOperator updateFunction) {
111 int result = holder.updateAndGet(updateFunction);
115 @RequiresApi(api = Build.VERSION_CODES.N)
116 public int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction) {
117 int result = holder.getAndAccumulate(x, accumulatorFunction);
121 @RequiresApi(api = Build.VERSION_CODES.N)
122 public int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction) {
123 int result = holder.accumulateAndGet(x, accumulatorFunction);
127 public int intValue() {
128 return holder.intValue();
130 public long longValue() {
131 return holder.longValue();
133 public float floatValue() {
134 return holder.floatValue();
136 public double doubleValue() {
137 return holder.doubleValue();
139 public byte byteValue() {
140 return holder.byteValue();
142 public short shortValue() {
143 return holder.shortValue();