Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature suggestions from Habib Pleines to PR #10

Merged
merged 4 commits into from
Aug 8, 2017
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## [freemarker-java8-1.1.6](https://github.com/amedia/freemarker-java-8/tree/freemarker-java8-1.1.6) (2017-07-24)
[Full Changelog](https://github.com/amedia/freemarker-java-8/compare/freemarker-java8-1.1.5...freemarker-java8-1.1.6)

- Added isEqual, isAfter and isBefore methods to adapters for LocalDate, LocalDateTime and Localtime (Code contributed by @kingmaoam)

## [freemarker-java8-1.1.5](https://github.com/amedia/freemarker-java-8/tree/freemarker-java8-1.1.5) (2016-12-19)
[Full Changelog](https://github.com/amedia/freemarker-java-8/compare/freemarker-java8-1.1.4...freemarker-java8-1.1.5)

Expand Down
22 changes: 21 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ new https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter

This is not a perfect solution as we cannot add built-ins to FreeMarker from outside code. But its the best we can do until FreeMarker adds full support in the future.

Our Cucumber feature test spec will hopefully explain how it all works : https://github.com/amedia/freemarker-java-8/blob/master/src/test/resources/no/api/freemarker/java8/time/datetime.feature
Out Cucumber feature test spec will hopefully explain how it all works : https://github.com/amedia/freemarker-java-8/blob/master/src/test/resources/no/api/freemarker/java8/time/

## Requirements

Expand Down Expand Up @@ -131,3 +131,23 @@ for formatting.
|Prints the ZoneOffset display name. You can override the textstyle with one of these values [FULL, FULL_STANDALONE, SHORT, SHORT_STANDALONE, NARROW and NARROW_STANDALONE]. You can also override the locale, but Java only seems to have locale support for a few languages.
|${myzoneoffset.format()}` or `${myzoneoffset.format('short')}` or `${myzoneoffset.format('short', 'no-NO')}
|===

[cols="^,^,^,^", options="header"]
.java.time comparison methods
|===
| java.time class | methods | comment | example

|LocalDate
|`isEqual(<LocalDate object>), isAfter(<LocalDate object>), isBefore(<LocalDate object>)`
|Can compare two LocalDate objects for equality.
|`${localDate.isEqual(anotherlocalDate)} or ${localDate.isAfter(anotherlocalDate)} or ${localDate.isBefore(anotherlocalDate)}`

|LocalDateTime
|`isEqual(<LocalDateTime object>), isAfter(<LocalDateTime object>), isBefore(<LocalDateTime object>)`
|Can compare two LocalDateTime objects for equality.
|`${localDateTime.isEqual(anotherlocalDateTime)} or ${localDateTime.isAfter(anotherlocalDateTime)} or ${localDateTime.isBefore(anotherlocalDateTime)}`

|LocalTime
|`isEqual(<LocalTime object>), isAfter(<LocalTime object>), isBefore(<LocalTime object>)`
|Can compare two LocalTime objects for equality.
|`${localTime.isEqual(anotherlocalTime)} or ${localTime.isAfter(anotherlocalTime)} or ${localTime.isBefore(anotherlocalTime)}`
22 changes: 22 additions & 0 deletions src/main/java/no/api/freemarker/java8/time/AbstractChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package no.api.freemarker.java8.time;

/**
* Abstract checker class.
*
* Adapters supporting checkers will extend this class.
*
* @param <E>
* The java.time class this formatter handles.
*/
public abstract class AbstractChecker<E> {

private E obj;

public AbstractChecker(E obj) {
this.obj = obj;
}

public E getObject() {
return obj;
}
}
3 changes: 3 additions & 0 deletions src/main/java/no/api/freemarker/java8/time/DateTimeTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
*/
public final class DateTimeTools {

public static final String METHOD_EQUALS = "isEqual";
public static final String METHOD_BEFORE = "isBefore";
public static final String METHOD_AFTER = "isAfter";
public static final String METHOD_FORMAT = "format";
public static final String METHOD_DAYS = "days";
public static final String METHOD_MONTHS = "months";
Expand Down
30 changes: 27 additions & 3 deletions src/main/java/no/api/freemarker/java8/time/LocalDateAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import java.time.format.DateTimeFormatter;
import java.util.List;

import static no.api.freemarker.java8.time.DateTimeTools.METHOD_FORMAT;
import static no.api.freemarker.java8.time.DateTimeTools.METHOD_UNKNOWN_MSG;
import static no.api.freemarker.java8.time.DateTimeTools.createDateTimeFormatter;
import static no.api.freemarker.java8.time.DateTimeTools.*;

/**
* LocalDateAdapter adds basic format support for {@link LocalDate} too FreeMarker 2.3.23 and above.
Expand All @@ -45,6 +43,8 @@ public LocalDateAdapter(LocalDate obj) {
public TemplateModel get(String s) throws TemplateModelException {
if (METHOD_FORMAT.equals(s)) {
return new LocalDateFormatter(getObject());
} else if(METHOD_EQUALS.equals(s) || METHOD_AFTER.equals(s) || METHOD_BEFORE.equals(s)) {
return new LocalDateChecker(getObject(), s);
}
throw new TemplateModelException(METHOD_UNKNOWN_MSG + s);
}
Expand All @@ -60,4 +60,28 @@ public Object exec(List list) throws TemplateModelException {
return getObject().format(createDateTimeFormatter(list, 0, DateTimeFormatter.ISO_LOCAL_DATE));
}
}

public class LocalDateChecker extends AbstractChecker<LocalDate> implements TemplateMethodModelEx {
private String method;

public LocalDateChecker(LocalDate obj, String method) {
super(obj);
this.method = method;
}

@SuppressWarnings("Duplicates")
@Override
public Object exec(List list) throws TemplateModelException {
LocalDateAdapter adapter = (LocalDateAdapter) list.get(0);
switch(method) {
case METHOD_EQUALS:
return getObject().isEqual(adapter.getObject());
case METHOD_AFTER:
return getObject().isAfter(adapter.getObject());
case METHOD_BEFORE:
return getObject().isBefore(adapter.getObject());
}
throw new TemplateModelException("method not implemented");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import java.time.format.DateTimeFormatter;
import java.util.List;

import static no.api.freemarker.java8.time.DateTimeTools.METHOD_FORMAT;
import static no.api.freemarker.java8.time.DateTimeTools.METHOD_UNKNOWN_MSG;
import static no.api.freemarker.java8.time.DateTimeTools.createDateTimeFormatter;
import static no.api.freemarker.java8.time.DateTimeTools.*;

/**
* LocalDateTimeAdapter adds basic format support for {@link LocalDateTime} too FreeMarker 2.3.23 and above.
Expand All @@ -46,6 +44,8 @@ public LocalDateTimeAdapter(LocalDateTime obj) {
public TemplateModel get(String s) throws TemplateModelException {
if (METHOD_FORMAT.equals(s)) {
return new LocalDateTimeFormatter(getObject());
} else if(METHOD_EQUALS.equals(s) || METHOD_AFTER.equals(s) || METHOD_BEFORE.equals(s)) {
return new LocalDateTimeChecker(getObject(), s);
}
throw new TemplateModelException(METHOD_UNKNOWN_MSG + s);
}
Expand All @@ -61,4 +61,28 @@ public Object exec(List list) throws TemplateModelException {
return getObject().format(createDateTimeFormatter(list, 0, DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
}
}

public class LocalDateTimeChecker extends AbstractChecker<LocalDateTime> implements TemplateMethodModelEx {
private String method;

public LocalDateTimeChecker(LocalDateTime obj, String method) {
super(obj);
this.method = method;
}

@SuppressWarnings("Duplicates")
@Override
public Object exec(List list) throws TemplateModelException {
LocalDateTimeAdapter adapter = (LocalDateTimeAdapter) list.get(0);
switch(method) {
case METHOD_EQUALS:
return getObject().isEqual(adapter.getObject());
case METHOD_AFTER:
return getObject().isAfter(adapter.getObject());
case METHOD_BEFORE:
return getObject().isBefore(adapter.getObject());
}
throw new TemplateModelException("method not implemented");
}
}
}
29 changes: 26 additions & 3 deletions src/main/java/no/api/freemarker/java8/time/LocalTimeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import java.time.format.DateTimeFormatter;
import java.util.List;

import static no.api.freemarker.java8.time.DateTimeTools.METHOD_FORMAT;
import static no.api.freemarker.java8.time.DateTimeTools.METHOD_UNKNOWN_MSG;
import static no.api.freemarker.java8.time.DateTimeTools.createDateTimeFormatter;
import static no.api.freemarker.java8.time.DateTimeTools.*;

/**
* LocalTimeAdapter adds basic format support for {@link LocalTime} too FreeMarker 2.3.23 and above.
Expand All @@ -45,6 +43,8 @@ public LocalTimeAdapter(LocalTime obj) {
public TemplateModel get(String s) throws TemplateModelException {
if (METHOD_FORMAT.equals(s)) {
return new LocalTimeFormatter(getObject());
} else if(METHOD_EQUALS.equals(s) || METHOD_AFTER.equals(s) || METHOD_BEFORE.equals(s)) {
return new LocalTimeChecker(getObject(), s);
}
throw new TemplateModelException(METHOD_UNKNOWN_MSG + s);
}
Expand All @@ -60,4 +60,27 @@ public Object exec(List list) throws TemplateModelException {
return getObject().format(createDateTimeFormatter(list, 0, DateTimeFormatter.ISO_LOCAL_TIME));
}
}

public class LocalTimeChecker extends AbstractChecker<LocalTime> implements TemplateMethodModelEx {
private String method;

public LocalTimeChecker(LocalTime obj, String method) {
super(obj);
this.method = method;
}

@Override
public Object exec(List list) throws TemplateModelException {
LocalTimeAdapter adapter = (LocalTimeAdapter) list.get(0);
switch(method) {
case METHOD_EQUALS:
return getObject().equals(adapter.getObject());
case METHOD_AFTER:
return getObject().isAfter(adapter.getObject());
case METHOD_BEFORE:
return getObject().isBefore(adapter.getObject());
}
throw new TemplateModelException("method not implemented");
}
}
}
36 changes: 36 additions & 0 deletions src/test/java/no/api/freemarker/java8/time/DateTimeStepdefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class DateTimeStepdefs {

private Object obj;

private Object obj2;

public DateTimeStepdefs() {
this.configuration = new Configuration(VERSION_2_3_23);
this.configuration.setObjectWrapper(new Java8ObjectWrapper(VERSION_2_3_23));
Expand Down Expand Up @@ -89,9 +91,28 @@ public void expect_the_template_to_return_the_current_year() throws Throwable {
}
}

@Then("^expect the template to return true$")
public void expect_the_template_to_true() throws Throwable {
Writer out = process("true");
if (!"true".equals(out.toString())) {
Assert.fail("Expected 'true', but got '" + out.toString());
}
}

@Then("^expect the template to return false$")
public void expect_the_template_to_false() throws Throwable {
Writer out = process("false");
if (!"false".equals(out.toString())) {
Assert.fail("Expected 'false', but got '" + out.toString());
}
}

private Writer process(String res) throws IOException, TemplateException {
Map<String, Object> map = new HashMap<>();
map.put("obj", obj);
if (obj2 != null) {
map.put("obj2", obj2);
}
Template t = new Template("name", new StringReader(template), configuration);
Writer out = new StringWriter();
t.process(map, out);
Expand Down Expand Up @@ -137,16 +158,31 @@ public void localdate_object_for(String arg1) throws Throwable {
obj = LocalDate.parse(arg1);
}

@Given("^LocalDate object2 for \"([^\"]*)\"$")
public void localdate_object2_for(String arg1) throws Throwable {
obj2 = LocalDate.parse(arg1);
}

@Given("^LocalDateTime object for \"([^\"]*)\"$")
public void localdatetime_object_for(String arg1) throws Throwable {
obj = LocalDateTime.parse(arg1);
}

@Given("^LocalDateTime object2 for \"([^\"]*)\"$")
public void localdatetime_object2_for(String arg1) throws Throwable {
obj2 = LocalDateTime.parse(arg1);
}

@Given("^LocalTime object for \"([^\"]*)\"$")
public void localtime_object_for(String arg1) throws Throwable {
obj = LocalTime.parse(arg1);
}

@Given("^LocalTime object2 for \"([^\"]*)\"$")
public void localtime_object2_for(String arg1) throws Throwable {
obj2 = LocalTime.parse(arg1);
}

@Given("^MonthDay object for \"([^\"]*)\"$")
public void monthday_object_for(String arg1) throws Throwable {
obj = MonthDay.parse(arg1);
Expand Down
Loading