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 Show documentation
Show all versions of groovy Show documentation
Groovy: A powerful, dynamic language for the JVM
/*
* Copyright 2003-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
}
}
}