// Path: components/uuid-generator/org.wso2.carbon.uuid.generator/src/main/java/org/wso2/carbon/uuid/generator/util/UUIDGeneratorConstants.java
// public class UUIDGeneratorConstants {
// 
//     public final static int CLOCK_SEQUENCE_HI_BYTE_OFFSET_IN_UUID = 8;
//     public final static int CLOCK_SEQUENCE_LOW_BYTE_OFFSET_IN_UUID = 9;
// 
// }
// 
// Path: components/uuid-generator/org.wso2.carbon.uuid.generator/src/main/java/org/wso2/carbon/uuid/generator/util/Util.java
// public class Util {
// 
//     public final static long createClockSeqInLong(byte[] buffer, int offset) {
// 
//         long hi = ((long) gatherInt(buffer, offset)) << 32;
//         long lo = (((long) gatherInt(buffer, offset + 4)) << 32) >>> 32;
//         return hi | lo;
//     }
// 
//     public static long createUUIDSecondHalfInLong(long l2) {
// 
//         l2 = ((l2 << 2) >>> 2); // remove 2 MSB
//         l2 |= (2L << 62); // set 2 MSB to '10'
//         return l2;
//     }
// 
//     private final static int gatherInt(byte[] buffer, int offset) {
// 
//         return (buffer[offset] << 24) | ((buffer[offset + 1] & 0xFF) << 16)
//                 | ((buffer[offset + 2] & 0xFF) << 8) | (buffer[offset + 3] & 0xFF);
//     }
// 
// }


import org.wso2.carbon.uuid.generator.util.UUIDGeneratorConstants;
import org.wso2.carbon.uuid.generator.util.Util;
import java.security.SecureRandom;
import java.util.Random;
import java.util.UUID;
/*
 *  Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. 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.
 *
 * NOTE: The logic in this class is copied from https://github.com/cowtowncoder/java-uuid-generator/, all credits
 * goes to the original authors of the project  https://github.com/cowtowncoder/java-uuid-generator/.
 */
package org.wso2.carbon.uuid.generator;



/**
 * Class responsible for constructing and generating a time based UUID.
 */
public class TimeBasedUUIDGenerator extends UUIDGenerator {

    long address;
    //Last 8 bytes of the UUID (node id + clock sequence)
    protected final long uuidSecondHalf;

    protected final UUIDTimeStamp uuidTimeStamp;

    public TimeBasedUUIDGenerator(UUIDTimeStamp timeStamp) {

        byte[] timeBaseduuidByteAray = new byte[16];
        constructNodeId();
        insertToByteArray(timeBaseduuidByteAray, 10);
        int clockSequence = timeStamp.getClockSequence();
        //set clock_seq_hi.The high field of the clock sequence.
        timeBaseduuidByteAray[UUIDGeneratorConstants.CLOCK_SEQUENCE_HI_BYTE_OFFSET_IN_UUID] =
                (byte) (clockSequence >> 8);
        //set clock_seq_low.
        timeBaseduuidByteAray[UUIDGeneratorConstants.CLOCK_SEQUENCE_LOW_BYTE_OFFSET_IN_UUID] = (byte) clockSequence;
// Path: components/uuid-generator/org.wso2.carbon.uuid.generator/src/main/java/org/wso2/carbon/uuid/generator/util/UUIDGeneratorConstants.java
// public class UUIDGeneratorConstants {
// 
//     public final static int CLOCK_SEQUENCE_HI_BYTE_OFFSET_IN_UUID = 8;
//     public final static int CLOCK_SEQUENCE_LOW_BYTE_OFFSET_IN_UUID = 9;
// 
// }
// 
// Path: components/uuid-generator/org.wso2.carbon.uuid.generator/src/main/java/org/wso2/carbon/uuid/generator/util/Util.java
// public class Util {
// 
//     public final static long createClockSeqInLong(byte[] buffer, int offset) {
// 
//         long hi = ((long) gatherInt(buffer, offset)) << 32;
//         long lo = (((long) gatherInt(buffer, offset + 4)) << 32) >>> 32;
//         return hi | lo;
//     }
// 
//     public static long createUUIDSecondHalfInLong(long l2) {
// 
//         l2 = ((l2 << 2) >>> 2); // remove 2 MSB
//         l2 |= (2L << 62); // set 2 MSB to '10'
//         return l2;
//     }
// 
//     private final static int gatherInt(byte[] buffer, int offset) {
// 
//         return (buffer[offset] << 24) | ((buffer[offset + 1] & 0xFF) << 16)
//                 | ((buffer[offset + 2] & 0xFF) << 8) | (buffer[offset + 3] & 0xFF);
//     }
// 
// }


// Path: components/uuid-generator/org.wso2.carbon.uuid.generator/src/main/java/org/wso2/carbon/uuid/generator/TimeBasedUUIDGenerator.java
import org.wso2.carbon.uuid.generator.util.UUIDGeneratorConstants;
import org.wso2.carbon.uuid.generator.util.Util;
import java.security.SecureRandom;
import java.util.Random;
import java.util.UUID;

/*
 *  Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. 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.
 *
 * NOTE: The logic in this class is copied from https://github.com/cowtowncoder/java-uuid-generator/, all credits
 * goes to the original authors of the project  https://github.com/cowtowncoder/java-uuid-generator/.
 */
package org.wso2.carbon.uuid.generator;



/**
 * Class responsible for constructing and generating a time based UUID.
 */
public class TimeBasedUUIDGenerator extends UUIDGenerator {

    long address;
    //Last 8 bytes of the UUID (node id + clock sequence)
    protected final long uuidSecondHalf;

    protected final UUIDTimeStamp uuidTimeStamp;

    public TimeBasedUUIDGenerator(UUIDTimeStamp timeStamp) {

        byte[] timeBaseduuidByteAray = new byte[16];
        constructNodeId();
        insertToByteArray(timeBaseduuidByteAray, 10);
        int clockSequence = timeStamp.getClockSequence();
        //set clock_seq_hi.The high field of the clock sequence.
        timeBaseduuidByteAray[UUIDGeneratorConstants.CLOCK_SEQUENCE_HI_BYTE_OFFSET_IN_UUID] =
                (byte) (clockSequence >> 8);
        //set clock_seq_low.
        timeBaseduuidByteAray[UUIDGeneratorConstants.CLOCK_SEQUENCE_LOW_BYTE_OFFSET_IN_UUID] = (byte) clockSequence;
        long clockSeqInLong = Util.createClockSeqInLong(timeBaseduuidByteAray, 8);