Skip to content

Commit

Permalink
CountOf - Text, Iterable, Iterator and Input
Browse files Browse the repository at this point in the history
  • Loading branch information
ixmanuel committed Jul 28, 2017
1 parent b27b0bc commit 52d7656
Show file tree
Hide file tree
Showing 22 changed files with 340 additions and 180 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ String text = new TextOf(
To write a text into a file:

```java
new LengthOfInput(
new CountOf(
new TeeInput(
new InputOf(
"Hello, world!"
Expand Down Expand Up @@ -182,7 +182,7 @@ List<String> sorted = new ListOf<>(
To count elements in an iterable:

```java
int total = new LengthOfIterable(
int total = new CountOf(
"how", "are", "you"
).value();
```
Expand Down Expand Up @@ -235,7 +235,7 @@ Cactoos | Guava | Apache Commons | JDK 8
`FormattedText` | - | - | `String.format()`
`IsBlank` | - | `StringUtils.isBlank()`| -
`JoinedText` | - | - | `String.join()`
`LengthOfText` | - | - | `String#length()`
`CountOf` | - | - | `String#length()`
`LowerText` | - | - | `String#toLowerCase()`
`NormalizedText` | - | `StringUtils.normalize()` | -
`RepeatedText` | - | `StringUtils.repeat()` | -
Expand Down
117 changes: 117 additions & 0 deletions src/main/java/org/cactoos/CountOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos;

import java.util.Iterator;
import org.cactoos.func.UncheckedScalar;
import org.cactoos.io.CountOfInput;
import org.cactoos.list.CountOfIterable;
import org.cactoos.list.CountOfIterator;
import org.cactoos.text.TextOf;

/**
* Length of iterator.
*
* <p>There is no thread-safety guarantee.
*
* @author Ix ([email protected])
* @version $Id$
* @since 0.12
*/
public final class CountOf implements Scalar<Number> {

/**
* The underlying scalar number.
*/
private final UncheckedScalar<?> scalar;

/**
* Ctor.
* @param string The string
*/
public CountOf(final String string) {
this(new TextOf(string));
}

/**
* Ctor.
* @param text The text
*/
public CountOf(final Text text) {
this(() -> text.asString().length());
}

/**
* Ctor.
* @param input The input
*/
public CountOf(final Input input) {
this(new CountOfInput(input));
}

/**
* Ctor.
* @param input The input
* @param max Buffer size
*/
public CountOf(final Input input, final int max) {
this(new CountOfInput(input, max));
}

/**
* Ctor.
* @param iterable The array of items
*/
public CountOf(final Iterable<?> iterable) {
this(new CountOfIterable(iterable));
}

/**
* Ctor.
* @param iterator The iterator
*/
public CountOf(final Iterator<?> iterator) {
this(new CountOfIterator(iterator));
}

/**
* Ctor.
* @param sclr The underlying scalar length
*/
public CountOf(final Scalar<?> sclr) {
this.scalar = new UncheckedScalar<>(sclr);
}

@Override
public String toString() {
return String.valueOf(this.value());
}

@Override
public Number value() {
return (Number) this.scalar.value();
}

}

4 changes: 2 additions & 2 deletions src/main/java/org/cactoos/Output.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* together with {@link Input} in order to modify the content
* of a text file:</p>
*
* <pre> new LengthOfInput(
* <pre> new CountOf(
* new TeeInput(
* new InputOf(new TextOf("Hello, world!")),
* new FileAsOutput(new File("/tmp/names.txt"))
Expand All @@ -45,7 +45,7 @@
* <p>Here {@link FileAsOutput} implements {@link Output} and behaves like
* one, providing write-only access to the encapsulated
* {@link java.io.File}. The {@link TeeInput} copies the content of the
* input to the output. The {@link org.cactoos.io.LengthOfInput}
* input to the output. The {@link org.cactoos.io.CountOf}
* calculates the size of the copied data.</p>
*
* <p>There is no thread-safety guarantee.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @version $Id$
* @since 0.1
*/
public final class LengthOfInput implements Scalar<Long> {
public final class CountOfInput implements Scalar<Long> {

/**
* The input.
Expand All @@ -54,7 +54,7 @@ public final class LengthOfInput implements Scalar<Long> {
* Ctor.
* @param input The input
*/
public LengthOfInput(final Input input) {
public CountOfInput(final Input input) {
// @checkstyle MagicNumber (1 line)
this(input, 16 << 10);
}
Expand All @@ -64,7 +64,7 @@ public LengthOfInput(final Input input) {
* @param input The input
* @param max Buffer size
*/
public LengthOfInput(final Input input, final int max) {
public CountOfInput(final Input input, final int max) {
this.source = input;
this.size = max;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/cactoos/io/StickyInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.cactoos.CountOf;
import org.cactoos.Input;
import org.cactoos.Scalar;
import org.cactoos.func.IoCheckedScalar;
Expand Down Expand Up @@ -56,7 +57,7 @@ public StickyInput(final Input input) {
this.cache = new StickyScalar<>(
() -> {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
new LengthOfInput(
new CountOf(
new TeeInput(input, new OutputStreamAsOutput(baos))
).value();
return baos.toByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @version $Id$
* @since 0.1
*/
public final class LengthOfIterable implements Scalar<Integer> {
public final class CountOfIterable implements Scalar<Integer> {

/**
* The iterable.
Expand All @@ -45,13 +45,13 @@ public final class LengthOfIterable implements Scalar<Integer> {
* Ctor.
* @param items The array
*/
public LengthOfIterable(final Iterable<?> items) {
public CountOfIterable(final Iterable<?> items) {
this.iterable = items;
}

@Override
public Integer value() {
return new LengthOfIterator(this.iterable.iterator()).value();
return new CountOfIterator(this.iterable.iterator()).value();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @version $Id$
* @since 0.1
*/
public final class LengthOfIterator implements Scalar<Integer> {
public final class CountOfIterator implements Scalar<Integer> {

/**
* The iterator.
Expand All @@ -46,7 +46,7 @@ public final class LengthOfIterator implements Scalar<Integer> {
* Ctor.
* @param items The iterator
*/
public LengthOfIterator(final Iterator<?> items) {
public CountOfIterator(final Iterator<?> items) {
this.iterator = items;
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/cactoos/list/ListOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import org.cactoos.CountOf;

/**
* Iterable as {@link List}.
Expand Down Expand Up @@ -79,7 +80,7 @@ public ListOf(final Iterable<T> src) {

@Override
public int size() {
return new LengthOfIterable(this.iterable).value();
return (int) new CountOf(this.iterable).value();
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/cactoos/list/MapOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.cactoos.CountOf;
import org.cactoos.Func;

/**
Expand Down Expand Up @@ -160,7 +161,7 @@ public MapOf(final Iterable<Map.Entry<X, Y>> list) {

@Override
public int size() {
return new LengthOfIterable(this.entries).value();
return (int) new CountOf(this.entries).value();
}

@Override
Expand Down
66 changes: 0 additions & 66 deletions src/main/java/org/cactoos/text/LengthOfText.java

This file was deleted.

Loading

0 comments on commit 52d7656

Please sign in to comment.