001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.id;
018:
019: import java.io.Serializable;
020: import java.net.InetAddress;
021:
022: /**
023: * @author kimchy
024: */
025: public class UUIDGenerator implements IdentifierGenerator {
026:
027: private static int toInt(byte[] bytes) {
028: int result = 0;
029: for (int i = 0; i < 4; i++) {
030: result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
031: }
032: return result;
033: }
034:
035: private static final int IP;
036: static {
037: int ipadd;
038: try {
039: ipadd = toInt(InetAddress.getLocalHost().getAddress());
040: } catch (Exception e) {
041: ipadd = 0;
042: }
043: IP = ipadd;
044: }
045:
046: private static short counter = (short) 0;
047:
048: private static final int JVM = (int) (System.currentTimeMillis() >>> 8);
049:
050: /**
051: * Unique across JVMs on this machine (unless they load this class in the
052: * same quater second - very unlikely)
053: */
054: protected int getJVM() {
055: return JVM;
056: }
057:
058: /**
059: * Unique in a millisecond for this JVM instance (unless there are >
060: * Short.MAX_VALUE instances created in a millisecond)
061: */
062: protected short getCount() {
063: synchronized (UUIDGenerator.class) {
064: if (counter < 0)
065: counter = 0;
066: return counter++;
067: }
068: }
069:
070: /**
071: * Unique in a local network
072: */
073: protected int getIP() {
074: return IP;
075: }
076:
077: /**
078: * Unique down to millisecond
079: */
080: protected short getHiTime() {
081: return (short) (System.currentTimeMillis() >>> 32);
082: }
083:
084: protected int getLoTime() {
085: return (int) System.currentTimeMillis();
086: }
087:
088: private String sep = "";
089:
090: protected String format(int intval) {
091: String formatted = Integer.toHexString(intval);
092: StringBuffer buf = new StringBuffer("00000000");
093: buf.replace(8 - formatted.length(), 8, formatted);
094: return buf.toString();
095: }
096:
097: protected String format(short shortval) {
098: String formatted = Integer.toHexString(shortval);
099: StringBuffer buf = new StringBuffer("0000");
100: buf.replace(4 - formatted.length(), 4, formatted);
101: return buf.toString();
102: }
103:
104: public Serializable generate() {
105: return new StringBuffer(36).append(format(getIP())).append(sep)
106: .append(format(getJVM())).append(sep).append(
107: format(getHiTime())).append(sep).append(
108: format(getLoTime())).append(sep).append(
109: format(getCount())).toString();
110: }
111:
112: }
|