Last active
December 16, 2015 19:19
-
-
Save electrum/5484725 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Closeable; | |
import java.io.IOException; | |
import java.lang.ref.PhantomReference; | |
import java.lang.ref.ReferenceQueue; | |
import static com.google.common.base.Preconditions.checkNotNull; | |
import static com.google.common.base.Preconditions.checkState; | |
public class Resource | |
{ | |
private Reference resourceReference; | |
public Resource(Reference resourceReference) | |
{ | |
this.resourceReference = checkNotNull(resourceReference, "resourceReference is null"); | |
} | |
public Closeable getResource() | |
{ | |
return resourceReference.getResource(); | |
} | |
public static class Reference | |
extends PhantomReference<Resource> | |
{ | |
private final Closeable resource; | |
public Reference(Resource referent, ReferenceQueue<Resource> queue, Closeable resource) | |
{ | |
super(referent, queue); | |
this.resource = resource; | |
} | |
public void destroy() | |
{ | |
try { | |
resource.close(); | |
} | |
catch (IOException e) { | |
// ignored | |
} | |
} | |
public Closeable getResource() | |
{ | |
return resource; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment