Java Code Examples for org.cactoos.Text#asString()

The following examples show how to use org.cactoos.Text#asString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqlParsed.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param sql SQL query
 * @param params SQL query parameters
 */
public SqlParsed(final Text sql, final Params params) {
    this.sql = new Sticky<>(
        () -> {
            final String str = sql.asString();
            final List<String> names = new LinkedList<>();
            final Pattern find = Pattern.compile("(?<!')(:[\\w]*)(?!')");
            final Matcher matcher = find.matcher(str);
            while (matcher.find()) {
                names.add(matcher.group().substring(1));
            }
            for (int idx = 0; idx < names.size(); ++idx) {
                if (!params.contains(names.get(idx), idx)) {
                    throw new IllegalArgumentException(
                        new FormattedText(
                            "SQL parameter #%d is wrong or out of order",
                            idx + 1
                        ).asString()
                    );
                }
            }
            return str.replaceAll(find.pattern(), "?");
        }
    );
}
 
Example 2
Source File: Abbreviated.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param text The Text
 * @param max Max width of the result string
 */
@SuppressWarnings({
    "PMD.CallSuperInConstructor",
    "PMD.ConstructorOnlyInitializesOrCallOtherConstructors"
    })
public Abbreviated(final Text text, final int max) {
    super((Scalar<String>) () -> {
        final Text abbreviated;
        if (text.asString().length() <= max) {
            abbreviated = text;
        } else {
            abbreviated = new FormattedText(
                "%s...",
                new Sub(
                    text,
                    0,
                    max - Abbreviated.ELLIPSES_WIDTH
                ).asString()
            );
        }
        return abbreviated.asString();
    });
}
 
Example 3
Source File: Rotated.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param text The text
 * @param shift The shift
 */
@SuppressWarnings({"PMD.CallSuperInConstructor",
    "PMD.ConstructorOnlyInitializesOrCallOtherConstructors"})
public Rotated(final Text text, final int shift) {
    super((Scalar<String>) () -> {
        String origin = text.asString();
        final int length = origin.length();
        if (length != 0 && shift != 0 && shift % length != 0) {
            final StringBuilder builder = new StringBuilder(length);
            int offset = -(shift % length);
            if (offset < 0) {
                offset = origin.length() + offset;
            }
            origin = builder.append(
                origin.substring(offset)
            ).append(
                origin.substring(0, offset)
            ).toString();
        }
        return origin;
    });
}
 
Example 4
Source File: SwappedCase.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param text The text
 */
@SuppressWarnings({"PMD.CallSuperInConstructor",
    "PMD.ConstructorOnlyInitializesOrCallOtherConstructors"})
public SwappedCase(final Text text) {
    super((Scalar<String>) () -> {
        final String origin = text.asString();
        final char[] chars = origin.toCharArray();
        for (int idx = 0; idx < chars.length; idx += 1) {
            final char chr = chars[idx];
            if (Character.isUpperCase(chr)) {
                chars[idx] = Character.toLowerCase(chr);
            } else if (Character.isLowerCase(chr)) {
                chars[idx] = Character.toUpperCase(chr);
            }
        }
        return new String(chars);
    });
}
 
Example 5
Source File: NoNulls.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param text The text
 */
public NoNulls(final Text text) {
    super(new Scalar<String>() {
        @Override
        public String value() throws Exception {
            if (text == null) {
                throw new IllegalArgumentException(
                    "NULL instead of a valid text"
                );
            }
            final String string = text.asString();
            if (string == null) {
                throw new IllegalStateException(
                    "NULL instead of a valid result string"
                );
            }
            return string;
        }
    });
}
 
Example 6
Source File: Sub.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param text The Text
 * @param start Start position in the text
 * @param end End position in the text
 */
@SuppressWarnings({"PMD.CallSuperInConstructor",
    "PMD.ConstructorOnlyInitializesOrCallOtherConstructors"})
public Sub(final Text text, final Unchecked<Integer> start,
    final Unchecked<Integer> end) {
    super((Scalar<String>) () -> {
        int begin = start.value();
        if (begin < 0) {
            begin = 0;
        }
        int finish = end.value();
        final String origin = text.asString();
        if (origin.length() < finish) {
            finish = origin.length();
        }
        return origin.substring(begin, finish);
    });
}
 
Example 7
Source File: Synced.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param text The text
 * @param lck The lock
 */
public Synced(final Text text, final Object lck) {
    super(new Scalar<String>() {
        @Override
        public String value() throws Exception {
            synchronized (lck) {
                return text.asString();
            }
        }
    });
}
 
Example 8
Source File: Joined.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param delimit Delimit among texts
 * @param txts Texts to be joined
 */
public Joined(final Text delimit, final Iterable<? extends Text> txts) {
    super((Scalar<String>) () -> {
        final StringJoiner joint =
            new StringJoiner(delimit.asString());
        for (final Text text : txts) {
            joint.add(text.asString());
        }
        return joint.toString();
    });
}
 
Example 9
Source File: PaddedStart.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param text The text
 * @param length The minimum length of the resulting string
 * @param symbol The padding symbol
 */
public PaddedStart(
    final Text text, final int length, final char symbol) {
    super((Scalar<String>) () -> {
        final String original = text.asString();
        return new Joined(
            new TextOf(""),
            new Repeated(
                new TextOf(symbol), length - original.length()
            ),
            text
        ).asString();
    });
}
 
Example 10
Source File: Strict.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param predicate The Func as a predicate
 * @param origin The Text
 */
public Strict(final Func<String, Boolean> predicate, final Text origin) {
    super((Scalar<String>) () -> {
        final String str = origin.asString();
        if (!predicate.apply(str)) {
            throw new IllegalArgumentException(
                new FormattedText(
                    "String '%s' does not match a given predicate",
                    str
                ).asString()
            );
        }
        return str;
    });
}
 
Example 11
Source File: ResourceOf.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * New resource input with current context {@link ClassLoader}.
 * @param res Resource name
 * @param fbk Fallback
 */
public ResourceOf(final Text res, final Text fbk) {
    this(res, input -> new InputOf(new BytesOf(fbk.asString())));
}