org.codehaus.groovy.util.ReferenceType Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovy-all Show documentation
Show all versions of groovy-all Show documentation
Groovy: A powerful, dynamic language for the JVM
package org.codehaus.groovy.util;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
public enum ReferenceType {
SOFT {
@Override
protected Reference createReference(T value, V handler, ReferenceQueue queue) {
return new SoftRef(value, handler, queue);
}
},
WEAK {
@Override
protected Reference createReference(T value, V handler, ReferenceQueue queue) {
return new WeakRef(value, handler, queue);
}
},
PHANTOM {
@Override
protected Reference createReference(T value, V handler, ReferenceQueue queue) {
return new PhantomRef(value, handler, queue);
}
},
HARD {
@Override
protected Reference createReference(T value, V handler, ReferenceQueue queue) {
return new HardRef(value, handler, queue);
}
};
protected abstract Reference createReference(T value, V handler, ReferenceQueue queue);
private static class SoftRef extends SoftReference implements Reference {
private final V handler;
public SoftRef(TT referent, V handler, ReferenceQueue q) {
super(referent, q);
this.handler = handler;
}
public V getHandler() {
return handler;
}
}
private static class WeakRef extends WeakReference implements Reference {
private final V handler;
public WeakRef(TT referent, V handler, ReferenceQueue q) {
super(referent, q);
this.handler = handler;
}
public V getHandler() {
return handler;
}
}
private static class PhantomRef extends PhantomReference implements Reference {
private final V handler;
public PhantomRef(TT referent, V handler, ReferenceQueue q) {
super(referent, q);
this.handler = handler;
}
public V getHandler() {
return handler;
}
}
private static class HardRef implements Reference {
private TT ref;
private final V handler;
public HardRef(TT referent, V handler, ReferenceQueue q) {
this.ref = referent;
this.handler = handler;
}
public V getHandler() {
return handler;
}
public TT get() {
return ref;
}
public void clear() {
ref = null;
}
}
}