001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.time.internal; 018 019import org.apache.logging.log4j.core.time.MutableInstant; 020import org.apache.logging.log4j.core.time.PreciseClock; 021 022/** 023 * Implementation of the {@code PreciseClock} interface that always returns a fixed time value. 024 * @since 2.11 025 */ 026public class FixedPreciseClock implements PreciseClock { 027 private final long currentTimeMillis; 028 private final int nanosOfMillisecond; 029 030 /** 031 * Constructs a {@code FixedPreciseClock} that always returns the epoch. 032 */ 033 public FixedPreciseClock() { 034 this(0); 035 } 036 037 /** 038 * Constructs a {@code FixedPreciseClock} that always returns the specified time in milliseconds since the epoch. 039 * @param currentTimeMillis milliseconds since the epoch 040 */ 041 public FixedPreciseClock(final long currentTimeMillis) { 042 this(currentTimeMillis, 0); 043 } 044 045 /** 046 * Constructs a {@code FixedPreciseClock} that always returns the specified time in milliseconds since the epoch 047 * and nanosecond of the millisecond. 048 * @param currentTimeMillis milliseconds since the epoch 049 * @param nanosOfMillisecond nanosecond of the specified millisecond 050 */ 051 public FixedPreciseClock(final long currentTimeMillis, final int nanosOfMillisecond) { 052 this.currentTimeMillis = currentTimeMillis; 053 this.nanosOfMillisecond = nanosOfMillisecond; 054 } 055 056 @Override 057 public void init(final MutableInstant instant) { 058 instant.initFromEpochMilli(currentTimeMillis, nanosOfMillisecond); 059 } 060 061 @Override 062 public long currentTimeMillis() { 063 return currentTimeMillis; 064 } 065}