1 5 package com.tc.l2.state; 6 7 import com.tc.logging.TCLogger; 8 import com.tc.logging.TCLogging; 9 import com.tc.net.groups.NodeID; 10 import com.tc.util.Assert; 11 12 import java.io.Externalizable ; 13 import java.io.IOException ; 14 import java.io.ObjectInput ; 15 import java.io.ObjectOutput ; 16 17 public class Enrollment implements Externalizable { 18 19 private static final TCLogger logger = TCLogging.getLogger(Enrollment.class); 20 private NodeID nodeID; 21 private int[] weights; 22 private boolean isNew; 23 24 public Enrollment() { 25 } 27 28 public Enrollment(NodeID nodeID, boolean isNew, int[] weights) { 29 this.nodeID = nodeID; 30 this.isNew = isNew; 31 Assert.assertNotNull(weights); 32 this.weights = weights; 33 } 34 35 public NodeID getNodeID() { 36 return nodeID; 37 } 38 39 public boolean isANewCandidate() { 40 return isNew; 41 } 42 43 public boolean wins(Enrollment other) { 44 if (isNew != other.isNew) { 45 return !isNew; 47 } 48 int myLength = weights.length; 49 int otherLength = other.weights.length; 50 if (myLength > otherLength) { 51 return true; 52 } else if (myLength < otherLength) { 53 return false; 54 } else { 55 for (int i = 0; i < myLength; i++) { 56 if (weights[i] > other.weights[i]) { 57 return true; 59 } else if (weights[i] < other.weights[i]) { 60 return false; 62 } 63 } 64 65 logger.warn("Two Enrollments with same weights : " + this + " == " + other); 69 return false; 70 } 71 } 72 73 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 74 this.nodeID = (NodeID) in.readObject(); 75 this.isNew = in.readBoolean(); 76 this.weights = new int[in.readInt()]; 77 for (int i = 0; i < weights.length; i++) { 78 weights[i] = in.readInt(); 79 } 80 } 81 82 public void writeExternal(ObjectOutput out) throws IOException { 83 out.writeObject(this.nodeID); 84 out.writeBoolean(this.isNew); 85 out.writeInt(weights.length); 86 for (int i = 0; i < weights.length; i++) { 87 out.writeInt(weights[i]); 88 } 89 } 90 91 public int hashCode() { 92 return nodeID.hashCode(); 93 } 94 95 public boolean equals(Object o) { 96 if (o instanceof Enrollment) { 97 Enrollment oe = (Enrollment) o; 98 return nodeID.equals(oe.nodeID); 99 } 100 return false; 101 } 102 103 public String toString() { 104 StringBuffer sb = new StringBuffer ("Enrollment [ "); 105 sb.append(nodeID).append(", isNew = ").append(isNew); 106 sb.append(", weights = "); 107 int length = weights.length; 108 for (int i = 0; i < length; i++) { 109 sb.append(weights[i]); 110 if (i < length - 1) { 111 sb.append(","); 112 } 113 } 114 sb.append(" ]"); 115 return sb.toString(); 116 } 117 118 } 119 | Popular Tags |