org.cactoos.text.UncheckedText Java Examples

The following examples show how to use org.cactoos.text.UncheckedText. 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: NoNulls.java    From cactoos with MIT License 6 votes vote down vote up
@Override
public final V get(final Object key) {
    if (key == null) {
        throw new IllegalStateException(
            "Key at #get(K) is NULL"
        );
    }
    final V value = this.map.get(key);
    if (value == null) {
        throw new IllegalStateException(
            new UncheckedText(
                new FormattedText(
                    "Value returned by #get(%s) is NULL",
                    key
                )
            ).asString()
        );
    }
    return value;
}
 
Example #2
Source File: FormParams.java    From verano-http with MIT License 6 votes vote down vote up
@Override
public final String asString() {
    return new UncheckedText(
        new JoinedText(
            "&",
            new Mapped<>(
                input -> String.format(
                    "%s=%s", input.key(),
                    URLEncoder.encode(
                        input.value(),
                        "UTF-8"
                    )
                ),
                new Mapped<>(
                    in -> new KvpOf(in.key().substring(2), in.value()),
                    new Filtered<>(
                        input -> input.key().startsWith("f."),
                        this.dict
                    )
                )
            )
        )
    ).asString();
}
 
Example #3
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public int executeUpdate() throws SQLException {
    final Instant start = Instant.now();
    final int updated = this.origin.executeUpdate();
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] updated a source and",
                    "returned '%d' in %dms."
                ),
                this.source,
                this.id,
                updated,
                millis
            )
        ).asString()
    );
    return updated;
}
 
Example #4
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void setShort(
    final int index,
    final short value
) throws SQLException {
    this.origin.setShort(index, value);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] changed at",
                    "parameter[#%d] with '%d' value."
                ),
                this.source,
                this.id,
                index,
                value
            )
        ).asString()
    );
}
 
Example #5
Source File: SortedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void mustSortTextIterableAsSetUsingCustomCOmparator() {
    new Assertion<>(
        "Must keep unique integer numbers sorted in descending order",
        new Sorted<>(
            (first, second) -> {
                final String left = new UncheckedText(first).asString();
                final String right = new UncheckedText(second).asString();
                return left.compareTo(right);
            },
            new TextOf("cd"),
            new TextOf("ab"),
            new TextOf("gh"),
            new TextOf("ef")
        ),
        new IsIterableContainingInOrder<>(
            new ListOf<Matcher<? super Text>>(
                new IsEqual<>(new TextOf("ab")),
                new IsEqual<>(new TextOf("cd")),
                new IsEqual<>(new TextOf("ef")),
                new IsEqual<>(new TextOf("gh"))
            )
        )
    ).affirm();
}
 
Example #6
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void setLong(final int index, final long value) throws SQLException {
    this.origin.setLong(index, value);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] changed at",
                    "parameter[#%d] with '%d' value."
                ),
                this.source,
                this.id,
                index,
                value
            )
        ).asString()
    );
}
 
Example #7
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void setFloat(
    final int index,
    final float value
) throws SQLException {
    this.origin.setFloat(index, value);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] changed at",
                    "parameter[#%d] with '%f' value."
                ),
                this.source,
                this.id,
                index,
                value
            )
        ).asString()
    );
}
 
Example #8
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void setDouble(
    final int index,
    final double value
) throws SQLException {
    this.origin.setDouble(index, value);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] changed at",
                    "parameter[#%d] with '%f' value."
                ),
                this.source,
                this.id,
                index,
                value
            )
        ).asString()
    );
}
 
Example #9
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void setByte(final int index, final byte value) throws SQLException {
    this.origin.setByte(index, value);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] changed at",
                    "parameter[#%d] with '%d' value."
                ),
                this.source,
                this.id,
                index,
                value
            )
        ).asString()
    );
}
 
Example #10
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void setObject(
    final int index,
    final Object value,
    final int type
) throws SQLException {
    this.origin.setObject(index, value, type);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] changed at",
                    "parameter[#%d] with '%s' data and '%d' type."
                ),
                this.source,
                this.id,
                index,
                value.toString(),
                type
            )
        ).asString()
    );
}
 
