Source Code Cross Referenced for Enrollment.java in  » Net » Terracotta » com » tc » l2 » state » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Terracotta » com.tc.l2.state 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003:         * notice. All rights reserved.
004:         */
005:        package com.tc.l2.state;
006:
007:        import com.tc.logging.TCLogger;
008:        import com.tc.logging.TCLogging;
009:        import com.tc.net.groups.NodeID;
010:        import com.tc.util.Assert;
011:
012:        import java.io.Externalizable;
013:        import java.io.IOException;
014:        import java.io.ObjectInput;
015:        import java.io.ObjectOutput;
016:        import java.util.Arrays;
017:
018:        public class Enrollment implements  Externalizable {
019:
020:            private static final TCLogger logger = TCLogging
021:                    .getLogger(Enrollment.class);
022:            private NodeID nodeID;
023:            private long[] weights;
024:            private boolean isNew;
025:
026:            public Enrollment() {
027:                // To make serialization happy
028:            }
029:
030:            public Enrollment(NodeID nodeID, boolean isNew, long[] weights) {
031:                this .nodeID = nodeID;
032:                this .isNew = isNew;
033:                Assert.assertNotNull(weights);
034:                this .weights = weights;
035:            }
036:
037:            public NodeID getNodeID() {
038:                return nodeID;
039:            }
040:
041:            public boolean isANewCandidate() {
042:                return isNew;
043:            }
044:
045:            public boolean wins(Enrollment other) {
046:                if (isNew != other.isNew) {
047:                    // an old candidate always wins over a new candidate
048:                    return !isNew;
049:                }
050:                int myLength = weights.length;
051:                int otherLength = other.weights.length;
052:                if (myLength > otherLength) {
053:                    return true;
054:                } else if (myLength < otherLength) {
055:                    return false;
056:                } else {
057:                    for (int i = 0; i < myLength; i++) {
058:                        if (weights[i] > other.weights[i]) {
059:                            // wins
060:                            return true;
061:                        } else if (weights[i] < other.weights[i]) {
062:                            // loses
063:                            return false;
064:                        }
065:                    }
066:
067:                    // XXX:: Both are the same weight. This shouldn't happen once we fix the weights to
068:                    // be unique (based on hardware,ip,process id etc.) But now it is possible and we
069:                    // handle it. If two nodes dont agree because of this there will be a re-election
070:                    logger.warn("Two Enrollments with same weights : " + this 
071:                            + " == " + other);
072:                    return false;
073:                }
074:            }
075:
076:            public void readExternal(ObjectInput in) throws IOException,
077:                    ClassNotFoundException {
078:                this .nodeID = (NodeID) in.readObject();
079:                this .isNew = in.readBoolean();
080:                this .weights = new long[in.readInt()];
081:                for (int i = 0; i < weights.length; i++) {
082:                    weights[i] = in.readLong();
083:                }
084:            }
085:
086:            public void writeExternal(ObjectOutput out) throws IOException {
087:                out.writeObject(this .nodeID);
088:                out.writeBoolean(this .isNew);
089:                out.writeInt(weights.length);
090:                for (int i = 0; i < weights.length; i++) {
091:                    out.writeLong(weights[i]);
092:                }
093:            }
094:
095:            public boolean equals(Object o) {
096:                if (o instanceof  Enrollment) {
097:                    Enrollment oe = (Enrollment) o;
098:                    return nodeID.equals(oe.nodeID)
099:                            && Arrays.equals(weights, oe.weights)
100:                            && isNew == oe.isNew;
101:                }
102:                return false;
103:            }
104:
105:            public String toString() {
106:                StringBuffer sb = new StringBuffer("Enrollment [ ");
107:                sb.append(nodeID).append(", isNew = ").append(isNew);
108:                sb.append(", weights = ");
109:                int length = weights.length;
110:                for (int i = 0; i < length; i++) {
111:                    sb.append(weights[i]);
112:                    if (i < length - 1) {
113:                        sb.append(",");
114:                    }
115:                }
116:                sb.append(" ]");
117:                return sb.toString();
118:            }
119:
120:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.