You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/05/04 12:03:28 UTC

svn commit: r1099391 - /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AtomicRefcount.java

Author: mturk
Date: Wed May  4 10:03:28 2011
New Revision: 1099391

URL: http://svn.apache.org/viewvc?rev=1099391&view=rev
Log:
Add atomic refcount wrapper over AtomicInteger

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AtomicRefcount.java   (with props)

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AtomicRefcount.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AtomicRefcount.java?rev=1099391&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AtomicRefcount.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AtomicRefcount.java Wed May  4 10:03:28 2011
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.commons.runtime;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Atomic reference counter.
+ * Used by the objects that need to be destroyed when reference
+ * counting is zero.
+ */
+public final class AtomicRefcount extends AtomicInteger
+{
+
+    /**
+     * Creates a new object with retention counter set to one.
+     */
+    public AtomicRefcount()
+    {
+        super(1);
+    }
+
+    /**
+     * True if retention counter equals one.
+     */
+    public boolean unique()
+    {
+        return get() == 1;
+    }
+
+    /**
+     * Get retention counter.
+     */
+    public int references()
+    {
+        return get();
+    }
+
+    /**
+     * Increase retention.
+     */
+    public void retain()
+    {
+        incrementAndGet();
+    }
+
+    /**
+     * Decrease retention by decrementing the reference counter.
+     * User of this method will usually destroy a guarded object when
+     * the reference counter value is unique.
+     *
+     * @return {@code true} if the object can be released.
+     */
+    public boolean release()
+    {
+        if (decrementAndGet() == 0)
+            return true;
+        else
+            return false;
+    }
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AtomicRefcount.java
------------------------------------------------------------------------------
    svn:eol-style = native