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

Refacto/refacto jackson mappers #65

Merged
merged 3 commits into from
Dec 2, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,65 @@
public class Scenario<R> {

@FunctionalInterface
public interface Function<U, V> {
V apply(final Scenario<U> scenario, U u) throws Exception;
public interface GivenClause {
void given(final Scenario<?> scenario) throws Exception;
}

@FunctionalInterface
public interface Supplier<V> {
V get(final Scenario<?> scenario) throws Exception;
public interface WhenClause<T> {
T when(final Scenario<?> scenario) throws Exception;
}

@FunctionalInterface
public interface Consumer<U> {
void accept(final Scenario<U> scenario, U u) throws Exception;
public interface ThenClause<T> {
void then(final Scenario<?> scenario, final T actual) throws Exception;
}

public static Scenario<Void> noParameters() throws Exception {
return new Scenario<Void>();
public static Scenario<Void> givenNoParameters() throws Exception {
return new Scenario<Void>(new HashMap<>(), null);
}

public static Scenario<Void> withParameters(final Map<String, Object> parameters) throws Exception {
return new Scenario<Void>(parameters);
public static Scenario<Void> givenParameters(final Map<String, Object> parameters) throws Exception {
return new Scenario<Void>(new HashMap<>(parameters), null);
}

private final Map<String, Object> context;

private final R value;

private Scenario() {
this(Map.of(), null);
public static Scenario<Void> givenScenario(final Scenario<?> parent) throws Exception {
return new Scenario<Void>(parent.context, null);
}

private Scenario(final Map<String, Object> context) {
this(context, null);
}
private final Map<String, Object> context;
private final R value;

private Scenario(final Map<String, Object> context, final R value) {
this.context = new HashMap<>(context);
this.context = context;
this.value = value;
}

public Map<String, Object> getContext() {
return this.context;
}

@SuppressWarnings("unchecked")
public <T> Optional<T> get(final String key) {
return Optional.ofNullable((T) this.context.get(key));
}

public <T> Scenario<R> put(final String key, final T value) {
public <T> T put(final String key, final T value) {
this.context.put(key, value);
return this;
return value;
}

public <T> Scenario<T> given(final Supplier<T> step) throws Exception {
return new Scenario<T>(this.context, step.get(this));
public Scenario<R> given(final GivenClause step) throws Exception {
step.given(this);
return this;
}

public <T> Scenario<T> when(final Function<R, T> step) throws Exception {
return new Scenario<T>(this.context, step.apply(this, this.value));
public <T> Scenario<T> when(final WhenClause<T> step) throws Exception {
return new Scenario<T>(this.context, step.when(this));
}

public Scenario<R> then(final Consumer<R> step) throws Exception {
step.accept(this, this.value);
public Scenario<R> then(final ThenClause<R> step) throws Exception {
step.then(this, this.value);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.github.romualdrousseau.archery.commons.dsf.json.jackson;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.io.FileInputStream;
import java.io.IOException;

import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.util.DefaultIndenter;
Expand All @@ -16,23 +13,30 @@
import com.github.romualdrousseau.archery.commons.dsf.DSFArray;
import com.github.romualdrousseau.archery.commons.dsf.DSFFactory;
import com.github.romualdrousseau.archery.commons.dsf.DSFObject;
import com.github.romualdrousseau.archery.commons.io.FileOps;

public class JSONJacksonFactory implements DSFFactory {
private final ObjectMapper mapper;

public JSONJacksonFactory() {
this.mapper = new ObjectMapper();
private static final ThreadLocal<ObjectMapper> OBJECT_MAPPER = new ThreadLocal<>() {
@Override
protected ObjectMapper initialValue() {
final var mapper = new ObjectMapper();

DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
mapper.setDefaultPrettyPrinter(prettyPrinter);
final var prettyPrinter = new DefaultPrettyPrinter();
prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
mapper.setDefaultPrettyPrinter(prettyPrinter);

final StreamReadConstraints streamReadConstraints = StreamReadConstraints
.builder()
.maxStringLength(Integer.MAX_VALUE)
.build();
this.mapper.getFactory().setStreamReadConstraints(streamReadConstraints);
}
final var streamReadConstraints = StreamReadConstraints
.builder()
.maxStringLength(Integer.MAX_VALUE)
.build();
mapper.getFactory().setStreamReadConstraints(streamReadConstraints);

return mapper;
}
};

private final ObjectMapper mapper = OBJECT_MAPPER.get();

public DSFArray newArray() {
return new JSONJacksonArray(this.mapper, this.mapper.createArrayNode());
Expand All @@ -51,7 +55,7 @@ public DSFArray parseArray(final Object object) {
}

public DSFArray loadArray(final Path filePath) {
try (BufferedReader reader = this.createReader(filePath)) {
try (final var reader = FileOps.createBufferedReaderUtfBOM(filePath)) {
return new JSONJacksonArray(this.mapper, this.mapper.readTree(reader));
} catch (final IOException e) {
throw new UncheckedIOException(e);
Expand Down Expand Up @@ -87,7 +91,7 @@ public DSFObject parseObject(final Object object) {
}

public DSFObject loadObject(final Path filePath) {
try (BufferedReader reader = this.createReader(filePath)) {
try (BufferedReader reader = FileOps.createBufferedReaderUtfBOM(filePath)) {
return new JSONJacksonObject(this.mapper, this.mapper.readTree(reader));
} catch (final IOException e) {
throw new UncheckedIOException(e);
Expand All @@ -105,19 +109,4 @@ public void saveObject(final DSFObject o, final Path filePath, final boolean pre
throw new UncheckedIOException(e);
}
}

private BufferedReader createReader(final Path filePath) throws IOException {
final BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(filePath.toFile()), StandardCharsets.UTF_8));

// consume the Unicode BOM (byte order marker) if present
reader.mark(1);
final int c = reader.read();
// if not the BOM, back up to the beginning again
if (c != '\uFEFF') {
reader.reset();
}

return reader;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.github.romualdrousseau.archery.commons.dsf.yaml.jackson;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.io.FileInputStream;
import java.io.IOException;

import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.util.DefaultIndenter;
Expand All @@ -18,25 +15,32 @@
import com.github.romualdrousseau.archery.commons.dsf.DSFArray;
import com.github.romualdrousseau.archery.commons.dsf.DSFFactory;
import com.github.romualdrousseau.archery.commons.dsf.DSFObject;
import com.github.romualdrousseau.archery.commons.io.FileOps;

public class YAMLJacksonFactory implements DSFFactory {
private final ObjectMapper mapper;

public YAMLJacksonFactory() {
this.mapper = YAMLMapper.builder()
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
.build();

DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
mapper.setDefaultPrettyPrinter(prettyPrinter);

final StreamReadConstraints streamReadConstraints = StreamReadConstraints
.builder()
.maxStringLength(Integer.MAX_VALUE)
.build();
this.mapper.getFactory().setStreamReadConstraints(streamReadConstraints);
}

private static final ThreadLocal<YAMLMapper> YAML_MAPPER = new ThreadLocal<>() {
@Override
protected YAMLMapper initialValue() {
final var mapper = YAMLMapper.builder()
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
.build();

final var prettyPrinter = new DefaultPrettyPrinter();
prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
mapper.setDefaultPrettyPrinter(prettyPrinter);

final var streamReadConstraints = StreamReadConstraints
.builder()
.maxStringLength(Integer.MAX_VALUE)
.build();
mapper.getFactory().setStreamReadConstraints(streamReadConstraints);

return mapper;
}
};

private final ObjectMapper mapper = YAML_MAPPER.get();

public DSFArray newArray() {
return new YAMLJacksonArray(this.mapper, this.mapper.createArrayNode());
Expand All @@ -55,7 +59,7 @@ public DSFArray parseArray(final Object object) {
}

public DSFArray loadArray(final Path filePath) {
try (BufferedReader reader = this.createReader(filePath)) {
try (BufferedReader reader = FileOps.createBufferedReaderUtfBOM(filePath)) {
return new YAMLJacksonArray(this.mapper, this.mapper.readTree(reader));
} catch (final IOException e) {
throw new UncheckedIOException(e);
Expand Down Expand Up @@ -92,7 +96,7 @@ public DSFObject parseObject(final Object object) {
}

public DSFObject loadObject(final Path filePath) {
try (BufferedReader reader = this.createReader(filePath)) {
try (BufferedReader reader = FileOps.createBufferedReaderUtfBOM(filePath)) {
return new YAMLJacksonObject(this.mapper, this.mapper.readTree(reader));
} catch (final IOException e) {
throw new UncheckedIOException(e);
Expand All @@ -111,19 +115,4 @@ public void saveObject(final DSFObject o, final Path filePath, final boolean pre
throw new UncheckedIOException(e);
}
}

private BufferedReader createReader(final Path filePath) throws IOException {
final BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(filePath.toFile()), StandardCharsets.UTF_8));

// consume the Unicode BOM (byte order marker) if present
reader.mark(1);
final int c = reader.read();
// if not the BOM, back up to the beginning again
if (c != '\uFEFF') {
reader.reset();
}

return reader;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
package com.github.romualdrousseau.archery.commons.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;

import com.github.romualdrousseau.archery.commons.strings.StringUtils;

public class FileOps {

public static void copyDir(Path src, Path dest) {
public static BufferedReader createBufferedReaderUtfBOM(final Path filePath) throws IOException {
return processUtfBOM(new BufferedReader(
new InputStreamReader(new FileInputStream(filePath.toFile()), StandardCharsets.UTF_8)));
}

public static BufferedReader processUtfBOM(final BufferedReader reader) throws IOException {
reader.mark(1);
if (reader.read() != StringUtils.BOM_CHAR) {
reader.reset(); // skip BOM if present
}
return reader;
}

public static void copyDir(final Path src, final Path dest) {
try {
Files.walk(src).forEach(source -> copyFile(source, dest.resolve(src.relativize(source))));
} catch (final IOException x) {
Expand All @@ -28,11 +47,11 @@ public static void deleteDir(final Path dir) {
}
}

public static void copyFile(Path src, Path dest) {
public static void copyFile(final Path src, final Path dest) {
try {
dest.getParent().toFile().mkdirs();
Files.copy(src, dest);
} catch (IOException x) {
} catch (final IOException x) {
throw new UncheckedIOException(x);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.github.romualdrousseau.archery.base.BaseSheet;
import com.github.romualdrousseau.archery.transform.op.DropColumnsWhenFillRatioLessThan;
import com.github.romualdrousseau.archery.commons.io.FileOps;
import com.github.romualdrousseau.archery.commons.strings.StringUtils;

public class CsvDocument extends BaseDocument {

Expand Down Expand Up @@ -87,7 +86,7 @@ private boolean openWithEncoding(final File txtFile, final String encoding, fina
try {
final var reader = new BufferedReader(new InputStreamReader(new FileInputStream(txtFile), encoding));
if (encoding.startsWith("UTF-")) {
this.processUtfBOM(reader);
FileOps.processUtfBOM(reader);
}
this.sheet = new CsvSheet(sheetName, reader);
this.sheet.checkDataEncoding();
Expand All @@ -96,12 +95,4 @@ private boolean openWithEncoding(final File txtFile, final String encoding, fina
return false;
}
}

private void processUtfBOM(final BufferedReader reader) throws IOException {
// skip BOM if present
reader.mark(1);
if (reader.read() != StringUtils.BOM_CHAR) {
reader.reset();
}
}
}
Loading