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

de.team33.patterns.arbitrary.mimas.Util Maven / Gradle / Ivy

Go to download

Provides classes that support the generation of arbitrary values of virtually any type.

The newest version!
package de.team33.patterns.arbitrary.mimas;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

final class Util {

    @SuppressWarnings("SpellCheckingInspection")
    static final String STD_CHARACTERS = "0123456789_abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ " +
                                         "!#$§%&*+,.?@äöüÄÖÜß";
    static final long MAX_RETRY = 16;
    static final BigInteger MIN_LONG = BigInteger.valueOf(Long.MIN_VALUE);
    static final BigInteger MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE);

    private static final String NEWLINE = String.format("%n");
    private static final String NO_RESOURCE = "Should not happen:" +
            " <%s> is not a valid resource or is not accessible in the context <%s>";

    private Util() {
    }

    static String load(final Class context, final String resource) {
        try (final InputStream in = context.getResourceAsStream(resource)) {
            return load(in);
        } catch (final IOException | RuntimeException e) {
            throw new UnfitConditionException(String.format(NO_RESOURCE, resource, context), e);
        }
    }

    private static String load(final InputStream stream) throws IOException {
        try (final Reader in = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
            return load(in);
        }
    }

    private static String load(final Reader reader) throws IOException {
        try (final BufferedReader in = new BufferedReader(reader)) {
            return in.lines()
                     .collect(Collectors.joining(NEWLINE));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy