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();
}
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();
}
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());
}
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());
}
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;
}
Aggregations