<commit_before>/*
 * Stun4j, the OpenSource Java Solution for NAT and Firewall Traversal.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.ice4j.stack;

import java.util.Random;
import java.util.Arrays;

/**
 * This class encapsulates a STUN transaction ID. It is useful for storing
 * transaction ids in collection objects as it implements the equals method.
 * It also provies a utility for creating unique transaction ids.
 *
 * <p>Organisation: <p> Louis Pasteur University, Strasbourg, France</p>
 * <p>Network Research Team (http://www-r2.u-strasbg.fr)</p></p>
 * @author <NAME>
 * @version 0.1
 */

class TransactionID
{
    /**
     * The id itself
     */
    private byte transactionID[] = new byte[12];

    /**
     * The object to use to generate the rightmost 8 bytes of the id.
     */
    private static Random random = new Random(System.currentTimeMillis());

    /**
     * A hashcode for hashtable storage.
     */
    private int hashCode = 0;

    /**
     * Limits access to <tt>TransactionID</tt> instantiation.
     */
    private TransactionID()
    {
    }

    /**
     * Creates a transaction id object.The transaction id itself is generated
     * using the following algorithm:
     *
     * The first 6 bytes of the id are given the value of
     * <tt>System.currentTimeMillis()</tt>. Putting the right most bits first
     * so that we get a more optimized equals() method.
     *
     * @return A <tt>TransactionID</tt> object with a unique transaction id.
     */
    static TransactionID createTransactionID()
    {
        TransactionID tid = new TransactionID();

        long left  = System.currentTimeMillis();//the first 6 bytes of the id
        long right = random.nextLong();//the last 6 bytes of the id

        for(int i = 0; i < 6; i++)
        {
            tid.transactionID[i]   = (byte)((left  >> (i*8))& 0xFFl);
            tid.transactionID[i+6] = (byte)((right >> (i*8))& 0xFFl);
        }

        //calculate hashcode for Hashtable storage.
        tid.hashCode =   (tid.transactionID[3] << 24 & 0xFF000000)
                       | (tid.transactionID[2] << 16 & 0x00FF0000)
                       | (tid.transactionID[1] << 8  & 0x0000FF00)
                       | (tid.transactionID[0]       & 0x000000FF);

        return tid;
    }

    /**
     * Creates a transaction identifier object with the specified id.
     * @param transactionID the id to give to the new TransactionID
     * @return a new TransactionID object with the specified id value;
     */
    static TransactionID createTransactionID(byte[] transactionID)
    {
        TransactionID tid = new TransactionID();

        System.arraycopy(transactionID, 0, tid.transactionID, 0, 12);

        //calculate hashcode for Hashtable storage.
        tid.hashCode =   (tid.transactionID[3] << 24 & 0xFF000000)
                       | (tid.transactionID[2] << 16 & 0x00FF0000)
                       | (tid.transactionID[1] << 8  & 0x0000FF00)
                       | (tid.transactionID[0]       & 0x000000FF);


        return tid;
    }


    /**
     * Returns the transaction id byte array (length 12).
     * @return the transaction ID byte array.
     */
    public byte[] getTransactionID()
    {
        return transactionID;
    }

    /**
     * Compares two TransactionID objects.
     * @param obj the object to compare with.
     * @return true if the objects are equal and false otherwise.
     */
    public boolean equals(Object obj)
    {

        if(   obj == null
           || !(obj instanceof TransactionID))
            return false;

        if(this == obj)
            return true;

        byte targetBytes[] = ((TransactionID)obj).transactionID;

        return Arrays.equals(transactionID, targetBytes);
    }

    /**
     * Compares the specified byte array with this transaction id.
     * @param targetID the id to compare with ours.
     * @return true if targetID matches this transaction id.
     */
    public boolean equals(byte[] targetID)
    {
        return Arrays.equals(transactionID, targetID);
    }

    /**
     * Returns the first four bytes of the transactionID to ensure proper
     * retrieval from hashtables;
     * @return the hashcode of this object - as advised by the Java Platform
     * Specification
     */
    public int hashCode()
    {
        return hashCode;
    }

    /**
     * Returns a string representation of the ID
     * @return a hex string representing the id
     */
    public String toString()
    {
        StringBuffer idStr = new StringBuffer();

        for(int i = 0; i < transactionID.length; i++)
        {
            idStr.append("0x");
            if((transactionID[i]&0xFF) <= 15)
            {
                idStr.append("0");
            }

            idStr.append(Integer.toHexString(transactionID[i]&0xff).toUpperCase());

            if(i < transactionID.length)
                idStr.append(" ");
        }

        return idStr.toString();
    }
}
<commit_msg>Makes stun transactions more visible to users of the stun stack<commit_after>/*
 * Stun4j, the OpenSource Java Solution for NAT and Firewall Traversal.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.ice4j.stack;

import java.util.Random;
import java.util.Arrays;

/**
 * This class encapsulates a STUN transaction ID. It is useful for storing
 * transaction ids in collection objects as it implements the equals method.
 * It also provies a utility for creating unique transaction ids.
 *
 * @author <NAME>
 * @version 0.1
 */

