An extremely lightweight Java interners library.
Allows to add intern() method (similarly to String.intern()) to your immutable classes with customizable interners.
MapInterner - interner based on java.util.Map (java.util.HashMap in particular)
ConcurrentMapInterner - same as MapInterner but based on ConcurrentMap
ProbInterner - interner that accepts the chance to deduplicate a given value and the parent interner (which technically makes it just a deduplicator and not an interner, but anyway).
import com.ydo4ki.interners.ConcurrentMapInterner;
import com.ydo4ki.interners.Internable;
import com.ydo4ki.interners.Interner;
import com.ydo4ki.interners.MapInterner;
class MyImmutableClass implements Internable<MyImmutableClass> {
private final int x;
private final long a;
MyImmutableClass(int x, long a) {
this.x = x;
this.a = a;
}
private static final MapInterner<MyImmutableClass> interner = new ConcurrentMapInterner<>();
@Override
public Interner<MyImmutableClass> interner() {
return interner;
}
public static MyImmutableClass create(int x, long a) {
return new MyImmutableClass(x, a).intern(); // returns the canonical instance with x and a values
}
public static void dropInterner() {
interner.clear(); // clear the table of MyImmutableClass
}
// it is important to define equals and hashcode so the interner maps knows which two instances are considered equal.
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
MyImmutableClass that = (MyImmutableClass) o;
return x == that.x && a == that.a;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + Long.hashCode(a);
return result;
}
}<dependency>
<groupId>com.ydo4ki</groupId>
<artifactId>Interners</artifactId>
<version>1.0.1</version>
</dependency>implementation 'com.ydo4ki:Interners:1.0.1'Go to the releases tab and download latest jar
- Clone this repository
git clone https://github.com/Y-Sulphuris/Interners.git
cd Interners- Run maven build:
mvn clean package