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

com.softicar.platform.common.core.clock.TestClock Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.core.clock;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.time.temporal.TemporalAmount;

/**
 * A controllable {@link Clock} useful for unit tests.
 * 

* This {@link Clock} returns the same {@link Instant} as long as it is not * changed with {@link #setInstant(Instant)}. * * @author Oliver Richers */ public class TestClock extends Clock { private final ZoneId zone; private Instant instant; /** * Initialized this clock with the current {@link Instant}. *

* The method {@link ZoneId#systemDefault()} is used to obtain the * {@link ZoneId} to be used. */ public TestClock() { this(ZoneId.systemDefault(), Instant.now()); } /** * Initialized this clock with the given {@link Instant}. *

* The method {@link ZoneId#systemDefault()} is used to obtain the * {@link ZoneId} to be used. * * @param instant * the {@link Instant} to use (never null) */ public TestClock(Instant instant) { this(ZoneId.systemDefault(), instant); } /** * Initialized this clock with the given {@link ZoneId} and {@link Instant}. * * @param zone * the {@link ZoneId} to use (never null) * @param instant * the {@link Instant} to use (never null) */ public TestClock(ZoneId zone, Instant instant) { this.zone = zone; this.instant = instant; } @Override public ZoneId getZone() { return zone; } @Override public Clock withZone(ZoneId zone) { return new TestClock(zone, instant); } @Override public Instant instant() { return instant; } /** * Specifies the {@link Instant} to return by this {@link Clock}. * * @param instant * the instant to use (never null) * @return this {@link Clock} */ public TestClock setInstant(Instant instant) { this.instant = instant; return this; } /** * Adds the given milliseconds to the {@link Instant} to return by this * {@link Clock}. * * @param millis * the milliseconds to add * @return this {@link Clock} */ public TestClock addMillis(long millis) { this.instant = instant.plusMillis(millis); return this; } /** * Adds the given {@link TemporalAmount} to the {@link Instant} to return by * this {@link Clock}. * * @param temporalAmount * the temporal amount to add * @return this {@link Clock} */ public TestClock add(TemporalAmount temporalAmount) { this.instant = instant.plus(temporalAmount); return this; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy