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

kieker.monitoring.timer.SystemMilliTimer Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
/***************************************************************************
 * Copyright 2022 Kieker Project (http://kieker-monitoring.net)
 *
 * Licensed 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.
 ***************************************************************************/

package kieker.monitoring.timer;

import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import kieker.common.configuration.Configuration;

/**
 * A timer implementation, counting in milliseconds since a specified offset.
 *
 * @author Jan Waller
 *
 * @since 1.5
 */
public final class SystemMilliTimer extends AbstractTimeSource {
	/** The name of the configuration for the offset. */
	public static final String CONFIG_OFFSET = SystemMilliTimer.class.getName() + ".offset";
	/** The name of the configuration for the time unit (0 = nanoseconds, 1 = microseconds, 2 = milliseconds, 3 = seconds). */
	public static final String CONFIG_UNIT = SystemMilliTimer.class.getName() + ".unit";

	private final long offset;
	private final TimeUnit timeunit;

	/**
	 * Creates a new instance of this class using the given parameters.
	 *
	 * @param configuration
	 *            The configuration for this timer.
	 */
	public SystemMilliTimer(final Configuration configuration) {
		super(configuration);
		if (configuration.getStringProperty(CONFIG_OFFSET).length() == 0) {
			this.offset = System.currentTimeMillis();
		} else {
			this.offset = configuration.getLongProperty(CONFIG_OFFSET);
		}
		this.timeunit = configuration.getEnumProperty(CONFIG_UNIT, TimeUnit.class, TimeUnit.MILLISECONDS);
	}

	@Override
	public long getTime() {
		return this.timeunit.convert(System.currentTimeMillis() - this.offset, TimeUnit.MILLISECONDS);
	}

	@Override
	public long getOffset() {
		return this.timeunit.convert(this.offset, TimeUnit.MILLISECONDS);
	}

	@Override
	public TimeUnit getTimeUnit() {
		return this.timeunit;
	}

	@Override
	public String toString() {
		final StringBuilder sb = new StringBuilder(64);
		sb.append("Time in ").append(this.timeunit.toString().toLowerCase(Locale.ENGLISH)).append(" (with milliseconds precision) since ")
				.append(new Date(this.offset));
		return sb.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy