From: Damyan Ivanov Date: Tue, 8 Jan 2019 20:13:42 +0000 (+0000) Subject: move ObservableXXXX classes under utils/ X-Git-Tag: v0.3~98 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=127d9d8e0562d315ed025f659503cf64da5c2e13;p=mobile-ledger.git move ObservableXXXX classes under utils/ --- diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index 3ab770b6..f32c3bc6 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -17,6 +17,9 @@ package net.ktnx.mobileledger.model; +import net.ktnx.mobileledger.utils.ObservableAtomicInteger; +import net.ktnx.mobileledger.utils.ObservableValue; + import java.util.ArrayList; import java.util.Date; import java.util.List; diff --git a/app/src/main/java/net/ktnx/mobileledger/model/ObservableAtomicInteger.java b/app/src/main/java/net/ktnx/mobileledger/model/ObservableAtomicInteger.java deleted file mode 100644 index 218229fa..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/model/ObservableAtomicInteger.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright © 2019 Damyan Ivanov. - * This file is part of Mobile-Ledger. - * Mobile-Ledger is free software: you can distribute it and/or modify it - * under the term of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your opinion), any later version. - * - * Mobile-Ledger is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License terms for details. - * - * You should have received a copy of the GNU General Public License - * along with Mobile-Ledger. If not, see . - */ - -package net.ktnx.mobileledger.model; - -import android.os.Build; -import android.support.annotation.RequiresApi; - -import java.util.Observable; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.IntBinaryOperator; -import java.util.function.IntUnaryOperator; - -public class ObservableAtomicInteger extends Observable { - private AtomicInteger holder; - ObservableAtomicInteger() { - super(); - holder = new AtomicInteger(); - } - ObservableAtomicInteger(int initialValue) { - this(); - holder.set(initialValue); - } - public int get() { - return holder.get(); - } - public void set(int newValue) { -// Log.d("atomic", "set"); - holder.set(newValue); - forceNotify(); - } - private void forceNotify() { - setChanged(); -// Log.d("atomic", String.format("notifying %d observers", countObservers())); - notifyObservers(); - } -// public void lazySet(int newValue) { -// holder.lazySet(newValue); -// forceNotify(); -// } - public int getAndSet(int newValue) { - int result = holder.getAndSet(newValue); - forceNotify(); - return result; - } - public boolean compareAndSet(int expect, int update) { - boolean result = holder.compareAndSet(expect, update); - if (result) forceNotify(); - return result; - } -// public boolean weakCompareAndSet(int expect, int update) { -// boolean result = holder.weakCompareAndSet(expect, update); -// if (result) forceNotify(); -// return result; -// } - public int getAndIncrement() { - int result = holder.getAndIncrement(); - forceNotify(); - return result; - } - public int getAndDecrement() { - int result = holder.getAndDecrement(); - forceNotify(); - return result; - } - public int getAndAdd(int delta) { - int result = holder.getAndAdd(delta); - forceNotify(); - return result; - } - public int incrementAndGet() { -// Log.d("atomic", "incrementAndGet"); - int result = holder.incrementAndGet(); - forceNotify(); - return result; - } - public int decrementAndGet() { -// Log.d("atomic", "decrementAndGet"); - int result = holder.decrementAndGet(); - forceNotify(); - return result; - } - public int addAndGet(int delta) { - int result = holder.addAndGet(delta); - forceNotify(); - return result; - } - @RequiresApi(Build.VERSION_CODES.N) - public int getAndUpdate(IntUnaryOperator updateFunction) { - int result = holder.getAndUpdate(updateFunction); - forceNotify(); - return result; - } - @RequiresApi(api = Build.VERSION_CODES.N) - public int updateAndGet(IntUnaryOperator updateFunction) { - int result = holder.updateAndGet(updateFunction); - forceNotify(); - return result; - } - @RequiresApi(api = Build.VERSION_CODES.N) - public int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction) { - int result = holder.getAndAccumulate(x, accumulatorFunction); - forceNotify(); - return result; - } - @RequiresApi(api = Build.VERSION_CODES.N) - public int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction) { - int result = holder.accumulateAndGet(x, accumulatorFunction); - forceNotify(); - return result; - } - public int intValue() { - return holder.intValue(); - } - public long longValue() { - return holder.longValue(); - } - public float floatValue() { - return holder.floatValue(); - } - public double doubleValue() { - return holder.doubleValue(); - } - public byte byteValue() { - return holder.byteValue(); - } - public short shortValue() { - return holder.shortValue(); - } -} diff --git a/app/src/main/java/net/ktnx/mobileledger/model/ObservableValue.java b/app/src/main/java/net/ktnx/mobileledger/model/ObservableValue.java deleted file mode 100644 index 46328157..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/model/ObservableValue.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright © 2019 Damyan Ivanov. - * This file is part of Mobile-Ledger. - * Mobile-Ledger is free software: you can distribute it and/or modify it - * under the term of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your opinion), any later version. - * - * Mobile-Ledger is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License terms for details. - * - * You should have received a copy of the GNU General Public License - * along with Mobile-Ledger. If not, see . - */ - -package net.ktnx.mobileledger.model; - -import java.util.Observable; -import java.util.Observer; - -public class ObservableValue { - private final ObservableValueImpl impl = new ObservableValueImpl<>(); - public ObservableValue() {} - public ObservableValue(T initialValue) { - impl.setValue(initialValue, false); - } - public void set(T newValue) { - impl.setValue(newValue); - } - public T get() { - return impl.getValue(); - } - public void addObserver(Observer o) { - impl.addObserver(o); - } - public void deleteObserver(Observer o) { - impl.deleteObserver(o); - } - public void notifyObservers() { - impl.notifyObservers(); - } - public void notifyObservers(Object arg) { - impl.notifyObservers(arg); - } - public void deleteObservers() { - impl.deleteObservers(); - } - public boolean hasChanged() { - return impl.hasChanged(); - } - public int countObservers() { - return impl.countObservers(); - } - private class ObservableValueImpl extends Observable { - protected T value; - public void setValue(T newValue) { - setValue(newValue, true); - } - private synchronized void setValue(T newValue, boolean notify) { - if ((newValue == null) && (value == null)) return; - - if ((newValue != null) && newValue.equals(value)) return; - - T oldValue = value; - value = newValue; - setChanged(); - if (notify) notifyObservers(oldValue); - } - public T getValue() { - return value; - } - } -} \ No newline at end of file diff --git a/app/src/main/java/net/ktnx/mobileledger/model/TransactionList.java b/app/src/main/java/net/ktnx/mobileledger/model/TransactionList.java index 966f3cc2..c01c2aac 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/TransactionList.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/TransactionList.java @@ -17,6 +17,8 @@ package net.ktnx.mobileledger.model; +import net.ktnx.mobileledger.utils.ObservableValue; + import java.util.List; public class TransactionList extends ObservableValue> { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java index 3693a149..299fcbb8 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java @@ -1,5 +1,5 @@ /* - * Copyright © 2018 Damyan Ivanov. + * Copyright © 2019 Damyan Ivanov. * This file is part of Mobile-Ledger. * Mobile-Ledger is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -27,7 +27,7 @@ import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.async.UpdateTransactionsTask; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.LedgerTransaction; -import net.ktnx.mobileledger.model.ObservableValue; +import net.ktnx.mobileledger.utils.ObservableValue; import java.util.List; diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/ObservableAtomicInteger.java b/app/src/main/java/net/ktnx/mobileledger/utils/ObservableAtomicInteger.java new file mode 100644 index 00000000..9e1ba922 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/utils/ObservableAtomicInteger.java @@ -0,0 +1,144 @@ +/* + * Copyright © 2019 Damyan Ivanov. + * This file is part of Mobile-Ledger. + * Mobile-Ledger is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * Mobile-Ledger is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + +package net.ktnx.mobileledger.utils; + +import android.os.Build; +import android.support.annotation.RequiresApi; + +import java.util.Observable; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.IntBinaryOperator; +import java.util.function.IntUnaryOperator; + +public class ObservableAtomicInteger extends Observable { + private AtomicInteger holder; + ObservableAtomicInteger() { + super(); + holder = new AtomicInteger(); + } + public ObservableAtomicInteger(int initialValue) { + this(); + holder.set(initialValue); + } + public int get() { + return holder.get(); + } + public void set(int newValue) { +// Log.d("atomic", "set"); + holder.set(newValue); + forceNotify(); + } + private void forceNotify() { + setChanged(); +// Log.d("atomic", String.format("notifying %d observers", countObservers())); + notifyObservers(); + } +// public void lazySet(int newValue) { +// holder.lazySet(newValue); +// forceNotify(); +// } + public int getAndSet(int newValue) { + int result = holder.getAndSet(newValue); + forceNotify(); + return result; + } + public boolean compareAndSet(int expect, int update) { + boolean result = holder.compareAndSet(expect, update); + if (result) forceNotify(); + return result; + } +// public boolean weakCompareAndSet(int expect, int update) { +// boolean result = holder.weakCompareAndSet(expect, update); +// if (result) forceNotify(); +// return result; +// } + public int getAndIncrement() { + int result = holder.getAndIncrement(); + forceNotify(); + return result; + } + public int getAndDecrement() { + int result = holder.getAndDecrement(); + forceNotify(); + return result; + } + public int getAndAdd(int delta) { + int result = holder.getAndAdd(delta); + forceNotify(); + return result; + } + public int incrementAndGet() { +// Log.d("atomic", "incrementAndGet"); + int result = holder.incrementAndGet(); + forceNotify(); + return result; + } + public int decrementAndGet() { +// Log.d("atomic", "decrementAndGet"); + int result = holder.decrementAndGet(); + forceNotify(); + return result; + } + public int addAndGet(int delta) { + int result = holder.addAndGet(delta); + forceNotify(); + return result; + } + @RequiresApi(Build.VERSION_CODES.N) + public int getAndUpdate(IntUnaryOperator updateFunction) { + int result = holder.getAndUpdate(updateFunction); + forceNotify(); + return result; + } + @RequiresApi(api = Build.VERSION_CODES.N) + public int updateAndGet(IntUnaryOperator updateFunction) { + int result = holder.updateAndGet(updateFunction); + forceNotify(); + return result; + } + @RequiresApi(api = Build.VERSION_CODES.N) + public int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction) { + int result = holder.getAndAccumulate(x, accumulatorFunction); + forceNotify(); + return result; + } + @RequiresApi(api = Build.VERSION_CODES.N) + public int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction) { + int result = holder.accumulateAndGet(x, accumulatorFunction); + forceNotify(); + return result; + } + public int intValue() { + return holder.intValue(); + } + public long longValue() { + return holder.longValue(); + } + public float floatValue() { + return holder.floatValue(); + } + public double doubleValue() { + return holder.doubleValue(); + } + public byte byteValue() { + return holder.byteValue(); + } + public short shortValue() { + return holder.shortValue(); + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/ObservableValue.java b/app/src/main/java/net/ktnx/mobileledger/utils/ObservableValue.java new file mode 100644 index 00000000..01fa0926 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/utils/ObservableValue.java @@ -0,0 +1,75 @@ +/* + * Copyright © 2019 Damyan Ivanov. + * This file is part of Mobile-Ledger. + * Mobile-Ledger is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * Mobile-Ledger is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + +package net.ktnx.mobileledger.utils; + +import java.util.Observable; +import java.util.Observer; + +public class ObservableValue { + private final ObservableValueImpl impl = new ObservableValueImpl<>(); + public ObservableValue() {} + public ObservableValue(T initialValue) { + impl.setValue(initialValue, false); + } + public void set(T newValue) { + impl.setValue(newValue); + } + public T get() { + return impl.getValue(); + } + public void addObserver(Observer o) { + impl.addObserver(o); + } + public void deleteObserver(Observer o) { + impl.deleteObserver(o); + } + public void notifyObservers() { + impl.notifyObservers(); + } + public void notifyObservers(Object arg) { + impl.notifyObservers(arg); + } + public void deleteObservers() { + impl.deleteObservers(); + } + public boolean hasChanged() { + return impl.hasChanged(); + } + public int countObservers() { + return impl.countObservers(); + } + private class ObservableValueImpl extends Observable { + protected T value; + public void setValue(T newValue) { + setValue(newValue, true); + } + private synchronized void setValue(T newValue, boolean notify) { + if ((newValue == null) && (value == null)) return; + + if ((newValue != null) && newValue.equals(value)) return; + + T oldValue = value; + value = newValue; + setChanged(); + if (notify) notifyObservers(oldValue); + } + public T getValue() { + return value; + } + } +} \ No newline at end of file