com.github.tonivade.purefun.effect.util.ZClock Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2019, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun.effect.util;
import com.github.tonivade.purefun.Nothing;
import com.github.tonivade.purefun.Unit;
import com.github.tonivade.purefun.effect.UIO;
import com.github.tonivade.purefun.effect.ZIO;
import java.time.Duration;
import java.time.OffsetDateTime;
public interface ZClock {
ZClock.Service clock();
static ZIO currentTime() {
return ZIO.accessM(env -> env.clock().currentTime());
}
static ZIO currentDateTime() {
return ZIO.accessM(env -> env.clock().currentDateTime());
}
static ZIO sleep(Duration duration) {
return ZIO.accessM(env -> env.clock().sleep(duration));
}
interface Service {
ZIO currentTime();
ZIO currentDateTime();
ZIO sleep(Duration duration);
}
static ZClock live() {
return new ZClock() {
@Override
public Service clock() {
return new Service() {
@Override
public ZIO currentTime() {
return UIO.task(System::currentTimeMillis).toZIO();
}
@Override
public ZIO currentDateTime() {
return UIO.task(OffsetDateTime::now).toZIO();
}
@Override
public ZIO sleep(Duration duration) {
return UIO.exec(() -> Thread.sleep(duration.toMillis())).toZIO();
}
};
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy