Skip to content

Commit

Permalink
Version 1.0.6.
Browse files Browse the repository at this point in the history
`Log.Time`: Auto-correct unix base time and code optimization.
`concurrent.rl`: Better annotation.
  • Loading branch information
ATATC committed Aug 3, 2023
1 parent 1e460f5 commit 2184858
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
apply plugin: 'maven-publish'

group 'com.atatctech'
version '1.0.5'
version '1.0.6'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.jetbrains.annotations.NotNull;

public abstract class RateLimiter {
protected Log.Time lastReplenishment = new Log.Time();
protected @NotNull Log.Time lastReplenishment = new Log.Time();
protected final long unitAmount, interval;
protected final Bucket bucket;

Expand All @@ -15,7 +15,7 @@ public RateLimiter(long unitAmount, long interval) {
bucket = new Bucket(unitAmount, unitAmount);
}

public RateLimiter(long unitAmount, Log.Time.@NotNull TimePeriod interval) {
public RateLimiter(long unitAmount, @NotNull Log.Time.TimePeriod interval) {
this.unitAmount = unitAmount;
this.interval = interval.getMilliseconds();
bucket = new Bucket(unitAmount, unitAmount);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.atatctech.packages.concurrent.rl;

import com.atatctech.packages.log.Log;
import org.jetbrains.annotations.NotNull;

public class SmoothRateLimiter extends RateLimiter {
public SmoothRateLimiter(long unitAmount, long interval) {
super(unitAmount, interval);
}

public SmoothRateLimiter(long unitAmount, Log.Time.TimePeriod interval) {
public SmoothRateLimiter(long unitAmount, @NotNull Log.Time.TimePeriod interval) {
super(unitAmount, interval);
}

Expand Down
34 changes: 24 additions & 10 deletions src/main/java/com/atatctech/packages/log/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public Time() {

public Time(long baseTime) {
if (baseTime < 0) throw new IllegalArgumentException("`baseTime` must be a positive integer.");
if (baseTime < 9999999999L) baseTime = convert(Unit.Second, Unit.Millisecond, baseTime);
this.baseTime = baseTime;
}

Expand Down Expand Up @@ -63,14 +64,21 @@ public static long convert(@NotNull Unit origin, @NotNull Unit result, long valu
}

public static long calculateDuration(@NotNull Runnable action, @NotNull Unit unit) {
return convert(Unit.Millisecond, unit, calculateDuration(action));
}

public static long calculateDuration(@NotNull Runnable action) {
long baseTime = System.currentTimeMillis();
action.run();
long endTime = System.currentTimeMillis();
return (endTime - baseTime) / getUnitCoefficients(unit);
return System.currentTimeMillis() - baseTime;
}

public static long calculateDuration(@NotNull Time time1, @NotNull Time time2, @NotNull Unit unit) {
return convert(Unit.Millisecond, unit, Math.abs(time1.getBaseTime() - time2.getBaseTime()));
return convert(Unit.Millisecond, unit, calculateDuration(time1, time2));
}

public static long calculateDuration(@NotNull Time time1, @NotNull Time time2) {
return Math.abs(time1.getBaseTime() - time2.getBaseTime());
}

public static boolean theSame(@NotNull Time time1, @NotNull Time time2, @NotNull Unit unit) {
Expand Down Expand Up @@ -132,33 +140,39 @@ public boolean isBetween(@NotNull Time time1, @NotNull Time time2) {
}

public long getDuration(@NotNull Unit unit) {
long duration = Math.abs(System.currentTimeMillis() - getBaseTime());
return duration / getUnitCoefficients(unit);
return convert(Unit.Millisecond, unit, getDuration());
}

public long getDuration() {
return Math.abs(System.currentTimeMillis() - getBaseTime());
return getDuration(this);
}

public @NotNull Milliseconds getDurationAsGap() {
return new Milliseconds(getDuration());
}

public long getDuration(@NotNull Time time, @NotNull Unit unit) {
long duration = Math.abs(time.getBaseTime() - getBaseTime());
return duration / getUnitCoefficients(unit);
return convert(Unit.Millisecond, unit, getDuration(time));
}

public long getDuration(@NotNull Time time) {
return Math.abs(time.getBaseTime() - getBaseTime());
}

public @NotNull TimePeriod getDurationAsGap(@NotNull Time time, @NotNull Unit unit) {
return TimePeriod.fromUnit(getDuration(time, unit), unit);
}

public long getBaseTime(@NotNull Unit unit) {
return baseTime / getUnitCoefficients(unit);
return convert(Unit.Millisecond, unit, getBaseTime());
}

public long getBaseTime() {
return getBaseTime(Unit.Millisecond);
return baseTime;
}

public long unix() {
return getBaseTime(Unit.Second);
}

public enum Unit {
Expand Down

0 comments on commit 2184858

Please sign in to comment.