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

dev.gradleplugins.runnerkit.providers.CharacterEncodingProvider Maven / Gradle / Ivy

There is a newer version: 0.0.148
Show newest version
package dev.gradleplugins.runnerkit.providers;

import dev.gradleplugins.runnerkit.GradleExecutionContext;
import dev.gradleplugins.runnerkit.InvalidRunnerConfigurationException;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Map;

import static java.util.Collections.singletonMap;

public final class CharacterEncodingProvider extends AbstractGradleExecutionProvider implements GradleExecutionJvmSystemPropertyProvider {
    public static CharacterEncodingProvider of(Charset characterEncoding) {
        return fixed(CharacterEncodingProvider.class, characterEncoding);
    }

    public static CharacterEncodingProvider defaultCharset() {
        return fixed(CharacterEncodingProvider.class, Charset.defaultCharset());
    }

    public static CharacterEncodingProvider unset() {
        return noValue(CharacterEncodingProvider.class);
    }

    @Override
    public Map getAsJvmSystemProperties() {
        return map(CharacterEncodingProvider::asJvmSystemProperties).orElseGet(Collections::emptyMap);
    }

    private static Map asJvmSystemProperties(Charset characterEncoding) {
        return singletonMap("file.encoding", characterEncoding.name());
    }

    @Override
    public void validate(GradleExecutionContext context) {
        if (context.getArguments().get().stream().anyMatch(it -> it.startsWith("-Dfile.encoding"))) {
            throw new InvalidRunnerConfigurationException("Please use GradleRunner#withDefaultCharacterEncoding(Charset) instead of using the command line flags.");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy