mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
Implement Transformer API
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.dfsek.terra.api.translator;
|
||||
|
||||
public class AttemptsFailedException extends RuntimeException {
|
||||
public AttemptsFailedException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public AttemptsFailedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public AttemptsFailedException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public AttemptsFailedException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.dfsek.terra.api.translator;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MapTransform<F, T> implements Transform<F, T> {
|
||||
private final Map<F, T> map;
|
||||
|
||||
public MapTransform(Map<F, T> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public MapTransform() {
|
||||
this.map = new HashMap<>();
|
||||
}
|
||||
|
||||
public MapTransform<F, T> add(F from, T to) {
|
||||
map.put(from, to);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MapTransform<F, T> remove(F from) {
|
||||
map.remove(from);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T transform(F input) throws TransformException {
|
||||
if(!map.containsKey(input)) throw new TransformException("No key matching " + input.toString() + " found in map.");
|
||||
return map.get(input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.dfsek.terra.api.translator;
|
||||
|
||||
/**
|
||||
* Interface to transform data from one type to another.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Transform<F, T> {
|
||||
T transform(F input) throws TransformException;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.dfsek.terra.api.translator;
|
||||
|
||||
public class TransformException extends Exception {
|
||||
public TransformException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TransformException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public TransformException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public TransformException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.dfsek.terra.api.translator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Class to translate types from one style/platform to another.
|
||||
*
|
||||
* @param <F> Data type to transform from.
|
||||
* @param <T> Data type to transform to.
|
||||
*/
|
||||
public class Transformer<F, T> {
|
||||
private final List<Transform<F, T>> transformer;
|
||||
|
||||
private Transformer(List<Transform<F, T>> transformer) {
|
||||
this.transformer = transformer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate data from {@code from} type to {@code to} type.
|
||||
*
|
||||
* @param from Data to translate
|
||||
* @return Result
|
||||
*/
|
||||
public T translate(F from) {
|
||||
List<TransformException> exceptions = new ArrayList<>();
|
||||
for(Transform<F, T> transform : transformer) {
|
||||
try {
|
||||
return transform.transform(from);
|
||||
} catch(TransformException exception) {
|
||||
exceptions.add(exception);
|
||||
}
|
||||
}
|
||||
StringBuilder exBuilder = new StringBuilder("Could not transform input; all attempts failed: ").append(from.toString()).append("\n");
|
||||
for(TransformException exception : exceptions) exBuilder.append(exception.getMessage()).append("\n");
|
||||
throw new AttemptsFailedException(exBuilder.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder pattern for building Transformers
|
||||
*
|
||||
* @param <T>
|
||||
* @param <F>
|
||||
*/
|
||||
public static class Builder<T, F> {
|
||||
private final List<Transform<T, F>> transforms = new ArrayList<>();
|
||||
|
||||
public Builder<T, F> addTransform(Transform<T, F> transform) {
|
||||
transforms.add(transform);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Transformer<T, F> build() {
|
||||
return new Transformer<>(transforms);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user