Valuable is an API that provides abstractions for holding and accessing values, making them modifiable within Java Lambdas and other nested objects contexts.
Valuable provides APIs for holding and accessing both object (Valuable<V>) and primitive types (Valuable.Boolean,
Valuable.Char, Valuable.Byte, Valuable.Short, Valuable.Int, Valuable.Long, Valuable.Float, Valuable.Double).
The main objectives of the Valuable API and its implementations are:
- To provide a shared value access logic.
- To offer value holders that can be utilized in nested objects' executions, permitting write access to these holders.
In the context of the second objective, it's common practice to use final, single-element object or primitive arrays within
nested classes and Lambdas which capture outer variable values as their fields. Valuable is introduced
to eliminate the need for this workaround.
Let's examine the usage of the Valuable by examples:
import org.moodminds.valuable.Valuable;
import java.util.function.IntSupplier;
public class Sample {
void sample() {
// define Valuable.
final Valuable<String> valuable;
// primitive Valuables.
final Valuable.Boolean booleanValuable;
final Valuable.Char charValuable;
final Valuable.Byte byteValuable;
final Valuable.Short shortValuable;
final Valuable.Int intValuable;
final Valuable.Long longValuable;
final Valuable.Float floatValuable;
final Valuable.Double doubleValuable;
// operate the Valuables.
// set values.
valuable.put("parallel");
booleanValuable.put(false);
charValuable.put('1');
byteValuable.put((byte) 1);
shortValuable.put((short) 1);
intValuable.put(1);
longValuable.put(1L);
floatValuable.put(1.0f);
doubleValuable.put(1.0D);
// get values.
String stringCurrent = valuable.get();
boolean booleanCurrent = booleanValuable.get();
char charCurrent = charValuable.get();
byte byteCurrent = byteValuable.get();
short shortCurrent = shortValuable.get();
int intCurrent = intValuable.get();
long longCurrent = longValuable.get();
float floatCurrent = floatValuable.get();
double doubleCurrent = doubleValuable.get();
// get previous and set possibly atomically (for atomic implementations).
String stringPrevious = valuable.set("parallel");
boolean booleanPrevious = booleanValuable.set(false);
char charPrevious = charValuable.set('1');
byte bytePrevious = byteValuable.set((byte) 1);
short shortPrevious = shortValuable.set((short) 1);
int intPrevious = intValuable.set(1);
long longPrevious = longValuable.set(1L);
float floatPrevious = floatValuable.set(1.0f);
double doublePrevious = doubleValuable.set(1.0D);
// compare and set possibly atomically (for atomic implementations).
boolean valueChanged = valuable.let("sequential", "parallel");
boolean booleanChanged = booleanValuable.let(true, false);
boolean charChanged = charValuable.let('0', '1');
boolean byteChanged = byteValuable.let((byte) 0, (byte) 1);
boolean shortChanged = shortValuable.let((short) 0, (short) 1);
boolean intChanged = intValuable.let(0, 1);
boolean longChanged = longValuable.let(0L, 1L);
boolean floatChanged = floatValuable.let(0.0f, 1.0f);
boolean doubleChanged = doubleValuable.let(0.0D, 1.0D);
// increment and decrement number Valuables (possibly atomic in atomic implementations).
IntSupplier intSupplier = intValuable::incr;
intSupplier = intValuable::decr;
}
}Include Valuable in your project by adding the dependency.
Artifacts can be found on Maven Central after publication.
<dependency>
<groupId>org.moodminds.valuable</groupId>
<artifactId>valuable</artifactId>
<version>${version}</version>
</dependency>You may need to build from source to use Valuable (until it is in Maven Central) with Maven and JDK 8 at least.
This project is going to be released under version 2.0 of the Apache License.