Skip to content

Instantly share code, notes, and snippets.

@rodericj
Created February 19, 2015 22:38
Show Gist options
  • Save rodericj/8e37ac90388e50fd2282 to your computer and use it in GitHub Desktop.
Save rodericj/8e37ac90388e50fd2282 to your computer and use it in GitHub Desktop.
/**
* Helper function to determine whether two cones are playing in the same cluster. Two TuneDevices
* are in the same cluster if one is a master and the other its slave or if both are slaves to the
* same master.
*
* @param other The other Cone that may or may not be in the cluster
* @return true if the two TuneDevices are in the same cluster. false otherwise.
*/
public boolean isInClusterWith(TuneDevice other){
switch(clusterRole){
case NONE: return false;
case MASTER:
/// If we are the master, then the only way this other TuneDevice is in the same
/// cluster is if they are one of our slaves.
for(TuneDevice slave : getSlaves()){
if(slave.equals(other)){
return true;
}
}
break;
case SLAVE:
/// If we are a slave, then either the other is our master or they are also a slave
/// to the same master.
if(other.clusterRole == ClusterRole.MASTER){
for(TuneDevice slave : other.getSlaves()){
if(slave.equals(this)){
return true;
}
}
}
else if(other.clusterRole == ClusterRole.SLAVE){
TuneDevice ourMaster = getMaster();
TuneDevice otherMaster = other.getMaster();
return ourMaster.equals(otherMaster);
}
else{
return false;
}
break;
default: break;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment