Upload files to "java/base"
This commit is contained in:
60
java/base/Log.java
Normal file
60
java/base/Log.java
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package base;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||||
|
*/
|
||||||
|
public class Log {
|
||||||
|
|
||||||
|
private final Pattern LINES = Pattern.compile("\n");
|
||||||
|
private int indent;
|
||||||
|
|
||||||
|
public static Supplier<Void> action(final Runnable action) {
|
||||||
|
return () -> {
|
||||||
|
action.run();
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void scope(final String name, final Runnable action) {
|
||||||
|
scope(name, action(action));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T scope(final String name, final Supplier<T> action) {
|
||||||
|
println(name);
|
||||||
|
indent++;
|
||||||
|
try {
|
||||||
|
return silentScope(name, action);
|
||||||
|
} finally {
|
||||||
|
indent--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T silentScope(
|
||||||
|
final String ignoredName,
|
||||||
|
final Supplier<T> action
|
||||||
|
) {
|
||||||
|
return action.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("UseOfSystemOutOrSystemErr")
|
||||||
|
public void println(final Object value) {
|
||||||
|
for (final String line : LINES.split(String.valueOf(value))) {
|
||||||
|
System.out.println(indent() + line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void format(final String format, final Object... args) {
|
||||||
|
println(String.format(format, args));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String indent() {
|
||||||
|
return " ".repeat(indent);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int getIndent() {
|
||||||
|
return indent;
|
||||||
|
}
|
||||||
|
}
|
||||||
35
java/base/MainChecker.java
Normal file
35
java/base/MainChecker.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package base;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("StaticMethodOnlyUsedInOneClass")
|
||||||
|
public final class MainChecker {
|
||||||
|
|
||||||
|
private final Runner runner;
|
||||||
|
|
||||||
|
public MainChecker(final Runner runner) {
|
||||||
|
this.runner = runner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> run(final TestCounter counter, final String... input) {
|
||||||
|
return runner.run(counter, input);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> run(
|
||||||
|
final TestCounter counter,
|
||||||
|
final List<String> input
|
||||||
|
) {
|
||||||
|
return runner.run(counter, input);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testEquals(
|
||||||
|
final TestCounter counter,
|
||||||
|
final List<String> input,
|
||||||
|
final List<String> expected
|
||||||
|
) {
|
||||||
|
runner.testEquals(counter, input, expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
java/base/Named.java
Normal file
15
java/base/Named.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||||
|
*/
|
||||||
|
public record Named<T>(String name, T value) {
|
||||||
|
public static <T> Named<T> of(final String name, final T f) {
|
||||||
|
return new Named<>(name, f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
51
java/base/Pair.java
Normal file
51
java/base/Pair.java
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package base;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.BinaryOperator;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||||
|
*/
|
||||||
|
@SuppressWarnings({ "StaticMethodOnlyUsedInOneClass", "unused" })
|
||||||
|
public record Pair<F, S>(F first, S second) {
|
||||||
|
public static <F, S> Pair<F, S> of(final F first, final S second) {
|
||||||
|
return new Pair<>(first, second);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <F, S> Pair<F, S> of(final Map.Entry<F, S> e) {
|
||||||
|
return of(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <F, S> UnaryOperator<Pair<F, S>> lift(
|
||||||
|
final UnaryOperator<F> f,
|
||||||
|
final UnaryOperator<S> s
|
||||||
|
) {
|
||||||
|
return p -> of(f.apply(p.first), s.apply(p.second));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <F, S> BinaryOperator<Pair<F, S>> lift(
|
||||||
|
final BinaryOperator<F> f,
|
||||||
|
final BinaryOperator<S> s
|
||||||
|
) {
|
||||||
|
return (p1, p2) ->
|
||||||
|
of(f.apply(p1.first, p2.first), s.apply(p1.second, p2.second));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T, F, S> Function<T, Pair<F, S>> tee(
|
||||||
|
final Function<? super T, ? extends F> f,
|
||||||
|
final Function<? super T, ? extends S> s
|
||||||
|
) {
|
||||||
|
return t -> of(f.apply(t), s.apply(t));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "(" + first + ", " + second + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
public <R> Pair<F, R> second(final R second) {
|
||||||
|
return new Pair<>(first, second);
|
||||||
|
}
|
||||||
|
}
|
||||||
7
java/base/package-info.java
Normal file
7
java/base/package-info.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* Common homeworks test classes
|
||||||
|
* of <a href="https://www.kgeorgiy.info/courses/prog-intro/">Introduction to Programming</a> course.
|
||||||
|
*
|
||||||
|
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||||
|
*/
|
||||||
|
package base;
|
||||||
Reference in New Issue
Block a user