1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.id.uuid.state; 19 20 import java.util.Arrays; 21 22 import org.apache.commons.id.uuid.clock.Clock; 23 import org.apache.commons.id.uuid.clock.OverClockedException; 24 25 26 /** 27 * <p>The <code>Node</code> class represents the data and accessors for a single 28 * node identifier. The node id is generally the IEEE 802 address, the clock 29 * sequence, and last timestamp generated are all attributes of a node that need 30 * to be maintained.</p> 31 * 32 * @author Commons-Id team 33 * @version $Id: Node.java 480488 2006-11-29 08:57:26Z bayard $ 34 */ 35 public class Node { 36 37 /** The node identifier bytes this class represents. */ 38 private byte[] id; 39 40 /** The clock sequence associated with this node. */ 41 private short clockSequence; 42 43 /** The last time stamp used. */ 44 private long lastTimestamp; 45 46 /** The Clock implementation instance for this Node. */ 47 private Clock clock; 48 49 /** 50 * <p>Constructor used to create a <node>Node</node> when the lastTimestamp 51 * and clock sequence are unavailable.</p> 52 * 53 * @param nodeId the byte array representing this nodes identifier. 54 * Usually the IEEE 802 address bytes. 55 */ 56 public Node(byte[] nodeId) { 57 id = nodeId; 58 clockSequence = StateHelper.newClockSequence(); 59 clock = StateHelper.getClockImpl(); 60 } 61 62 /** 63 * <p>Constructor used to create a <node>Node</node> when all persistent state 64 * information is available; node bytes, lastTimestamp, and clock sequence. 65 * </p> 66 * 67 * @param nodeId the byte array representing this nodes identifier. 68 * Usually the IEEE 802 address bytes. 69 * @param lastTime the last timestamp this <code>Node</code> used. 70 * @param clockSeq the last clock sequence used in generation from this node. 71 */ 72 public Node(byte[] nodeId, long lastTime, short clockSeq) { 73 id = nodeId; 74 lastTimestamp = lastTime; 75 clockSequence = clockSeq; 76 clock = StateHelper.getClockImpl(); 77 } 78 79 /** 80 * <p>Returns the node identifier bytes for this node.</p> 81 * 82 * @return the node identifier bytes for this node. 83 */ 84 85 public byte[] getNodeIdentifier() { 86 return id; 87 } 88 89 /** 90 * <p>Returns the clock sequence used in this node.</p> 91 * 92 * @return the clock sequence used in this node. 93 */ 94 public short getClockSequence() { 95 return clockSequence; 96 } 97 98 /** 99 * <p>Increments the clock sequence in this node.</p> 100 */ 101 private void incrementClockSequence() { 102 //Increment, but if it's greater than its 14-bits, reset it 103 if (++clockSequence > 0x3FFF) { 104 clockSequence = 0; 105 } 106 } 107 108 /** 109 * <p>Returns the time in UUID time.</p> 110 * 111 * @return the time in UUID time. 112 * @throws OverClockedException the max number of timestamps generated in 113 * this interval has been exceeded. 114 */ 115 public long getUUIDTime() throws OverClockedException { 116 long newTime = clock.getUUIDTime(); 117 if (newTime <= lastTimestamp) { 118 incrementClockSequence(); 119 } 120 lastTimestamp = newTime; 121 return newTime; 122 } 123 124 /** 125 * <p>Returns true if the identifier value in this Node is equal to the 126 * identifier value in the compare to Node.</p> 127 * 128 * @param compareTo the Node to compare for equivalence. 129 * @return true if the two node's identifiers are equal; otherwise false. 130 * @see Object#equals 131 */ 132 public boolean equals(Object compareTo) { 133 if (compareTo instanceof Node) { 134 byte[] comp = ((Node) compareTo).getNodeIdentifier(); 135 return Arrays.equals(comp, this.id); 136 } else if (compareTo instanceof byte[]) { 137 return Arrays.equals((byte[]) compareTo, id); 138 } 139 return false; 140 } 141 142 /** 143 * @see Object#hashCode 144 */ 145 public int hashCode() { 146 int hash = 0; 147 for (int i = 0; i < id.length; i++) { 148 hash += id[i]; 149 } 150 return hash; 151 } 152 /** 153 * <p>Returns the last uuid timestamp from this <code>Node</code>.</p> 154 * 155 * @return the last uuid timestamp from this Node. 156 */ 157 public long getLastTimestamp() { 158 return lastTimestamp; 159 } 160 161 }