Skip to content

Commit

Permalink
Merge pull request #1 from yegor256/master
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
ixmanuel committed Jun 24, 2017
2 parents 249bb28 + 540a3c7 commit 7e961e5
Show file tree
Hide file tree
Showing 164 changed files with 9,572 additions and 528 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ cache:
- $HOME/.m2
script:
- set -e
- mvn clean install -Pqulice --errors --batch-mode
- mvn clean site -Psite --errors --batch-mode
- mvn clean install -Pqulice --errors --batch-mode
env:
global:
- MAVEN_OPTS="-Xmx256m"
Expand Down
96 changes: 75 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![DevOps By Rultor.com](http://www.rultor.com/b/yegor256/cactoos)](http://www.rultor.com/p/yegor256/cactoos)

[![Build Status](https://travis-ci.org/yegor256/cactoos.svg?branch=master)](https://travis-ci.org/yegor256/cactoos)
[![Build status](https://ci.appveyor.com/api/projects/status/8vs8huy61og6jwif?svg=true)](https://ci.appveyor.com/project/yegor256/cactoos)
[![Javadoc](https://javadoc-emblem.rhcloud.com/doc/org.cactoos/cactoos/badge.svg?color=blue&prefix=v)](http://www.javadoc.io/doc/org.cactoos/cactoos)
[![PDD status](http://www.0pdd.com/svg?name=yegor256/cactoos)](http://www.0pdd.com/p?name=yegor256/cactoos)
[![Test Coverage](https://img.shields.io/codecov/c/github/yegor256/cactoos.svg)](https://codecov.io/github/yegor256/cactoos?branch=master)
Expand Down Expand Up @@ -52,7 +53,10 @@ Java version required: 1.8+.

## Input/Output

To read a file:
More about it here:
[Object-Oriented Declarative Input/Output in Cactoos](http://www.yegor256.com/2017/06/22/object-oriented-input-output-in-cactoos.html).

To read a text file in UTF-8:

```java
String text = new BytesAsText(
Expand All @@ -69,8 +73,10 @@ To write a text into a file:
```java
new LengthOfInput(
new TeeInput(
new TextAsInput(
new StringAsText("Hello, world!")
new BytesAsInput(
new TextAsBytes(
new StringAsText("Hello, world!")
)
),
new FileAsOutput(
new File("/code/a.txt")
Expand All @@ -83,9 +89,7 @@ To read a binary file from classpath:

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

Expand Down Expand Up @@ -141,25 +145,24 @@ new IterableAsCollection<>(
To iterate a collection:

```java
new AllOf(
new TransformedIterable<>(
new And(
new MappedIterable<>(
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();
```

Or even more compact:

```java
new IterableAsBoolean(
new ArrayAsIterable<>("how", "are", "you"),
(Func.Quiet<String>) i -> System.out.printf("Item: %s\n", i)
new And(
input -> System.out.printf("Item: %s\n", i),
"how", "are", "you"
).asValue();
```

Expand All @@ -168,10 +171,12 @@ To sort a list of words in the file:
```java
List<String> sorted = new SortedList<>(
new IterableAsList<>(
new TextAsLines(
new InputAsText(
new FileAsInput(
new File("/tmp/names.txt")
new SplitText(
new BytesAsText(
new InputAsBytes(
new FileAsInput(
new File("/tmp/names.txt")
)
)
)
)
Expand All @@ -183,7 +188,48 @@ To count elements in an iterable:

```java
int total = new LengthOfIterable(
new ArrayAsIterable<>("how", "are", "you")
"how", "are", "you"
).asValue();
```

## Funcs and Procs

This is a traditional `foreach` loop:

```java
for (String name : names) {
System.out.printf("Hello, %s!\n", name);
}
```

This is its object-oriented alternative (no streams!):

```java
new And<>(
names,
n -> {
System.out.printf("Hello, %s!\n", n);
}
).asValue();
```

This is an endless `while/do` loop:

```java
while (!ready) {
System.out.prinln("Still waiting...");
}
```

Here is its object-oriented alternative:

```java
new And<>(
new EndlessIterable<>(ready),
r -> {
System.out.prinln("Still waiting...");
return !ready;
}
).asValue();
```

Expand All @@ -202,7 +248,15 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static

## Contributors

- [Kirill Che.](https://github.com/g4s8) [email protected]
- [@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
32 changes: 32 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '{build}'
skip_tags: true
clone_depth: 10
environment:
matrix:
- JAVA_HOME: C:\Program Files\Java\jdk1.8.0
branches:
only:
- master
except:
- gh-pages
os: Windows Server 2012
install:
- ps: |
Add-Type -AssemblyName System.IO.Compression.FileSystem
if (!(Test-Path -Path "C:\maven" )) {
(new-object System.Net.WebClient).DownloadFile('http://www.us.apache.org/dist/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.zip', 'C:\maven-bin.zip')
[System.IO.Compression.ZipFile]::ExtractToDirectory("C:\maven-bin.zip", "C:\maven")
}
- cmd: SET PATH=C:\maven\apache-maven-3.2.5\bin;%JAVA_HOME%\bin;%PATH:C:\Ruby193\bin;=%
- cmd: SET M2_HOME=C:\maven\apache-maven-3.2.5
- cmd: SET MAVEN_OPTS=-XX:MaxPermSize=2g -Xmx4g
- cmd: SET JAVA_OPTS=-XX:MaxPermSize=2g -Xmx4g
- cmd: mvn --version
- cmd: java -version
build_script:
- mvn clean package -B -Dmaven.test.skip=true
test_script:
- mvn clean install --batch-mode -Pqulice
cache:
- C:\maven\
- C:\Users\appveyor\.m2
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>com.jcabi</groupId>
<artifactId>parent</artifactId>
<version>0.48.1</version>
<version>0.48.5</version>
</parent>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/cactoos/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @see org.cactoos.text.TextAsBytes
* @since 0.1
*/
public interface Bytes {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/cactoos/Func.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,24 @@
/**
* Function.
*
* <p>If you don't want to have any checked exceptions being thrown
* out of your {@link Func}, you can use
* {@link org.cactoos.func.UncheckedFunc} decorator. Also
* you may try {@link org.cactoos.func.IoCheckedFunc}.</p>
*
* <p>If you want to cache the result of the {@link Func} and
* make sure it doesn't calculate anything twice, you can use
* {@link org.cactoos.func.StickyFunc} decorator.</p>
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @param <X> Type of input
* @param <Y> Type of output
* @see org.cactoos.func.StickyFunc
* @see org.cactoos.func.UncheckedFunc
* @see org.cactoos.func.IoCheckedFunc
* @since 0.1
*/
public interface Func<X, Y> {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/cactoos/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @see org.cactoos.io.BytesAsInput
* @see FileAsInput
* @see org.cactoos.io.PathAsInput
* @since 0.1
*/
public interface Input {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/cactoos/Output.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @see FileAsOutput
* @see org.cactoos.io.PathAsOutput
* @since 0.1
*/
public interface Output {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/cactoos/Proc.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @param <X> Type of input
* @see org.cactoos.func.ProcAsFunc
* @since 0.1
*/
public interface Proc<X> {
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/org/cactoos/Scalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,35 @@
*/
package org.cactoos;

import java.io.IOException;

/**
* Scalar.
*
* <p>If you don't want to have any checked exceptions being thrown
* out of your {@link Scalar}, you can use
* {@link org.cactoos.func.UncheckedScalar} decorator. Also
* you may try {@link org.cactoos.func.IoCheckedScalar}.</p>
*
* <p>If you want to cache the result of the {@link Scalar} and
* make sure it doesn't calculate anything twice, you can use
* {@link org.cactoos.func.StickyScalar} decorator.</p>
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @param <T> Type of result
* @see org.cactoos.func.StickyScalar
* @see org.cactoos.func.UncheckedScalar
* @see org.cactoos.func.IoCheckedScalar
* @since 0.1
*/
public interface Scalar<T> {

/**
* Convert it to the value.
* @return The value
* @throws IOException If fails
* @throws Exception If fails
*/
T asValue() throws IOException;
T asValue() throws Exception;

}
10 changes: 4 additions & 6 deletions src/main/java/org/cactoos/ScalarHasValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
package org.cactoos;

import java.io.IOException;
import org.cactoos.func.UncheckedScalar;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
Expand Down Expand Up @@ -63,11 +63,9 @@ public ScalarHasValue(final Matcher<T> mtr) {

@Override
public boolean matchesSafely(final Scalar<T> item) {
try {
return this.matcher.matches(item.asValue());
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
return this.matcher.matches(
new UncheckedScalar<>(item).asValue()
);
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/cactoos/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @see org.cactoos.text.StringAsText
* @since 0.1
*/
public interface Text {
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/cactoos/TextHasString.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
package org.cactoos;

import java.io.IOException;
import org.cactoos.text.UncheckedText;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
Expand Down Expand Up @@ -62,11 +62,7 @@ public TextHasString(final Matcher<String> mtr) {

@Override
public boolean matchesSafely(final Text item) {
try {
return this.matcher.matches(item.asString());
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
return this.matcher.matches(new UncheckedText(item).asString());
}

@Override
Expand Down
Loading

0 comments on commit 7e961e5

Please sign in to comment.