All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Stats.HPTime Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Common.Stats;

import com.alachisoft.ncache.serialization.core.io.InternalCompactSerializable;

import java.io.IOException;

/**
 * HPTime represents the time based on the ticks of High Performance coutners. It is a relative time not synchronized with system time. The time accuracy is upto micro seconds.
 *
 * Taimoor Haider
 */
//C# TO JAVA CONVERTER TODO TASK: The interface type was changed to the closest equivalent Java type, but the methods implemented will need adjustment:
public class HPTime implements java.lang.Comparable, InternalCompactSerializable {
    private static long _frequency;
    private static long _baseTimeInNanoSeconds;
    private static Object _synObj = new Object();
    private static String _col = ":";

    static {
        synchronized (_synObj) {
            _baseTimeInNanoSeconds = System.nanoTime();

        }
    }

    private int _hr;
    private int _min;
    private int _sec;
    private int _mlSec;
    private int _micSec;
    private double _baseTime;
    private double _baseRem;

    /**
     * Gets current HP time
     */


    public static void initialize() {
        getNow();
        for (int i = 1; i < 1000; i++) {
            getNow();
        }
    }

    /**
     * Gets current HP time
     */

    public static HPTime getNow() {
        double rem = 0;
        long currentTimeInNano = 0;
        long diff;

        HPTime time = new HPTime();
        currentTimeInNano = System.nanoTime();

        diff = currentTimeInNano - _baseTimeInNanoSeconds;
        rem = ((double) diff / (double) 1000000);

        time._hr = (int) (rem / 3600000);
        rem = rem - (time._hr * 3600000);

        time._min = (int) rem / 60000;
        rem = rem - (time._min * 60000);

        time._sec = (int) rem / 1000;
        rem = rem - (time._sec * 1000);

        time._mlSec = (int) rem;
        rem = (rem - (double) time._mlSec) * 1000;
        time._micSec = (int) rem;

        return time;
    }

    /**
     * Gets the hours component of the time of this instance.
     */
    public final int getHours() {
        return _hr;
    }

    /**
     * Gets the hours component of the time of this instance.
     */
    public final int getMinutes() {
        return _min;
    }

    /**
     * Gets the Secnds component of the time of this instance.
     */
    public final int getSeconds() {
        return _sec;
    }

    /**
     * Gets the MilliSecond component of the time of this instance.
     */
    public final int getMilliSeconds() {
        return _mlSec;
    }

    /**
     * Gets the MicroSeconds component of the time of this instance.
     */
    public final int getMicroSeconds() {
        return _micSec;
    }

    public final double getBaseTime() {
        return _baseTime;
    }

    public final void setBaseTime(double value) {
        _baseRem = value;
    }

    public final double getServerTicks() {
        return _baseRem;
    }

    public final HPTime getCurrentTime() {
        try {

            double rem = 0;
            long currentTimeInNano = 0;
            long diff;

            HPTime time = new HPTime();
            currentTimeInNano = System.nanoTime();

            diff = currentTimeInNano - _baseTimeInNanoSeconds;
            rem = ((double) diff / (double) 1000000);

            _baseTime = rem;
            time._baseTime = rem;
            rem += _baseRem;


            time._hr = (int) (rem / 3600000);
            rem = rem - (time._hr * 3600000);

            time._min = (int) rem / 60000;
            rem = rem - (time._min * 60000);

            time._sec = (int) rem / 1000;
            rem = rem - (time._sec * 1000);

            time._mlSec = (int) rem;
            rem = (rem - (double) time._mlSec) * 1000;
            time._micSec = (int) rem;

            return time;
        } finally {
        }
    }

    /**
     * Gets the string representation of the current instance of HP time.
     *
     * @return
     */
    @Override
    public String toString() {
        return _hr % 24 + _col + _min % 60 + _col + _sec % 60 + _col + (long) _mlSec + _col + _micSec;
    }

    /**
     * Gets the string representation of the current instance of HP time.
     *
     * @return
     */
    public final String ToAbsoluteTimeString() {
        return _hr + _col + _min + _col + _sec + _col + (long) _mlSec + _col + _micSec;
    }

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
    ///#region IComparable Members


    public final int compareTo(Object obj) {
        if (obj instanceof HPTime) {
            HPTime other = (HPTime) obj;
            int result = (new Integer(this.getHours())).compareTo(other.getHours());
            if (result == 0) {
                result = (new Integer(this.getMinutes())).compareTo(other.getMinutes());
                if (result == 0) {
                    result = (new Integer(this.getSeconds())).compareTo(other.getSeconds());
                    if (result == 0) {
                        result = (new Integer(this.getMilliSeconds())).compareTo(other.getMilliSeconds());
                        if (result == 0) {
                            return (new Integer(this.getMicroSeconds())).compareTo(other.getMicroSeconds());
                        }
                        return result;
                    }
                    return result;
                }
                return result;
            }
            return result;
        }
        throw new IllegalArgumentException("Object is not HPTime");
    }

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
    ///#endregion

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
    ///#region ICompactSerializable Members


    @Override
    public final void Deserialize(com.alachisoft.ncache.serialization.standard.io.CompactReader reader) throws IOException, ClassNotFoundException {
        _hr = reader.ReadInt32();
        _micSec = reader.ReadInt32();
        _min = reader.ReadInt32();
        _mlSec = reader.ReadInt32();
        _sec = reader.ReadInt32();
    }

    @Override
    public final void Serialize(com.alachisoft.ncache.serialization.standard.io.CompactWriter writer) throws IOException {
        writer.Write(_hr);
        writer.Write(_micSec);
        writer.Write(_min);
        writer.Write(_mlSec);
        writer.Write(_sec);
    }

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
    ///#endregion

//    @Override
//    public void writeExternal(NCacheObjectOutput out) throws IOException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void readExternal(NCacheObjectInput in) throws IOException, ClassNotFoundException {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy