Skip to content

Commit

Permalink
Refactor AbstractQuery class
Browse files Browse the repository at this point in the history
  • Loading branch information
otaviojava committed Feb 9, 2015
1 parent 061d823 commit 491a7dc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 46 deletions.
3 changes: 2 additions & 1 deletion src/main/java/javax/money/AbstractContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package javax.money;

import java.time.LocalDateTime;
import java.time.temporal.TemporalAccessor;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -215,7 +216,7 @@ public B setTimestampMillis(long timestamp){
* @return this instance for chaining
* @see #setTimestampMillis(long)
*/
public B setTimestamp(TemporalAccessor timestamp){
public B setTimestamp(LocalDateTime timestamp) {
set(AbstractContext.KEY_TIMESTAMP, Objects.requireNonNull(timestamp));
return (B) this;
}
Expand Down
33 changes: 15 additions & 18 deletions src/main/java/javax/money/AbstractQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
package javax.money;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.util.*;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* Represents a general context of data targeting an item of type {@code Q}. Contexts are used to add arbitrary
Expand Down Expand Up @@ -81,31 +86,23 @@ public Class<?> getTargetType() {
*
* @return the timestamp in millis, or null.
*/
public Long getTimestampMillis() {
Object value = get(KEY_QUERY_TIMESTAMP, Object.class);
if (value instanceof Long) {
return (Long) value;
} else if (value instanceof TemporalAccessor) {
TemporalAccessor acc = (TemporalAccessor) value;
return (acc.getLong(ChronoField.INSTANT_SECONDS) * 1000L) + acc.getLong(ChronoField.MILLI_OF_SECOND);
@Override
public Long getTimestampMillis() {
LocalDateTime value = getTimestamp();
if (Objects.nonNull(value)) {
return Date.from(value.atZone(ZoneId.systemDefault()).toInstant())
.getTime();
}
return null;
}

/**
* Get the current timestamp. If not set it tries to of an Instant from #getTimestampMillis().
*
* @return the current timestamp, or null.
*/
public TemporalAccessor getTimestamp() {
Object value = get(KEY_QUERY_TIMESTAMP, Object.class);
if (value instanceof TemporalAccessor) {
return (TemporalAccessor) value;
} else if (value instanceof Long) {
Long ts = (Long) value;
return Instant.ofEpochMilli(ts);
}
return null;
@Override
public LocalDateTime getTimestamp() {
return get(KEY_QUERY_TIMESTAMP, LocalDateTime.class);
}

}
49 changes: 29 additions & 20 deletions src/main/java/javax/money/AbstractQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
*/
package javax.money;

import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalUnit;
import java.util.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* This abstract class defines the common generic parts of a query. Queries are used to pass complex parameters sets
Expand Down Expand Up @@ -64,11 +69,20 @@ public B setProviders(List<String> providers) {
* @return this instance for chaining
* @see #setTimestampMillis(long)
*/
public B setTimestamp(TemporalAccessor timestamp) {
set(AbstractQuery.KEY_QUERY_TIMESTAMP, Objects.requireNonNull(timestamp));
return (B) this;
}
@Override
public B setTimestamp(LocalDateTime timestamp) {
set(AbstractQuery.KEY_QUERY_TIMESTAMP,
Objects.requireNonNull(timestamp));
return (B) this;
}

public B setTimestamp(LocalDate timestamp) {
return setTimestamp(timestamp.atTime(LocalTime.now()));
}

public B setTimestamp(LocalTime timestamp) {
return setTimestamp(LocalDate.now().atTime(timestamp));
}
/**
* Set the providers to be considered. If not set explicitly the <i>default</i> providers and the corresponding
* default ordering are used.
Expand Down Expand Up @@ -97,18 +111,12 @@ public B setProvider(String provider) {
* @param timestamp the target timestamp
* @return the query builder for chaining.
*/
public B setTimestampMillis(long timestamp) {
return set(AbstractQuery.KEY_QUERY_TIMESTAMP, timestamp);
}

/**
* Sets the target timestamp as {@link java.time.temporal.TemporalUnit}.
*
* @param timestamp the target timestamp
* @return the query builder for chaining.
*/
public B setTimestamp(TemporalUnit timestamp) {
return set(AbstractQuery.KEY_QUERY_TIMESTAMP, timestamp);
@Override
public B setTimestampMillis(long timestamp) {
Date date = new Date(timestamp);
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(),
ZoneId.systemDefault());
return set(AbstractQuery.KEY_QUERY_TIMESTAMP, localDateTime);
}

/**
Expand All @@ -131,6 +139,7 @@ public B setTargetType(Class<?> type) {
*
* @return a new {@link AbstractQuery}. never {@code null}.
*/
public abstract C build();
@Override
public abstract C build();

}
12 changes: 8 additions & 4 deletions src/test/java/javax/money/AbstractContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
*/
package javax.money;

import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.testng.Assert.*;
import org.testng.annotations.Test;

/**
* Tests for {@link javax.money.AbstractContext}.
Expand Down Expand Up @@ -132,7 +136,7 @@ public void testGetTimestampMillis() {

@Test
public void testGetTimestamp() {
Instant now = Instant.now();
LocalDateTime now = LocalDateTime.now();
TestContext ctx = new TestContext.Builder().setTimestamp(now).build();
assertEquals(now, ctx.getTimestamp());
}
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/javax/money/AbstractQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.testng.annotations.Test;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;

import static org.testng.Assert.*;
Expand Down Expand Up @@ -59,16 +60,18 @@ public void testGetTimestampMillis() throws Exception {
b.setTimestampMillis(200L);
AbstractQuery query = b.build();
assertEquals(query.getTimestampMillis().longValue(), 200L);
assertEquals(query.getTimestamp(), Instant.ofEpochMilli(200L));
LocalDateTime localDateTime = LocalDateTime
.parse("1969-12-31T21:00:00.200");
assertEquals(query.getTimestamp(), localDateTime);
}

@Test
public void testGetTimestamp() throws Exception {
AbstractQueryBuilder b = createBuilder();
Instant instant = Instant.now();
LocalDateTime instant = LocalDateTime.now();
b.setTimestamp(instant);
AbstractQuery query = b.build();
assertEquals(query.getTimestamp(), instant);
assertEquals(query.getTimestampMillis().longValue(), instant.toEpochMilli());
assertEquals(query.getTimestamp(), instant);
}
}

0 comments on commit 491a7dc

Please sign in to comment.