001: /*
002: * $Id: StringIdentifierGenerator.java,v 1.6 2005/08/24 00:40:27 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.util;
042:
043: import java.net.InetAddress;
044: import java.util.Random;
045:
046: /**
047: * Generates Unique Id across system
048: * @author Ahimanikya Satapathy
049: */
050: public class StringIdentifierGenerator {
051: public static StringIdentifierGenerator INSTANCE = new StringIdentifierGenerator();
052: private Random _randomForSpace = null;
053: private Random _randomForTime = null;
054:
055: /**
056: * @return String generated new ID
057: */
058: public synchronized String nextIdentifier() {
059: long uniqueInSpace = _randomForSpace.nextLong();
060: long uniqueInTime = _randomForTime.nextLong();
061:
062: return Long.toHexString(uniqueInSpace).toUpperCase() + "_"
063: + Long.toHexString(uniqueInTime).toUpperCase();
064: }
065:
066: /**
067: * @param prefix String to be used as prefix for the ID
068: * @return String generated new ID
069: */
070: public String nextIdentifier(String prefix) {
071: return prefix + "_" + nextIdentifier();
072: }
073:
074: private StringIdentifierGenerator() {
075: long uniqueSpaceSeed = createUniqueSpaceSeed();
076: long uniqueTimeSeed = System.currentTimeMillis();
077:
078: _randomForSpace = new Random(uniqueSpaceSeed);
079: _randomForTime = new Random(uniqueTimeSeed);
080: }
081:
082: /**
083: * @return long a seed number based on current state of the local machine
084: */
085: private long createUniqueSpaceSeed() {
086: StringBuffer hBuf = new StringBuffer(20);
087:
088: hBuf.append(Runtime.getRuntime().freeMemory());
089: hBuf.append(Runtime.getRuntime().maxMemory());
090: hBuf.append(Runtime.getRuntime().totalMemory());
091:
092: //try to get my IP address and add to the hashingString
093: try {
094: hBuf.append(InetAddress.getLocalHost());
095: } catch (Exception e) {
096: //Do nothing
097: }
098:
099: return hBuf.hashCode();
100: }
101:
102: /**
103: * Generates a new 16-digit (hex) identifier.
104: *
105: * @return new 16-digit (hex) identifier
106: */
107: public synchronized String next16DigitIdentifier() {
108: String hexSpaceValue = getNextUniqueInSpace();
109: String hexTimeValue = getUniqueInTime();
110:
111: return "00000000".substring(0, 8 - hexSpaceValue.length())
112: + hexSpaceValue
113: + "00000000".substring(0, 8 - hexTimeValue.length())
114: + hexTimeValue;
115: }
116:
117: private String getUniqueInTime() {
118: int maskedValue;
119: long uniqueInTime = _randomForTime.nextLong();
120: maskedValue = (int) (uniqueInTime & 0xFFFFFFFFL);
121: String hexTimeValue = Integer.toHexString(maskedValue)
122: .toUpperCase();
123: return hexTimeValue;
124: }
125:
126: private String getNextUniqueInSpace() {
127: long uniqueInSpace = _randomForSpace.nextLong();
128: int maskedValue = (int) (uniqueInSpace & 0xFFFFFFFFL);
129: String hexSpaceValue = Integer.toHexString(maskedValue)
130: .toUpperCase();
131: return hexSpaceValue;
132: }
133:
134: /**
135: * Generates a new 32-digit (hex) identifier.
136: *
137: * @return new 32-digit (hex) identifier
138: */
139: public synchronized String next32DigitIdentifier() {
140: String hexSpaceValue = getNextUniqueInSpace();
141: String hexTimeValue = getUniqueInTime();
142:
143: return "0000000000000000".substring(0, 16 - hexSpaceValue
144: .length())
145: + hexSpaceValue
146: + "0000000000000000".substring(0, 16 - hexTimeValue
147: .length()) + hexTimeValue;
148: }
149:
150: /**
151: * Generates a new 16-digit (hex) identifier with the given String as its prefix.
152: *
153: * @return new 16-digit (hex) identifier, prefixed by the given String and an underscore separator.
154: */
155: public String next32DigitIdentifier(String prefix) {
156: return prefix + "_" + next32DigitIdentifier();
157: }
158:
159: /**
160: * Generates a new 16-digit (hex) identifier with the given String as its prefix.
161: *
162: * @return new 16-digit (hex) identifier, prefixed by the given String and an underscore separator.
163: */
164: public String next16DigitIdentifier(String prefix) {
165: return prefix + "_" + next16DigitIdentifier();
166: }
167:
168: // public static void main(String[] args) {
169: // for (int i = 0; i < 1000; i++) {
170: // System.out.println(INSTANCE.next16DigitIdentifier());
171: // }
172: // }
173: }
|