Skip to content

Commit

Permalink
Refactor ReferenceCounted to return boolean indicating whether all re…
Browse files Browse the repository at this point in the history
…ferences have been released.
  • Loading branch information
kuujo committed Sep 4, 2015
1 parent 60cf261 commit a2537cf
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 11 deletions.
4 changes: 3 additions & 1 deletion io/src/main/java/net/kuujo/copycat/io/AbstractBuffer.java
Expand Up @@ -86,13 +86,15 @@ public Buffer acquire() {
}

@Override
public void release() {
public boolean release() {
if (references.decrementAndGet() == 0) {
if (referenceManager != null)
referenceManager.release(this);
else
bytes.close();
return true;
}
return false;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions io/src/main/java/net/kuujo/copycat/io/ReadOnlyBuffer.java
Expand Up @@ -65,8 +65,8 @@ public Buffer acquire() {
}

@Override
public void release() {
root.release();
public boolean release() {
return root.release();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions io/src/main/java/net/kuujo/copycat/io/SlicedBuffer.java
Expand Up @@ -75,8 +75,8 @@ public Buffer acquire() {
}

@Override
public void release() {
root.release();
public boolean release() {
return root.release();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions io/src/main/java/net/kuujo/copycat/io/SwappedBuffer.java
Expand Up @@ -82,8 +82,8 @@ public Buffer acquire() {
}

@Override
public void release() {
root.release();
public boolean release() {
return root.release();
}

@Override
Expand Down
Expand Up @@ -34,8 +34,10 @@ public interface ReferenceCounted<T> extends AutoCloseable {

/**
* Releases a reference.
*
* @return Indicates whether all references to the object have been released.
*/
void release();
boolean release();

/**
* Returns the number of open references.
Expand Down
Expand Up @@ -44,14 +44,16 @@ public T acquire() {

@Override
@SuppressWarnings("unchecked")
public void release() {
public boolean release() {
int refs = references.decrementAndGet();
if (refs == 0) {
referenceManager.release((T) this);
return true;
} else if (refs < 0) {
references.set(0);
throw new IllegalStateException("cannot dereference non-referenced object");
}
return false;
}

@Override
Expand Down
Expand Up @@ -57,14 +57,16 @@ public T acquire() {

@Override
@SuppressWarnings("unchecked")
public void release() {
public boolean release() {
int refs = references.decrementAndGet();
if (refs == 0) {
referenceManager.release((T) this);
return true;
} else if (refs < 0) {
references.set(0);
throw new IllegalStateException("cannot dereference non-referenced object");
}
return false;
}

@Override
Expand Down
Expand Up @@ -110,15 +110,17 @@ public Entry acquire() {
}

@Override
public void release() {
public boolean release() {
int refs = references.decrementAndGet();
if (refs == 0) {
if (referenceManager != null)
referenceManager.release(this);
return true;
} else if (refs < 0) {
references.set(0);
throw new IllegalStateException("cannot dereference non-referenced object");
}
return false;
}

@Override
Expand Down

0 comments on commit a2537cf

Please sign in to comment.