Search in sources :

Example 1 with UncheckedText

use of org.cactoos.text.UncheckedText in project cactoos by yegor256.

the class LoggingInputStream method reset.

@Override
public void reset() throws IOException {
    this.origin.reset();
    this.logger.log(this.level.value(), new UncheckedText(new FormattedText("Reset input stream from %s.", this.source)).asString());
}
Also used : UncheckedText(org.cactoos.text.UncheckedText) FormattedText(org.cactoos.text.FormattedText)

Example 2 with UncheckedText

use of org.cactoos.text.UncheckedText in project cactoos by yegor256.

the class LoggingOutputStream method flush.

@Override
public void flush() throws IOException {
    this.origin.flush();
    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());
    }
    this.logger.log(level, new UncheckedText(new FormattedText("Flushed output stream from %s.", this.destination)).asString());
}
Also used : UncheckedText(org.cactoos.text.UncheckedText) Level(java.util.logging.Level) FormattedText(org.cactoos.text.FormattedText)

Example 3 with UncheckedText

use of org.cactoos.text.UncheckedText in project cactoos by yegor256.

the class Checked method wrappedException.

/**
 * 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;
}
Also used : UncheckedText(org.cactoos.text.UncheckedText) FormattedText(org.cactoos.text.FormattedText)

Example 4 with UncheckedText

use of org.cactoos.text.UncheckedText in project cactoos by yegor256.

the class NoNulls method next.

@Override
public X next() {
    final X next = this.iterator.next();
    if (next == null) {
        throw new IllegalStateException(new UncheckedText(new FormattedText("Item #%d of %s is NULL", this.pos.get(), this.iterator)).asString());
    }
    this.pos.incrementAndGet();
    return next;
}
Also used : UncheckedText(org.cactoos.text.UncheckedText) FormattedText(org.cactoos.text.FormattedText)

Example 5 with UncheckedText

use of org.cactoos.text.UncheckedText in project cactoos by yegor256.

the class LoggingInputStream method read.

@Override
public int read(final byte[] buf, final int offset, final int len) throws IOException {
    final Instant start = Instant.now();
    final int byts = this.origin.read(buf, offset, len);
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    if (byts > 0) {
        this.bytes.getAndAdd(byts);
        this.time.getAndAdd(millis);
    }
    final UncheckedText msg = new UncheckedText(new FormattedText("Read %d byte(s) from %s in %dms.", this.bytes.get(), this.source, this.time.get()));
    if (byts > 0) {
        if (!this.level.value().equals(Level.INFO)) {
            this.logger.log(this.level.value(), msg.asString());
        }
    } else {
        if (this.level.value().equals(Level.INFO)) {
            this.logger.info(msg.asString());
        }
    }
    return byts;
}
Also used : UncheckedText(org.cactoos.text.UncheckedText) Instant(java.time.Instant) FormattedText(org.cactoos.text.FormattedText)

Aggregations

UncheckedText (org.cactoos.text.UncheckedText)13 FormattedText (org.cactoos.text.FormattedText)12 Level (java.util.logging.Level)3 Instant (java.time.Instant)2 TextOf (org.cactoos.text.TextOf)1 IsIterableContainingInOrder (org.hamcrest.collection.IsIterableContainingInOrder)1 Test (org.junit.jupiter.api.Test)1 IsText (org.llorllale.cactoos.matchers.IsText)1