001: /*
002: * (c) Copyright 2008 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.id;
028:
029: import java.net.InetAddress;
030:
031: import org.databene.commons.NumberUtil;
032:
033: /**
034: * Creates strings with hexadecimal UUIDs.<br/><br/>
035: * Created: 27.01.2008 12:16:35
036: * @since 0.4.0
037: * @author Volker Bergmann
038: */
039: public class UUIDProvider implements IdProvider<String> {
040:
041: private static final String IP_ADDRESS;
042: private static final String JVM_ID = NumberUtil.formatHex(
043: (int) (System.currentTimeMillis() >>> 8), 8);
044:
045: private static short counter = (short) 0;
046:
047: private String ipJvm;
048: private String separator;
049:
050: // constructors ----------------------------------------------------------------------------------------------------
051:
052: static {
053: int ipadd;
054: try {
055: ipadd = NumberUtil.toInt(InetAddress.getLocalHost()
056: .getAddress());
057: } catch (Exception e) {
058: ipadd = 0;
059: }
060: IP_ADDRESS = NumberUtil.formatHex(ipadd, 8);
061: }
062:
063: public UUIDProvider() {
064: this ("");
065: }
066:
067: public UUIDProvider(String separator) {
068: this .separator = separator;
069: this .ipJvm = IP_ADDRESS + separator + JVM_ID + separator;
070: }
071:
072: // properties ------------------------------------------------------------------------------------------------------
073:
074: public String getSeparator() {
075: return separator;
076: }
077:
078: // Iterator interface ----------------------------------------------------------------------------------------------
079:
080: public Class<String> getType() {
081: return String.class;
082: }
083:
084: public boolean hasNext() {
085: return true;
086: }
087:
088: public String next() {
089: long time = System.currentTimeMillis();
090: short count;
091: synchronized (UUIDProvider.class) {
092: if (counter < 0)
093: counter = 0;
094: count = counter++;
095: }
096: return new StringBuilder(36).append(ipJvm).append(
097: NumberUtil.formatHex((short) (time >>> 32), 4)).append(
098: separator).append(NumberUtil.formatHex((int) time, 8))
099: .append(separator).append(
100: NumberUtil.formatHex(count, 4)).toString();
101: }
102:
103: public void remove() {
104: throw new UnsupportedOperationException(
105: "Removal is not supported.");
106: }
107:
108: public void close() {
109: // nothing to do
110: }
111:
112: // java.lang.Object overrides --------------------------------------------------------------------------------------
113:
114: public String toString() {
115: return getClass().getSimpleName() + "[ipAddress=" + IP_ADDRESS
116: + ", jvmId=" + JVM_ID + ']';
117: }
118:
119: }
|