Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion source/ceylon/time/Duration.ceylon
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@
import ceylon.time.base { ReadableDuration }

"Duration specifies a discreet amount of milliseconds between two instances of time."
shared serializable class Duration(milliseconds) satisfies ReadableDuration & Scalable<Integer, Duration> {
shared serializable class Duration satisfies ReadableDuration
& Comparable<Duration>
& Summable<Duration>
& Scalable<Integer, Duration>
& Invertible<Duration> {

shared static Duration zero = Duration(0);

"Number of milliseconds of this duration."
shared actual Integer milliseconds;

shared new(Integer milliseconds) {
this.milliseconds = milliseconds;
}

"Returns this duration as a period of milliseconds."
shared Period period => Period { milliseconds = milliseconds; };
Expand All @@ -35,6 +45,14 @@ shared serializable class Duration(milliseconds) satisfies ReadableDuration & Sc
value result = 3;
return prime * result + milliseconds.hash;
}

shared actual Comparison compare(Duration other) => milliseconds.compare(other.milliseconds);

shared actual Duration plus(Duration other) => Duration(milliseconds.plus(other.milliseconds));

shared actual Duration negated => Duration(milliseconds.negated);

shared actual Duration minus(Duration other) => Duration(milliseconds.minus(other.milliseconds));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn’t this be simpler to read:

milliseconds - other.milliseconds

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to make it visible that it just delegates to the corresponding method on the milliseconds, but I won't object changing it if you like. Should I?


"Returns a new [[Duration]] with it´s milliseconds scaled."
shared actual Duration scale(Integer scale) => Duration( scale * milliseconds );
Expand Down
23 changes: 22 additions & 1 deletion test-source/test/ceylon/time/testDuration.ceylon
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,25 @@ import ceylon.time {
];
parameters (`value scalableDurationTests`)
shared test void testScalableDuration(Duration expectedDuration, Integer scale)
=> assertEquals { expected = expectedDuration; actual = scale ** Duration(1000); };
=> assertEquals { actual = scale ** Duration(1000); expected = expectedDuration; };

shared test void testPlus()
=> assertEquals { actual = Duration(-4567) + Duration(4567); expected= Duration.zero; };

shared test void testInv()
=> assertEquals { actual = -Duration(99); expected= Duration(-99); };

shared test void testInvZero()
=> assertEquals { actual = -Duration.zero; expected= Duration.zero; };

shared test void testMinus()
=> assertEquals { actual = Duration(4567) - Duration(4567); expected= Duration.zero; };

shared test void testCompare()
=> assertEquals { actual = Duration(345) <=> Duration(4567); expected= Comparison.smaller; };

shared test void testCompare()
=> assertEquals { actual = Duration(3450) <=> Duration(456); expected= Comparison.greater; };

shared test void testCompare()
=> assertEquals { actual = Duration(345) <=> Duration(345); expected= Comparison.equal; };