Java AtomicInteger getUuidAsFourCharacterGroups()

Here you can find the source of getUuidAsFourCharacterGroups()

Description

get Uuid As Four Character Groups

License

Apache License

Declaration

public static String getUuidAsFourCharacterGroups() 

Method Source Code

//package com.java2s;
/*//  w ww.j a  v a  2s  .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;

    public static String getUuidAsFourCharacterGroups() {
        UUID uuid = getTimeBasedUUID();
        String uuid_s = uuid.toString();
        String[] parts = uuid_s.split("-", 0); //$NON-NLS-1$
        StringBuilder sb = new StringBuilder();
        for (String p : parts) {
            if (p.length() <= 4) {
                sb.append('-').append(p);
            } else {
                int plen = p.length();
                int spos = 0;
                while (spos < plen) {
                    sb.append('-').append(p.substring(spos, spos + Math.min(4, plen - spos)));
                    spos += 4;
                }
            }
        }
        return sb.substring(1);
    }

    /**
     * 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. getTempFileName()
  2. getThreadDumpId()
  3. getThreadName(String pattern, String name)
  4. getTimeBasedUUID()
  5. getUniqueId()
  6. increase(A a, Map map)
  7. isPropBlocked(String s)
  8. loadServiceCount(String id)
  9. print2DIntArray(int[][] array)