You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.3 KiB
44 lines
1.3 KiB
package command;
|
|
|
|
import exception.CrawlerException;
|
|
import model.ResultContainer;
|
|
|
|
import java.util.List;
|
|
import java.util.function.Function;
|
|
import java.util.function.Supplier;
|
|
|
|
public class CommandWrapper<T> {
|
|
private final String name;
|
|
private final Supplier<ResultContainer<T>> action;
|
|
private final Function<T, String> formatter;
|
|
|
|
private CommandWrapper(String name, Supplier<ResultContainer<T>> action,
|
|
Function<T, String> formatter) {
|
|
this.name = name;
|
|
this.action = action;
|
|
this.formatter = formatter;
|
|
}
|
|
|
|
public static <T> CommandWrapper<T> create(String name,
|
|
Supplier<ResultContainer<T>> action) {
|
|
return new CommandWrapper<>(name, action, Object::toString);
|
|
}
|
|
|
|
public static <T> CommandWrapper<T> create(String name,
|
|
Supplier<ResultContainer<T>> action,
|
|
Function<T, String> formatter) {
|
|
return new CommandWrapper<>(name, action, formatter);
|
|
}
|
|
|
|
public ResultContainer<T> execute() {
|
|
return action.get();
|
|
}
|
|
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
public String format(T result) {
|
|
return formatter.apply(result);
|
|
}
|
|
}
|