A collection of small, useful tools for Java development. TinyTools aims to provide lightweight utilities that simplify common programming tasks, focusing on thread safety, simplicity, and ease of use.
This project is licensed under the LGPLv3 license. See the LICENSE file for more information.
To use TinyTools, use the following steps for either Maven or Gradle:
git clone https://github.com/glitchruk/tinytools.git
Navigate to the project directory:
cd tinytools
mvn clean package install
gradle clean build
Add the following dependency to your pom.xml
file:
<dependency>
<groupId>com.github.glitchruk</groupId>
<artifactId>tinytools</artifactId>
<version>0.4.6</version>
</dependency>
- Copy the built JAR file from the
tinytools/build/libs
directory to your project'slibs
directory. - Add the following dependency to your
build.gradle
file:
dependencies {
implementation files('libs/tinytools-0.4.6.jar')
}
A list of the components in this library.
Class | Description |
---|---|
Late<T> |
A thread-safe utility class for late initialization. |
Memo<T> |
A thread-safe utility class for memoization. |
The Late
class allows for deferred initialization of an object, where the value is only set once and accessed multiple
times. This is useful in cases where an expensive computation or initialization needs to only be performed once, while
ensuring thread safety and efficient access for subsequent reads.
- Single initialization with
set(T value)
. - Thread-safe access to the value using
get()
. - Initialization state checking with
isInitialized()
.
public final class Person {
private final Late<Integer> age = new Late<>();
public Person() {
// Constructor does not set age; it will be set later
}
public void initializeAge(final int initialAge) {
// Age is set once here, outside the constructor
age.set(initialAge);
}
public int getAge() {
return age.get();
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.initializeAge(42); // Deferred initialization
System.out.println("Age: " + person.getAge()); // Outputs: Age: 42
}
}
The Memo
class allows for deferred initialization and reuse of a value, ensuring thread-safe
access and the ability to reset and reinitialize the value when needed.
- Single initialization with
set(T value)
. - Conditional initialization using
setIfAbsent(T value)
. - Safe access with
get()
and fallback support viagetOrElse(T defaultValue)
. - Reinitialization support with
reset()
orresetAndSet(T value)
. - Initialization state checking with
isInitialized()
.
public final class Person {
private final Memo<Integer> age = new Memo<>();
public Person() {
// Constructor does not set age; it will be initialized later
}
public void setAge(final int initialAge) {
// Set the age initially
age.set(initialAge);
}
public void updateAge(final int newAge) {
// Reset and set a new value
age.resetAndSet(newAge);
}
public boolean hasAge() {
// Check if the age has been initialized
return age.isInitialized();
}
public int getAge() {
return age.get();
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
if (!person.hasAge()) {
System.out.println("Age not set yet!");
person.setAge(42);
}
System.out.println("Age: " + person.getAge()); // Outputs: Age: 42
}
}