Example #11
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void setObject(
    final int index,
    final Object value
) throws SQLException {
    this.origin.setObject(index, value);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] changed at",
                    "parameter[#%d] with '%s' value."
                ),
                this.source,
                this.id,
                index,
                value.toString()
            )
        ).asString()
    );
}
 
Example #12
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public Statement createStatement(
    final int resultSetType,
    final int resultSetConcurrency
) throws SQLException {
    final Statement stmt = this.origin.createStatement(
        resultSetType,
        resultSetConcurrency
    );
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                // @checkstyle LineLengthCheck (1 line)
                "[%s] Statement[#%d] created with type '%d' and concurrency '%d'.",
                this.source,
                this.statements.get(),
                resultSetType,
                resultSetConcurrency
            )
        ).asString()
    );
    return stmt;
}
 
Example #13
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public int executeUpdate(final String sql) throws SQLException {
    final Instant start = Instant.now();
    final int updated = this.origin.executeUpdate(sql);
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                "[%s] PreparedStatement[#%d] executed SQL %s in %dms.",
                this.source,
                this.id,
                sql,
                updated,
                millis
            )
        ).asString()
    );
    return updated;
}
 
Example #14
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void setMaxFieldSize(final int max) throws SQLException {
    this.origin.setMaxFieldSize(max);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] changed",
                    "max field size to '%d' bytes."
                ),
                this.source,
                this.id,
                max
            )
        ).asString()
    );
}
 
Example #15
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void setQueryTimeout(final int seconds) throws SQLException {
    this.origin.setQueryTimeout(seconds);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] changed",
                    "timeout to '%d' seconds."
                ),
                this.source,
                this.id,
                seconds
            )
        ).asString()
    );
}
 
Example #16
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public boolean execute() throws SQLException {
    final Instant start = Instant.now();
    final boolean result = this.origin.execute();
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                "[%s] PreparedStatement[#%d] returned '%s' in %dms.",
                this.source,
                this.id,
                result,
                millis
            )
        ).asString()
    );
    return result;
}
 
Example #17
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public boolean execute(final String sql) throws SQLException {
    final Instant start = Instant.now();
    final boolean result = this.origin.execute(sql);
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                "[%s] PreparedStatement[#%d] executed SQL '%s' in %dms.",
                this.source,
                this.id,
                sql,
                millis
            )
        ).asString()
    );
    return result;
}
 
Example #18
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public ResultSet getResultSet() throws SQLException {
    final Instant start = Instant.now();
    final ResultSet rset = this.origin.getResultSet();
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                "[%s] PreparedStatement[#%d] returned a ResultSet in %dms.",
                this.source,
                this.id,
                millis
            )
        ).asString()
    );
    return rset;
}
 
Example #19
Source File: Checked.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Wraps exception.
 * Skips unnecessary wrapping of exceptions of the same type.
 * Allows wrapping of exceptions of the same type if the error message
 * has been changed.
 *
 * @param exp Exception
 * @return E Wrapped exception
 */
@SuppressWarnings("unchecked")
private E wrappedException(final Exception exp) {
    E wrapped = new UncheckedFunc<>(this.func).apply(exp);
    final int level = new InheritanceLevel(
        exp.getClass(), wrapped.getClass()
    ).value();
    final String message = wrapped.getMessage()
        .replaceFirst(
            new UncheckedText(
                new FormattedText(
                    "%s: ",
                    exp.getClass().getName()
                )
            ).asString(),
            ""
        );
    if (level >= 0 && message.equals(exp.getMessage())) {
        wrapped = (E) exp;
    }
    return wrapped;
}
 
Example #20
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public int[] executeBatch() throws SQLException {
    final int[] counts = this.origin.executeBatch();
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                "[%s] PreparedStatement[#%d] returned '%d' counts.",
                this.source,
                this.id,
                counts.length
            )
        ).asString()
    );
    return counts;
}
 
Example #21
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public ResultSet getGeneratedKeys() throws SQLException {
    final Instant start = Instant.now();
    final ResultSet rset = this.origin.getGeneratedKeys();
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] returned",
                    "a ResultSet keys in %dms."
                ),
                this.source,
                this.id,
                millis
            )
        ).asString()
    );
    return rset;
}
 
Example #22
Source File: LoggingOutputStream.java    From cactoos with MIT License 6 votes vote down vote up
@Override
public void write(final byte[] buf, final int offset,
    final int len) throws IOException {
    final Instant start = Instant.now();
    this.origin.write(buf, offset, len);
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    this.bytes.getAndAdd(len);
    this.time.getAndAdd(millis);
    final Level level = this.logger.getLevel();
    if (!level.equals(Level.INFO)) {
        this.logger.log(
            level,
            new UncheckedText(
                new FormattedText(
                    "Written %d byte(s) to %s in %dms.",
                    this.bytes.get(),
                    this.destination,
                    this.time.get()
                )
            ).asString()
        );
    }
}
 
Example #23
Source File: NoNulls.java    From cactoos with MIT License 6 votes vote down vote up
@Override
public Object[] toArray() {
    final Object[] array = this.col.toArray();
    for (int idx = 0; idx < array.length; ++idx) {
        if (array[idx] == null) {
            throw new IllegalStateException(
                new UncheckedText(
                    new FormattedText(
                        "Item #%d of #toArray() is NULL", idx
                    )
                ).asString()
            );
        }
    }
    return array;
}
 
Example #24
Source File: NoNulls.java    From cactoos with MIT License 6 votes vote down vote up
@Override
public final V remove(final Object key) {
    if (key == null) {
        throw new IllegalStateException(
            "Key at #remove(K) is NULL"
        );
    }
    final V result = this.map.remove(key);
    if (result == null) {
        throw new IllegalStateException(
            new UncheckedText(
                new FormattedText(
                    "Value returned by #remove(%s) is NULL",
                    key
                )
            ).asString()
        );
    }
    return result;
}
 
Example #25
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public PreparedStatement prepareStatement(final String sql) throws
    SQLException {
    final PreparedStatement stmt = this.origin.prepareStatement(sql);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                "[%s] PreparedStatement[#%d] created using SQL '%s'.",
                this.source,
                this.statements.get(),
                sql
            )
        ).asString()
    );
    return new com.github.fabriciofx.cactoos.jdbc.prepared.Logged(
        stmt,
        this.source,
        this.logger,
        this.level,
        this.statements.getAndIncrement()
    );
}
 
Example #26
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public CallableStatement prepareCall(final String sql) throws SQLException {
    final CallableStatement stmt = this.origin.prepareCall(sql);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                "[%s] CallableStatement[#%d] created using SQL '%s'.",
                this.source,
                this.statements.get(),
                sql
            )
        ).asString()
    );
    return stmt;
}
 
Example #27
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public String nativeSQL(final String sql) throws SQLException {
    final String nat = this.origin.nativeSQL(sql);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                "[%s] SQL '%s' converted to '%s.",
                this.source,
                sql,
                nat
            )
        ).asString()
    );
    return nat;
}
 
Example #28
Source File: NoNulls.java    From cactoos with MIT License 6 votes vote down vote up
@Override
public T get(final int index) {
    final T item = this.list.get(index);
    if (item == null) {
        throw new IllegalStateException(
            new UncheckedText(
                new FormattedText(
                    "Item #%d of %s is NULL",
                    index,
                    this.list
                )
            ).asString()
        );
    }
    return item;
}
 
Example #29
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void commit() throws SQLException {
    final Instant start = Instant.now();
    this.origin.commit();
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                "[%s] executed in %dms.",
                this.source,
                millis
            )
        ).asString()
    );
}
 
Example #30
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void rollback() throws SQLException {
    final Instant start = Instant.now();
    this.origin.rollback();
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                "[%s] executed in %dms.",
                this.source,
                millis
            )
        ).asString()
    );
}