aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/References.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /jdisc_core/src/main/java/com/yahoo/jdisc/References.java
Publish
Diffstat (limited to 'jdisc_core/src/main/java/com/yahoo/jdisc/References.java')
-rw-r--r--jdisc_core/src/main/java/com/yahoo/jdisc/References.java47
1 files changed, 47 insertions, 0 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/References.java b/jdisc_core/src/main/java/com/yahoo/jdisc/References.java
new file mode 100644
index 00000000000..868ae6ac720
--- /dev/null
+++ b/jdisc_core/src/main/java/com/yahoo/jdisc/References.java
@@ -0,0 +1,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 <a href="mailto:bakksjo@yahoo-inc.com">Oyvind Bakksjo</a>
+ */
+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();
+ }
+ };
+ }
+}