blob: d0adf8ead615357c00857b235c1fcb519875cbd5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc;
/**
* Utility class for working with {@link SharedResource}s and {@link ResourceReference}s.
*
* @author bakksjo
*/
public class References {
// Prevents instantiation.
private References() {
}
/**
* A {@link ResourceReference} that does nothing.
* Useful for e.g. testing of resource types when reference counting is not the focus.
*/
public static final ResourceReference NOOP_REFERENCE = new ResourceReference() {
@Override
public void close() {
}
};
/**
* <p>Returns a {@link ResourceReference} that invokes {@link SharedResource#release()} on
* {@link ResourceReference#close() close}. Useful for treating the "main" reference of a {@link SharedResource}
* just as any other reference obtained by calling {@link SharedResource#refer()}. Example:</p>
* <pre>
* final Request request = new Request(...);
* try (final ResourceReference ref = References.fromResource(request)) {
* ....
* }
* // The request will be released on exit from the try block.
* </pre>
*
* @param resource The resource to create a ResourceReference for.
* @return a ResourceReference whose close() method will call release() on the given resource.
*/
public static ResourceReference fromResource(final SharedResource resource) {
return new ResourceReference() {
@Override
public void close() {
resource.release();
}
};
}
}
|