Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 23, 2018
2 parents 8684193 + 4cacb11 commit f82ec74
Show file tree
Hide file tree
Showing 17 changed files with 505 additions and 135 deletions.
18 changes: 12 additions & 6 deletions src/main/java/org/cactoos/collection/CollectionNoNulls.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.Collection;
import java.util.Iterator;
import org.cactoos.iterator.IteratorNoNulls;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* A decorator of {@link Collection} that tolerates no NULLs.
Expand Down Expand Up @@ -82,9 +84,11 @@ public Object[] toArray() {
for (int idx = 0; idx < array.length; ++idx) {
if (array[idx] == null) {
throw new IllegalStateException(
String.format(
"Item #%d of #toArray() is NULL", idx
)
new UncheckedText(
new FormattedText(
"Item #%d of #toArray() is NULL", idx
)
).asString()
);
}
}
Expand All @@ -98,9 +102,11 @@ public <T> T[] toArray(final T[] array) {
for (int idx = 0; idx < array.length; ++idx) {
if (array[idx] == null) {
throw new IllegalStateException(
String.format(
"Item #%d of #toArray(array) is NULL", idx
)
new UncheckedText(
new FormattedText(
"Item #%d of #toArray(array) is NULL", idx
)
).asString()
);
}
}
Expand Down
84 changes: 50 additions & 34 deletions src/main/java/org/cactoos/io/LoggingInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.logging.Logger;
import org.cactoos.scalar.StickyScalar;
import org.cactoos.scalar.UncheckedScalar;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Logged input stream.
Expand Down Expand Up @@ -139,19 +141,21 @@ public int read(final byte[] buf, final int offset, final int len)
this.bytes.getAndAdd(byts);
this.time.getAndAdd(millis);
}
final String msg = String.format(
"Read %d byte(s) from %s in %dms.",
this.bytes.get(),
this.source,
this.time.get()
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);
this.logger.log(this.level.value(), msg.asString());
}
} else {
if (this.level.value().equals(Level.INFO)) {
this.logger.info(msg);
this.logger.info(msg.asString());
}
}
return byts;
Expand All @@ -162,11 +166,13 @@ public long skip(final long num) throws IOException {
final long skipped = this.origin.skip(num);
this.logger.log(
this.level.value(),
String.format(
"Skipped %d byte(s) from %s.",
skipped,
this.source
)
new UncheckedText(
new FormattedText(
"Skipped %d byte(s) from %s.",
skipped,
this.source
)
).asString()
);
return skipped;
}
Expand All @@ -176,11 +182,13 @@ public int available() throws IOException {
final int avail = this.origin.available();
this.logger.log(
this.level.value(),
String.format(
"There is(are) %d byte(s) available from %s.",
avail,
this.source
)
new UncheckedText(
new FormattedText(
"There is(are) %d byte(s) available from %s.",
avail,
this.source
)
).asString()
);
return avail;
}
Expand All @@ -190,10 +198,12 @@ public void close() throws IOException {
this.origin.close();
this.logger.log(
this.level.value(),
String.format(
"Closed input stream from %s.",
this.source
)
new UncheckedText(
new FormattedText(
"Closed input stream from %s.",
this.source
)
).asString()
);
}

Expand All @@ -202,11 +212,13 @@ public void mark(final int limit) {
this.origin.mark(limit);
this.logger.log(
this.level.value(),
String.format(
"Marked position %d from %s.",
limit,
this.source
)
new UncheckedText(
new FormattedText(
"Marked position %d from %s.",
limit,
this.source
)
).asString()
);
}

Expand All @@ -215,10 +227,12 @@ public void reset() throws IOException {
this.origin.reset();
this.logger.log(
this.level.value(),
String.format(
"Reset input stream from %s.",
this.source
)
new UncheckedText(
new FormattedText(
"Reset input stream from %s.",
this.source
)
).asString()
);
}

Expand All @@ -233,10 +247,12 @@ public boolean markSupported() {
}
this.logger.log(
this.level.value(),
String.format(
msg,
this.source
)
new UncheckedText(
new FormattedText(
msg,
this.source
)
).asString()
);
return supported;
}
Expand Down
65 changes: 38 additions & 27 deletions src/main/java/org/cactoos/io/LoggingOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Logged output stream.
Expand Down Expand Up @@ -123,12 +125,14 @@ public void write(final byte[] buf, final int offset,
if (!level.equals(Level.INFO)) {
this.logger.log(
level,
String.format(
"Written %d byte(s) to %s in %dms.",
this.bytes.get(),
this.destination,
this.time.get()
)
new UncheckedText(
new FormattedText(
"Written %d byte(s) to %s in %dms.",
this.bytes.get(),
this.destination,
this.time.get()
)
).asString()
);
}
}
Expand All @@ -140,20 +144,24 @@ public void close() throws IOException {
if (level.equals(Level.INFO)) {
this.logger.log(
level,
String.format(
"Written %d byte(s) to %s in %dms.",
this.bytes.get(),
this.destination,
this.time.get()
)
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,
String.format(
"Closed output stream from %s.",
this.destination
)
new UncheckedText(
new FormattedText(
"Closed output stream from %s.",
this.destination
)
).asString()
);
}

Expand All @@ -164,21 +172,24 @@ public void flush() throws IOException {
if (level.equals(Level.INFO)) {
this.logger.log(
level,
String.format(
"Written %d byte(s) to %s in %dms.",
this.bytes.get(),
this.destination,
this.time.get()
)
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,
String.format(
"Flushed output stream from %s.",
this.destination
)
new UncheckedText(
new FormattedText(
"Flushed output stream from %s.",
this.destination
)
).asString()
);
}

}
13 changes: 8 additions & 5 deletions src/main/java/org/cactoos/iterator/IteratorNoNulls.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLong;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* A decorator of an {@link Iterator} that returns no NULL.
Expand Down Expand Up @@ -65,10 +67,12 @@ public X next() {
final X next = this.iterator.next();
if (next == null) {
throw new IllegalStateException(
String.format(
"Item #%d of %s is NULL",
this.pos.get(), this.iterator
)
new UncheckedText(
new FormattedText(
"Item #%d of %s is NULL",
this.pos.get(), this.iterator
)
).asString()
);
}
this.pos.incrementAndGet();
Expand All @@ -79,5 +83,4 @@ public X next() {
public void remove() {
this.iterator.remove();
}

}
Loading

0 comments on commit f82ec74

Please sign in to comment.