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

xapi.time.impl.AbstractTimeService Maven / Gradle / Ivy

Go to download

Everything needed to run a comprehensive dev environment. Just type X_ and pick a service from autocomplete; new dev modules will be added as they are built. The only dev service not included in the uber jar is xapi-dev-maven, as it includes all runtime dependencies of maven, adding ~4 seconds to build time, and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).

The newest version!
package xapi.time.impl;

import xapi.time.api.Moment;
import xapi.time.service.TimeService;

public abstract class AbstractTimeService extends ImmutableMoment implements TimeService {

  private static final long serialVersionUID = 1130197439830993337L;

  public AbstractTimeService() {
    super(System.currentTimeMillis());
  }

  protected double now;
  private double delta;

  @Override
  public double birth() {
    return super.millis();
  }

  @Override
  public Moment now() {
    return new ImmutableMoment(System.currentTimeMillis());
  }

  @Override
  public Moment clone(Moment moment) {
    return new ImmutableMoment(moment.millis());
  }

  @Override
  public double millis() {
    return now;
  }

  @Override
  public Moment nowPlusOne() {
    double later;
    // locks on static var
    synchronized (TimeService.second_to_nano) {
      later = now+(delta += TimeService.second_to_nano);// ensured unique
    }
    return new ImmutableMoment(later);
  }

  @Override
  public void tick() {
    // low precision, but deterministic and atomic
    now = System.currentTimeMillis();
    delta = 0;
  }
  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy