All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.harmonysoft.oss.traute.javac.text.ExceptionTextGeneratorImpl Maven / Gradle / Ivy

There is a newer version: 1.1.10
Show newest version
package tech.harmonysoft.oss.traute.javac.text;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class ExceptionTextGeneratorImpl implements ExceptionTextGenerator {

    private final List> generators = new ArrayList<>();

    @NotNull private final Class contextClass;

    public ExceptionTextGeneratorImpl(@NotNull Class contextClass, @NotNull List> generators) {
        this.contextClass = contextClass;
        this.generators.addAll(generators);
    }

    @SuppressWarnings("unchecked")
    @NotNull
    @Override
    public String generate(@NotNull Object context) {
        if (!contextClass.isInstance(context)) {
            throw new IllegalStateException(String.format(
                    "Can't generate exception text - expected to get a context of type %s but got %s",
                    contextClass.getName(), context.getClass().getName()
            ));
        }
        T typedContext = (T) context;
        StringBuilder buffer = new StringBuilder();
        for (Function generator : generators) {
            buffer.append(generator.apply(typedContext));
        }
        return buffer.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy