Search in sources :

Example 1 with FormattedText

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

the class MappedWithIndexTest method failsIfIteratorExhausted.

@Test
void failsIfIteratorExhausted() {
    final Iterator<Text> iterator = new MappedWithIndex<>((item, index) -> new FormattedText("%1$s X %2$s", index, item), new IterableOf<>("1").iterator());
    iterator.next();
    new Assertion<>("must throw NSEE", iterator::next, new Throws<>(NoSuchElementException.class)).affirm();
}
Also used : IterableOf(org.cactoos.iterable.IterableOf) Text(org.cactoos.Text) FormattedText(org.cactoos.text.FormattedText) FormattedText(org.cactoos.text.FormattedText) Throws(org.llorllale.cactoos.matchers.Throws) Test(org.junit.jupiter.api.Test)

Example 2 with FormattedText

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

the class MappedWithIndexTest method removingElementsFromIterator.

@Test
void removingElementsFromIterator() {
    final Iterator<Text> iterator = new MappedWithIndex<>((item, index) -> new FormattedText("%1$s : %2$s", index, item), new ArrayList<>(Arrays.asList("1", "2", "3")).iterator());
    iterator.next();
    iterator.remove();
    new Assertion<>("must map values of changed iterator", new IterableOf<>(iterator), new HasValues<>(new TextOf("1 : 2"), new TextOf("2 : 3"))).affirm();
}
Also used : HasValues(org.llorllale.cactoos.matchers.HasValues) IterableOf(org.cactoos.iterable.IterableOf) ArrayList(java.util.ArrayList) Text(org.cactoos.Text) FormattedText(org.cactoos.text.FormattedText) TextOf(org.cactoos.text.TextOf) FormattedText(org.cactoos.text.FormattedText) Test(org.junit.jupiter.api.Test)

Example 3 with FormattedText

use of org.cactoos.text.FormattedText 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 4 with FormattedText

use of org.cactoos.text.FormattedText 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 5 with FormattedText

use of org.cactoos.text.FormattedText 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)

Aggregations

FormattedText (org.cactoos.text.FormattedText)15 UncheckedText (org.cactoos.text.UncheckedText)12 Level (java.util.logging.Level)3 Instant (java.time.Instant)2 Text (org.cactoos.Text)2 IterableOf (org.cactoos.iterable.IterableOf)2 Test (org.junit.jupiter.api.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 TextOf (org.cactoos.text.TextOf)1 HasValues (org.llorllale.cactoos.matchers.HasValues)1 Throws (org.llorllale.cactoos.matchers.Throws)1