Skip to content

Commit

Permalink
Merged Rsp.retval and Rsp.exception into 1 field: Rsp.value
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Feb 2, 2016
1 parent f41969f commit 6356f0b
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/org/jgroups/util/Rsp.java
Expand Up @@ -9,24 +9,24 @@
*/ */
public class Rsp<T> { public class Rsp<T> {
/** Flag that represents whether the response was received */ /** Flag that represents whether the response was received */
protected static final byte RECEIVED = 1; protected static final byte RECEIVED = 1;


/** Flag that represents whether the sender of the response was suspected */ /** Flag that represents whether the sender of the response was suspected */
protected static final byte SUSPECTED = 1 << 1; protected static final byte SUSPECTED = 1 << 1;


/** If true, the sender (below) could not be reached, e.g. if a site was down (only used by RELAY2) */ /** If true, the sender (below) could not be reached, e.g. if a site was down (only used by RELAY2) */
protected static final byte UNREACHABLE = 1 << 2; protected static final byte UNREACHABLE = 1 << 2;

/** Set when the value is an exception */
protected static final byte IS_EXCEPTION = 1 << 3;


protected byte flags; protected byte flags;


/** The sender of this response */ /** The sender of this response */
protected final Address sender; protected final Address sender;


/** The value from the response */ /** The value from the response (or the exception) */
protected T retval; protected Object value; // untyped, to be able to hold both T and Throwable

/** If there was an exception, this field will contain it */
protected Throwable exception;




public Rsp(Address sender) { public Rsp(Address sender) {
Expand Down Expand Up @@ -58,30 +58,31 @@ public int hashCode() {
} }


public T getValue() { public T getValue() {
return retval; return (T)value;
} }


public Rsp<T> setValue(T val) { public Rsp<T> setValue(T val) {
this.retval=val; this.value=val;
setReceived(); setReceived();
exception=null; this.flags=Util.clearFlags(flags, IS_EXCEPTION); // clear the exception flag just in case it is set
return this; return this;
} }


public boolean hasException() { public boolean hasException() {
return exception != null; return Util.isFlagSet(flags, IS_EXCEPTION);
} }


public Throwable getException() { public Throwable getException() {
return exception; return hasException()? (Throwable)value : null;
} }


public void setException(Throwable t) { public Rsp<T> setException(Throwable t) {
if(t != null) { if(t != null) {
this.exception=t; this.value=t;
setReceived(); setReceived();
retval=null; this.flags=Util.setFlag(flags, IS_EXCEPTION);
} }
return this;
} }


public Address getSender() { public Address getSender() {
Expand Down Expand Up @@ -121,10 +122,12 @@ public boolean setUnreachable() {
public String toString() { public String toString() {
StringBuilder sb=new StringBuilder(); StringBuilder sb=new StringBuilder();
sb.append("sender=").append(sender); sb.append("sender=").append(sender);
if(retval != null) if(value != null) {
sb.append(", retval=").append(retval); if(!hasException())
if(exception != null) sb.append(", value=").append(value);
sb.append(", exception=").append(exception); else
sb.append(", exception=").append(getException());
}
sb.append(", received=").append(wasReceived()).append(", suspected=").append(wasSuspected()); sb.append(", received=").append(wasReceived()).append(", suspected=").append(wasSuspected());
if(wasUnreachable()) if(wasUnreachable())
sb.append(" (unreachable)"); sb.append(" (unreachable)");
Expand Down

0 comments on commit 6356f0b

Please sign in to comment.