Skip to content

Commit

Permalink
Allow formatting of any object type
Browse files Browse the repository at this point in the history
To be able to use the `GenerationOptions` formatting
in custom placeholders for resolving single classes,
an additional `tryToFormat` method is added, which
can be used to format any type.
  • Loading branch information
AntonOellerer committed May 3, 2022
1 parent 577be17 commit 8c2835b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '1.4.0-alpha.30'
version = '1.4.0-alpha.31'

sourceCompatibility = "17"
targetCompatibility = "17"
Expand Down
33 changes: 31 additions & 2 deletions src/main/java/com/docutools/jocument/GenerationOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,46 @@ public record GenerationOptions(ImageStrategy imageStrategy,
List<PlaceholderDataFormattingOption> formattingOptions,
BiFunction<String, Locale, Optional<String>> translationFunction) {

/**
* Try to format a {@link PlaceholderData} with the given {@link Locale}.
*
* @param locale The locale to use for formatting
* @param placeholderData The data to format
* @return An {@link Optional} containing the formatted {@link PlaceholderData} if successful, {@link Optional#empty()} otherwise.
*/
public Optional<PlaceholderData> tryToFormat(Locale locale, PlaceholderData placeholderData) {
return formattingOptions.stream()
.filter(option -> option.filter().accepts(placeholderData.getRawValue()))
.findAny()
.findFirst()
.map(option -> option.formatter().format(locale, placeholderData.getRawValue()))
.map(ScalarPlaceholderData::new);

}

/**
* Try to format an object with the given {@link Locale}.
*
* @param locale The locale to use for formatting
* @param toFormat The data to format
* @return An {@link Optional} containing the formatted {@link String} if successful, {@link Optional#empty()} otherwise.
*/
public <T> Optional<String> tryToFormat(Locale locale, T toFormat) {
return formattingOptions.stream()
.filter(option -> option.filter().accepts(toFormat))
.findFirst()
.map(option -> option.formatter().format(locale, toFormat));

}

/**
* Translate a given term to the given locale.
*
* @param term The term to translate
* @param locale The locale to translate the term to
* @return An {@link Optional} containing the translated term if successful, {@link Optional#empty()} otherwise
*/
public Optional<String> translate(String term, Locale locale) {
if(translationFunction != null) {
if (translationFunction != null) {
return translationFunction.apply(term, locale);
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.matchesPattern;

import com.docutools.jocument.GenerationOptionsBuilder;
import com.docutools.jocument.impl.ReflectionResolver;
import com.docutools.jocument.sample.model.SampleModelData;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Optional;
import java.util.regex.Pattern;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@DisplayName("Formatting")
class Formatting {
@Tag("automated")
class FormattingTests {

@Test
@DisplayName("Register custom formatting for temporal types.")
Expand All @@ -35,4 +42,19 @@ void registerCustomFormatterForTemporalTypes() {
assertThat(actual, equalTo(expected));
}

@Test
void formatObjects() {
// Arrange
var dtf = DateTimeFormatter.ofPattern("dd.MM.yyyy");
var options = new GenerationOptionsBuilder()
.format(LocalDate.class, dtf::format)
.build();

// Act
Optional<String> formattedDate = options.tryToFormat(Locale.ENGLISH, LocalDate.now());

// Assert
assertThat(formattedDate.isPresent(), is(true));
assertThat(formattedDate.get(), matchesPattern(Pattern.compile("\\d\\d\\.\\d\\d\\.\\d\\d\\d\\d")));
}
}

0 comments on commit 8c2835b

Please sign in to comment.