class TransactionID
{
    /**
     * The id itself
     */
    private byte transactionID[] = new byte[12];

    /**
     * The object to use to generate the rightmost 8 bytes of the id.
     */
    private static Random random = new Random(System.currentTimeMillis());

    /**
     * A hashcode for hashtable storage.
     */
    private int hashCode = 0;

    /**
     * Limits access to <tt>TransactionID</tt> instantiation.
     */
    private TransactionID()
    {
    }

    /**
     * Creates a transaction id object.The transaction id itself is generated
     * using the following algorithm:
     *
     * The first 6 bytes of the id are given the value of
     * <tt>System.currentTimeMillis()</tt>. Putting the right most bits first
     * so that we get a more optimized equals() method.
     *
     * @return A <tt>TransactionID</tt> object with a unique transaction id.
     */
    static TransactionID createTransactionID()
    {
        TransactionID tid = new TransactionID();

        long left  = System.currentTimeMillis();//the first 6 bytes of the id
        long right = random.nextLong();//the last 6 bytes of the id

        for(int i = 0; i < 6; i++)
        {
            tid.transactionID[i]   = (byte)((left  >> (i*8))& 0xFFl);
            tid.transactionID[i+6] = (byte)((right >> (i*8))& 0xFFl);
        }

        //calculate hashcode for Hashtable storage.
        tid.hashCode =   (tid.transactionID[3] << 24 & 0xFF000000)
                       | (tid.transactionID[2] << 16 & 0x00FF0000)
                       | (tid.transactionID[1] << 8  & 0x0000FF00)
                       | (tid.transactionID[0]       & 0x000000FF);

        return tid;
    }

    /**
     * Creates a transaction identifier object with the specified id.
     * @param transactionID the id to give to the new TransactionID
     * @return a new TransactionID object with the specified id value;
     */
    static TransactionID createTransactionID(byte[] transactionID)
    {
        TransactionID tid = new TransactionID();

        System.arraycopy(transactionID, 0, tid.transactionID, 0, 12);

        //calculate hashcode for Hashtable storage.
        tid.hashCode =   (tid.transactionID[3] << 24 & 0xFF000000)
                       | (tid.transactionID[2] << 16 & 0x00FF0000)
                       | (tid.transactionID[1] << 8  & 0x0000FF00)
                       | (tid.transactionID[0]       & 0x000000FF);


        return tid;
    }


    /**
     * Returns the transaction id byte array (length 12).
     * @return the transaction ID byte array.
     */
    public byte[] getTransactionID()
    {
        return transactionID;
    }

    /**
     * Compares two TransactionID objects.
     * @param obj the object to compare with.
     * @return true if the objects are equal and false otherwise.
     */
    public boolean equals(Object obj)
    {

        if(   obj == null
           || !(obj instanceof TransactionID))
            return false;

        if(this == obj)
            return true;

        byte targetBytes[] = ((TransactionID)obj).transactionID;

        return Arrays.equals(transactionID, targetBytes);
    }

    /**
     * Compares the specified byte array with this transaction id.
     * @param targetID the id to compare with ours.
     * @return true if targetID matches this transaction id.
     */
    public boolean equals(byte[] targetID)
    {
        return Arrays.equals(transactionID, targetID);
    }

    /**
     * Returns the first four bytes of the transactionID to ensure proper
     * retrieval from hashtables;
     * @return the hashcode of this object - as advised by the Java Platform
     * Specification
     */
    public int hashCode()
    {
        return hashCode;
    }

    /**
     * Returns a string representation of the ID
     * @return a hex string representing the id
     */
    public String toString()
    {
        StringBuffer idStr = new StringBuffer();

        for(int i = 0; i < transactionID.length; i++)
        {
            idStr.append("0x");
            if((transactionID[i]&0xFF) <= 15)
            {
                idStr.append("0");
            }

            idStr.append(Integer.toHexString(transactionID[i]&0xff).toUpperCase());

            if(i < transactionID.length)
                idStr.append(" ");
        }

        return idStr.toString();
    }
}

{'fraction_non_alphanumeric': 0.10166599759132879, 'fraction_numerical': 0.021176234443998396, 'mean_word_length': 2.074668312249306, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2218871', 'n_tokens_mistral': 2947, 'n_tokens_neox': 2727, 'n_words': 905}