Skip to content

Commit

Permalink
Optimize the code in TimeBounds. (#270)
Browse files Browse the repository at this point in the history
* fix: parameters checking rule in TimeBounds.

* feat: add `TimeBounds.afterNow` and tests for `TimeBounds`

* style: Optimize code style in `TimeBounds`.

* rename `TimeBounds.afterNow` to `TimeBounds.expiresAfter`.
  • Loading branch information
overcat authored Mar 30, 2020
1 parent da89c6c commit 48b28e6
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 13 deletions.
47 changes: 34 additions & 13 deletions src/main/java/org/stellar/sdk/TimeBounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,44 @@
final public class TimeBounds {
final private long mMinTime;
final private long mMaxTime;

/**
* @param minTime 64bit Unix timestamp
* @param maxTime 64bit Unix timestamp
*/
public TimeBounds(long minTime, long maxTime) {
if (maxTime > 0 && minTime >= maxTime) {
if (minTime < 0) {
throw new IllegalArgumentException("minTime cannot be negative");
}

if (maxTime < 0) {
throw new IllegalArgumentException("maxTime cannot be negative");
}

if (maxTime != 0 && minTime > maxTime) {
throw new IllegalArgumentException("minTime must be >= maxTime");
}

mMinTime = minTime;
mMaxTime = maxTime;
}


/**
* A factory method that sets maxTime to the specified second from now.
*
* @param timeout Timeout in seconds.
* @return TimeBounds
*/
public static TimeBounds expiresAfter(long timeout) {
long now = System.currentTimeMillis() / 1000L;
long endTime = now + timeout;
return new TimeBounds(0, endTime);
}

public long getMinTime() {
return mMinTime;
}

public long getMaxTime() {
return mMaxTime;
}
Expand All @@ -39,8 +59,8 @@ public static TimeBounds fromXdr(org.stellar.sdk.xdr.TimeBounds timeBounds) {
}

return new TimeBounds(
timeBounds.getMinTime().getTimePoint().getUint64().longValue(),
timeBounds.getMaxTime().getTimePoint().getUint64().longValue()
timeBounds.getMinTime().getTimePoint().getUint64(),
timeBounds.getMaxTime().getTimePoint().getUint64()
);
}

Expand All @@ -61,13 +81,14 @@ public org.stellar.sdk.xdr.TimeBounds toXdr() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TimeBounds that = (TimeBounds) o;

if (mMinTime != that.mMinTime) return false;
return mMaxTime == that.mMaxTime;
return mMinTime == that.mMinTime && mMaxTime == that.mMaxTime;
}


Expand Down
75 changes: 75 additions & 0 deletions src/test/java/org/stellar/sdk/TimeBoundsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.stellar.sdk;

import org.junit.Test;

import static org.junit.Assert.*;

public class TimeBoundsTest {
@Test
public void testSetTimeBoundsNegativeMinTime() {
try {
new TimeBounds(-1, 300);
fail();
} catch (IllegalArgumentException e) {
assertEquals("minTime cannot be negative", e.getMessage());
}
}

@Test
public void testSetTimeBoundsNegativeMaxTime() {
try {
new TimeBounds(1, -300);
fail();
} catch (IllegalArgumentException e) {
assertEquals("maxTime cannot be negative", e.getMessage());
}
}

@Test
public void testSetTimeBoundsMinTimeGreatThanMaxTime() {
try {
new TimeBounds(300, 1);
fail();
} catch (IllegalArgumentException e) {
assertEquals("minTime must be >= maxTime", e.getMessage());
}
}

@Test
public void TestSetTimeoutInfinite() {
TimeBounds timebounds = new TimeBounds(300, 0);
assertEquals(300, timebounds.getMinTime());
assertEquals(0, timebounds.getMaxTime());
}

@Test
public void TestSetTimeoutInfiniteBothZero() {
TimeBounds timebounds = new TimeBounds(0, 0);
assertEquals(0, timebounds.getMinTime());
assertEquals(0, timebounds.getMaxTime());
}

@Test
public void TestSetTimeout() {
TimeBounds timebounds = new TimeBounds(1, 300);
assertEquals(1, timebounds.getMinTime());
assertEquals(300, timebounds.getMaxTime());
}

@Test
public void TestSetTimeoutMinEqualMax() {
TimeBounds timebounds = new TimeBounds(300, 300);
assertEquals(300, timebounds.getMinTime());
assertEquals(300, timebounds.getMaxTime());
}

@Test
public void TestTimeoutWithTimeout() {
long timeout = 300;
TimeBounds timebounds = TimeBounds.expiresAfter(timeout);
long now = System.currentTimeMillis() / 1000L;
assertEquals(0, timebounds.getMinTime());
assertTrue(timebounds.getMaxTime() - timeout <= now && timebounds.getMaxTime() - timeout >= now - 1);
}
}

0 comments on commit 48b28e6

Please sign in to comment.