Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into padded-text-start
Browse files Browse the repository at this point in the history
* upstream/master:
  yegor256#123: fixed
  yegor256#123: bug
  yegor256#121: fixed
  yegor256#121: bug
  doc
  doc
  blog
  names
  yegor256#116 package removed
  yegor256#119: StickyScalar fixed
  yegor256#116: IterableAsMap
  Switched to non Cached size
  Refactored IterableAsList to remove unnecessary call to size() and remove not working cache
  • Loading branch information
vivekimsit committed Jun 14, 2017
2 parents 42b4d93 + 2e988e9 commit 62b87c6
Show file tree
Hide file tree
Showing 11 changed files with 478 additions and 69 deletions.
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Java version required: 1.8+.

## Input/Output

To read a file:
To read a text file in UTF-8:

```java
String text = new BytesAsText(
Expand Down Expand Up @@ -86,7 +86,7 @@ To read a binary file from classpath:

```java
byte[] data = new InputAsBytes(
new ResourceAsInput("/foo/img.jpg")
new ResourceAsInput("foo/img.jpg")
).asBytes();
```

Expand Down Expand Up @@ -145,12 +145,11 @@ To iterate a collection:
new AllOf(
new TransformedIterable<>(
new ArrayAsIterable<>("how", "are", "you"),
new Func.Quiet<String>() {
@Override
public void exec(final String input) throws Exception {
new ProcAsFunc<>(
input -> {
System.out.printf("Item: %s\n", input);
}
}
)
)
).asValue();
```
Expand All @@ -160,7 +159,9 @@ Or even more compact:
```java
new IterableAsBoolean(
new ArrayAsIterable<>("how", "are", "you"),
(Func.Quiet<String>) i -> System.out.printf("Item: %s\n", i)
new ProcAsFunc<>(
input -> System.out.printf("Item: %s\n", i)
)
).asValue();
```

Expand Down Expand Up @@ -246,13 +247,15 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static

## Contributors

- [Yegor Bugayenko](https://github.com/yegor256)
- [Kirill Che.](https://github.com/g4s8) [email protected]
- [Fabrício Cabral](https://github.com/fabriciofx)
- [Andriy Kryvtsun](https://github.com/englishman)
- [Vseslav Sekorin](https://github.com/VsSekorin)
- [Andrey Valyaev](https://github.com/DronMDF)
- [Dušan Rychnovský](https://github.com/dusan-rychnovsky) [Blog](http://blog.dusanrychnovsky.cz/)
- [@yegor256](https://github.com/yegor256) as Yegor Bugayenko ([Blog](http://www.yegor256.com))
- [@g4s8](https://github.com/g4s8) as Kirill Che. ([email protected])
- [@fabriciofx](https://github.com/fabriciofx) as Fabrício Cabral
- [@englishman](https://github.com/englishman) as Andriy Kryvtsun
- [@VsSekorin](https://github.com/VsSekorin) as Vseslav Sekorin
- [@DronMDF](https://github.com/DronMDF) as Andrey Valyaev
- [@dusan-rychnovsky](https://github.com/dusan-rychnovsky) as Dušan Rychnovský ([Blog](http://blog.dusanrychnovsky.cz/))
- [@timmeey](https://github.com/timmeey) as Tim Hinkes ([Blog](https://blog.timmeey.de))


## License (MIT)

Expand Down
23 changes: 8 additions & 15 deletions src/main/java/org/cactoos/func/StickyScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
*/
package org.cactoos.func;

import java.util.ArrayList;
import java.util.List;
import org.cactoos.Func;
import org.cactoos.Scalar;

/**
Expand All @@ -33,36 +32,30 @@
* <p>There is no thread-safety guarantee.
*
* @author Tim Hinkes ([email protected])
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @param <T> Type of result
* @since 0.3
*/
public final class StickyScalar<T> implements Scalar<T> {

/**
* The scalar to cache.
* Func.
*/
private final Scalar<T> source;

/**
* The list used as a optional value.
*/
private final List<T> cache;
private final Func<Boolean, T> func;

/**
* Ctor.
* @param src The Scalar to cache
*/
public StickyScalar(final Scalar<T> src) {
this.source = src;
this.cache = new ArrayList<>(1);
this.func = new StickyFunc<>(
input -> src.asValue()
);
}

@Override
public T asValue() throws Exception {
if (this.cache.isEmpty()) {
this.cache.add(this.source.asValue());
}
return this.cache.get(0);
return this.func.apply(true);
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/cactoos/io/InputAsBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public InputAsBytes(final Input input, final int max) {

@Override
public byte[] asBytes() throws IOException {
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final InputStream stream = new TeeInput(
this.source,
new OutputStreamAsOutput(baos)
).stream();
).stream()) {
final byte[] buf = new byte[this.size];
while (true) {
if (stream.read(buf) != buf.length) {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/cactoos/io/TeeInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@
* @since 0.1
*/
public final class TeeInputStream extends InputStream {

/**
* Input.
*/
private final InputStream input;

/**
* Output.
*/
private final OutputStream output;

/**
* Ctor.
* @param src Source of data
Expand All @@ -59,7 +62,9 @@ public TeeInputStream(final InputStream src, final OutputStream tgt) {
@Override
public int read() throws IOException {
final int data = this.input.read();
this.output.write(data);
if (data >= 0) {
this.output.write(data);
}
return data;
}

Expand Down
54 changes: 17 additions & 37 deletions src/main/java/org/cactoos/list/IterableAsList.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
package org.cactoos.list;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import org.cactoos.func.StickyScalar;
import org.cactoos.func.UncheckedScalar;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;
Expand All @@ -48,11 +46,6 @@ public final class IterableAsList<T> extends AbstractList<T> {
*/
private final Iterable<T> source;

/**
* Cache for source.
*/
private final List<T> cache;

/**
* Iterable length.
*/
Expand All @@ -66,48 +59,35 @@ public final class IterableAsList<T> extends AbstractList<T> {
public IterableAsList(final Iterable<T> iterable) {
super();
this.source = iterable;
this.cache = new ArrayList<>(0);
this.length = new UncheckedScalar<>(
new StickyScalar<>(
new LengthOfIterable(iterable)
)
new LengthOfIterable(iterable)
);
}

@Override
public T get(final int index) {
if (index < 0 || index >= this.size()) {
throw new IndexOutOfBoundsException(
new UncheckedText(
new FormattedText(
"index=%d, bounds=[%d; %d]",
index,
0,
this.size()
)
).asString()
);
int position = 0;
for (final T elem : this.source) {
if (position == index) {
return elem;
}
position += 1;
}
return this.cachedItem(index);
throw new IndexOutOfBoundsException(
new UncheckedText(
new FormattedText(
"index=%d, bounds=[%d; %d]",
index,
0,
this.size()
)
).asString()
);
}

@Override
public int size() {
return this.length.asValue();
}

/**
* Find item in cache by index.
*
* @param index Item index
* @return Cached item
*/
private T cachedItem(final int index) {
if (this.cache.size() != this.size()) {
for (final T item : this.source) {
this.cache.add(item);
}
}
return this.cache.get(index);
}
}
Loading

0 comments on commit 62b87c6

Please sign in to comment.