/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2009 Sun Microsystems, Inc.
* Portions Copyright 2011-2015 ForgeRock AS.
*/
/**
* This class provides an application-wide timing service. It provides
* the ability to retrieve the current time in various different formats
* and resolutions.
*/
mayInstantiate = false,
mayExtend = false,
mayInvoke = true)
public final class TimeThread
{
/**
* Timer job.
*/
{
/** The calendar holding the current time. */
/** The date for this time thread. */
/** The timestamp for this time thread in the generalized time format. */
/** The timestamp for this time thread in GMT. */
/** The date formatter that will be used to obtain the GMT timestamp. */
/** The current time in HHmm form as an integer. */
private int hourAndMinute;
/** The timestamp for this time thread in the local time zone. */
/** The date formatter that will be used to obtain the local timestamp. */
/** The current time in nanoseconds. */
private volatile long nanoTime;
/** The current time in milliseconds since the epoch. */
private volatile long time;
/**
* A set of arbitrary formatters that should be maintained by this time
* thread.
*/
/**
* A set of arbitrary formatted times, mapped from format string to the
* corresponding formatted time representation.
*/
/**
* Create a new job with the specified delay.
*/
public TimeInfo()
{
userDefinedFormatters = new CopyOnWriteArrayList<>();
userDefinedTimeStrings = new ConcurrentHashMap<>();
new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z");
// Populate initial values.
run();
}
/** {@inheritDoc} */
public void run()
{
try
{
calendar = new GregorianCalendar();
{
}
}
catch (Exception e)
{
logger.traceException(e);
}
}
}
/**
* Thread factory used by the scheduled execution service.
*/
private static final class TimeThreadFactory implements
{
/** {@inheritDoc} */
{
t.setDaemon(true);
return t;
}
}
/** The singleton instance. */
/** The tracer object for the debug logger. */
/**
* Retrieves a <CODE>Calendar</CODE> containing the time at the last
* update.
*
* @return A <CODE>Calendar</CODE> containing the time at the last
* update.
* @throws IllegalStateException
* If the time service has not been started.
*/
{
checkState();
}
/**
* Retrieves a <CODE>Date</CODE> containing the time at the last
* update.
*
* @return A <CODE>Date</CODE> containing the time at the last update.
* @throws IllegalStateException
* If the time service has not been started.
*/
{
checkState();
}
/**
* Retrieves a string containing a normalized representation of the
* current time in a generalized time format. The timestamp will look
* like "20050101000000.000Z".
*
* @return A string containing a normalized representation of the
* current time in a generalized time format.
* @throws IllegalStateException
* If the time service has not been started.
*/
{
checkState();
}
/**
* Retrieves a string containing the current time in GMT. The
* timestamp will look like "20050101000000Z".
*
* @return A string containing the current time in GMT.
* @throws IllegalStateException
* If the time service has not been started.
*/
{
checkState();
}
/**
* Retrieves an integer containing the time in HHmm format at the last
* update. It will be calculated as "(hourOfDay*100) + minuteOfHour".
*
* @return An integer containing the time in HHmm format at the last
* update.
* @throws IllegalStateException
* If the time service has not been started.
*/
{
checkState();
}
/**
* Retrieves a string containing the current time in the local time
* zone. The timestamp format will look like
* "01/Jan/2005:00:00:00 -0600".
*
* @return A string containing the current time in the local time
* zone.
* @throws IllegalStateException
* If the time service has not been started.
*/
{
checkState();
}
/**
* Retrieves the time in nanoseconds from the most precise available system
* timer. The value returned represents nanoseconds since some fixed but
* arbitrary time.
*
* @return The time in nanoseconds from some fixed but arbitrary time.
* @throws IllegalStateException
* If the time service has not been started.
*/
{
checkState();
}
/**
* Retrieves the time in milliseconds since the epoch at the last
* update.
*
* @return The time in milliseconds since the epoch at the last
* update.
* @throws IllegalStateException
* If the time service has not been started.
*/
{
checkState();
}
/**
* Retrieves the current time formatted using the given format string.
* <p>
* The first time this method is used with a given format string, it
* will be used to create a formatter that will generate the time
* string. That formatter will then be put into a list so that it will
* be maintained automatically for future use.
*
* @param formatString
* The string that defines the format of the time string to
* retrieve.
* @return The formatted time string.
* @throws IllegalArgumentException
* If the provided format string is invalid.
* @throws IllegalStateException
* If the time service has not been started.
*/
{
checkState();
if (timeString == null)
{
}
return timeString;
}
/**
* Removes the user-defined time formatter from this time thread so
* that it will no longer be maintained. This is a safe operation
* because if the same format string is used for multiple purposes
* then it will be added back the next time a time is requested with
* the given format.
*
* @param formatString
* The format string for the date formatter to remove.
* @throws IllegalStateException
* If the time service has not been started.
*/
throws IllegalStateException
{
checkState();
formatString));
}
/**
* Starts the time service if it has not already been started.
*/
public static void start()
{
{
INSTANCE = new TimeThread();
}
}
/**
* Stops the time service if it has not already been stopped.
*/
public static void stop()
{
{
}
}
/** Ensures that the time service has been started. */
{
{
throw new IllegalStateException("Time service not started");
}
}
/** The scheduler. */
/** The current time information. */
/**
* Creates a new instance of this time service and starts it.
*/
private TimeThread()
{
}
}