Java AtomicInteger getTimeBasedUUID()

Here you can find the source of getTimeBasedUUID()

Description

Generates Type 1 UUID.

License

Apache License

Return

universally unique identifiers (UUID)

Declaration

public static UUID getTimeBasedUUID() 

Method Source Code

//package com.java2s;
/*/*from w  ww  . ja  v a 2  s . c  o m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */

import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

public class Main {
    private static AtomicInteger count = new AtomicInteger(0);
    private static final long TYPE1 = 0x1000L;
    private static final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;
    private static long least;
    private static final long LOW_MASK = 0xffffffffL;
    private static final long MID_MASK = 0xffff00000000L;
    private static final long HIGH_MASK = 0xfff000000000000L;
    private static final int SHIFT_2 = 16;
    private static final int SHIFT_4 = 32;
    private static final int SHIFT_6 = 48;
    private static final int HUNDRED_NANOS_PER_MILLI = 10000;

    /**
     * Generates Type 1 UUID. The time contains the number of 100NS intervals that have occurred
     * since 00:00:00.00 UTC, 10 October 1582. Each UUID on a particular machine is unique to the 100NS interval
     * until they rollover around 3400 A.D.
     * <ol>
     * <li>Digits 1-12 are the lower 48 bits of the number of 100 ns increments since the start of the UUID
     * epoch.</li>
     * <li>Digit 13 is the version (with a value of 1).</li>
     * <li>Digits 14-16 are a sequence number that is incremented each time a UUID is generated.</li>
     * <li>Digit 17 is the variant (with a value of binary 10) and 10 bits of the sequence number</li>
     * <li>Digit 18 is final 16 bits of the sequence number.</li>
     * <li>Digits 19-32 represent the system the application is running on.
     * </ol>
     *
     * @return universally unique identifiers (UUID)
     */
    public static UUID getTimeBasedUUID() {
        long time = ((System.currentTimeMillis() * HUNDRED_NANOS_PER_MILLI) + NUM_100NS_INTERVALS_SINCE_UUID_EPOCH)
                + (count.incrementAndGet() % HUNDRED_NANOS_PER_MILLI);
        long timeLow = (time & LOW_MASK) << SHIFT_4;
        long timeMid = (time & MID_MASK) >> SHIFT_2;
        long timeHi = (time & HIGH_MASK) >> SHIFT_6;
        long most = timeLow | timeMid | TYPE1 | timeHi;
        return new UUID(most, least);
    }
}

Related

  1. getReply()
  2. getSerialNum()
  3. getTempFileName()
  4. getThreadDumpId()
  5. getThreadName(String pattern, String name)
  6. getUniqueId()
  7. getUuidAsFourCharacterGroups()
  8. increase(A a, Map map)
  9. isPropBlocked(String s)