sorted = new org.cactoos.set.Sorted<>(
(first, second) -> first.compareTo(second),
new IterableOf<>("abc", "bcd", "abc", "ccc", "acd")
);
```
## 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(
n -> {
System.out.printf("Hello, %s!\n", n);
},
names
).value();
```
This is an endless `while/do` loop:
```java
while (!ready) {
System.out.println("Still waiting...");
}
```
Here is its object-oriented alternative:
```java
new And(
ready -> {
System.out.println("Still waiting...");
return !ready;
},
new Endless<>(booleanParameter)
).value();
```
## Dates and Times
From our `org.cactoos.time` package.
Our classes are divided in two groups: those that parse strings into date/time objects, and those that format those objects into strings.
For example, this is the traditional way of parsing a string into an [OffsetDateTime](https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html):
```java
final OffsetDateTime date = OffsetDateTime.parse("2007-12-03T10:15:30+01:00");
```
Here is its object-oriented alternative (no static method calls!) using `OffsetDateTimeOf`, which is a `Scalar`:
```java
final OffsetDateTime date = new OffsetDateTimeOf("2007-12-03T10:15:30+01:00").value();
```
To format an `OffsetDateTime` into a `Text`:
```java
final OffsetDateTime date = ...;
final String text = new TextOf(date).asString();
```
## Our objects vs. their static methods
Cactoos | Guava | Apache Commons | JDK 8
------ | ------ | ------ | ------
`And` | `Iterables.all()` | - | -
`Filtered` | `Iterables.filter()` | ? | -
`FormattedText` | - | - | `String.format()`
`IsBlank` | - | `StringUtils.isBlank()`| -
`Joined` | - | - | `String.join()`
`LengthOf` | - | - | `String#length()`
`Lowered` | - | - | `String#toLowerCase()`
`Normalized` | - | `StringUtils.normalize()` | -
`Or` | `Iterables.any()` | - | -
`Repeated` | - | `StringUtils.repeat()` | -
`Replaced` | - | - | `String#replace()`
`Reversed` | - | - | `StringBuilder#reverse()`
`Rotated` | - | `StringUtils.rotate()`| -
`Split` | - | - | `String#split()`
`StickyList` | `Lists.newArrayList()` | ? | `Arrays.asList()`
`Sub` | - | - | `String#substring()`
`SwappedCase` | - | `StringUtils.swapCase()` | -
`TextOf` | ? | `IOUtils.toString()` | -
`TrimmedLeft` | - | `StringUtils.stripStart()` | -
`TrimmedRight` | - | `StringUtils.stripEnd()` | -
`Trimmed` | - | `StringUtils.stripAll()` | `String#trim()`
`Upper` | - | - | `String#toUpperCase()`
## Questions
Ask your questions related to cactoos library on [Stackoverflow](https://stackoverflow.com/questions/ask) with [cactoos](https://stackoverflow.com/tags/cactoos/info) tag.
## How to contribute?
Just fork the repo and send us a pull request.
Make sure your branch builds without any warnings/issues:
```
mvn clean verify -Pqulice
```
To run a build similar to the CI with Docker only, use:
```
docker run \
--tty \
--interactive \
--workdir=/main \
--volume=${PWD}:/main \
--volume=cactoos-mvn-cache:/root/.m2 \
--rm \
maven:3-jdk-8 \
bash -c "mvn clean install site -Pqulice -Psite --errors; chown -R $(id -u):$(id -g) target/"
```
To remove the cache used by Docker-based build:
```
docker volume rm cactoos-mvn-cache
```
Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static code analyze tool with
[checks list](http://checkstyle.sourceforge.net/checks.html) in GitHub precommits.
## Contributors
- [@yegor256](https://github.com/yegor256) as Yegor Bugayenko ([Blog](http://www.yegor256.com))
- [@g4s8](https://github.com/g4s8) as Kirill Che. (g4s8.public@gmail.com)
- [@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))
- [@alex-semenyuk](https://github.com/alex-semenyuk) as Alexey Semenyuk
- [@smallcreep](https://github.com/smallcreep) as Ilia Rogozhin
- [@memoyil](https://github.com/memoyil) as Mehmet Yildirim
- [@llorllale](https://github.com/llorllale) as George Aristy
- [@driver733](https://github.com/driver733) as Mikhail Yakushin
- [@izrik](https://github.com/izrik) as Richard Sartor
- [@Vatavuk](https://github.com/Vatavuk) as Vedran Grgo Vatavuk
- [@dgroup](https://github.com/dgroup) as Yurii Dubinka
- [@iakunin](https://github.com/iakunin) as Maksim Iakunin
- [@fanifieiev](https://github.com/fanifieiev) as Fevzi Anifieiev
- [@victornoel](https://github.com/victornoel) as Victor Noël
- [@paulodamaso](https://github.com/paulodamaso) as Paulo Lobo
cactoos-0.54.0/pom.xml 0000664 0000000 0000000 00000025524 14300500625 0014553 0 ustar 00root root 0000000 0000000
4.0.0
com.jcabi
parent
0.60.5
org.cactoos
cactoos
1.0-SNAPSHOT
jar
cactoos
Object-Oriented Java Primitives
https://github.com/yegor256/cactoos
2017
Cactoos
https://github.com/yegor256/cactoos
MIT
https://raw.githubusercontent.com/yegor256/cactoos/master/LICENSE.txt
site
1
Yegor Bugayenko
yegor256@gmail.com
yegor256.com
https://www.yegor256.com
Architect
Developer
+3
GitHub
https://github.com/yegor256/cactoos/issues
scm:git:git@github.com:yegor256/cactoos.git
scm:git:git@github.com:yegor256/cactoos.git
https://github.com/yegor256/cactoos
rultor
https://www.rultor.com/s/cactoos
github-pages
https://github.com/yegor256/cactoos
org.takes
takes
1.24.0
test
org.cactoos
cactoos
org.llorllale
cactoos-matchers
0.25
test
org.cactoos
cactoos
com.google.code.findbugs
annotations
3.0.1u2
provided
net.jcip
jcip-annotations
com.google.code.findbugs
jsr305
org.junit.vintage
junit-vintage-engine
5.9.0
test
org.junit.jupiter
junit-jupiter-api
5.9.0
test
org.junit.jupiter
junit-jupiter-params
5.9.0
test
junit
junit
4.13.2
test
org.hamcrest
hamcrest-core
org.apache.maven.plugins
maven-verifier-plugin
1.1
main
package
verify
src/verifier/verifications.xml
org.jacoco
jacoco-maven-plugin
jacoco-initialize
prepare-agent
jacoco-check
test
check
report
BUNDLE
INSTRUCTION
COVEREDRATIO
0.61
LINE
COVEREDRATIO
0.65
BRANCH
COVEREDRATIO
0.65
COMPLEXITY
COVEREDRATIO
0.57
METHOD
COVEREDRATIO
0.57
CLASS
MISSEDCOUNT
15
qulice
com.qulice
qulice-maven-plugin
0.21.1
checkstyle:/src/site/resources/.*
de.thetaphi
forbiddenapis
2.6
./src/test/resources/forbidden-apis.txt
org/cactoos/bytes/*.class
org/cactoos/collection/*.class
org/cactoos/experimental/*.class
org/cactoos/func/*.class
org/cactoos/io/*.class
org/cactoos/proc/*.class
org/cactoos/set/*.class
org/cactoos/test/*.class
org/cactoos/time/*.class
testCheck
sonar
https://sonarcloud.io
yegor256-github
org.cactoos:cactoos
org.codehaus.mojo
sonar-maven-plugin
3.7.0.1746
package
sonar
cactoos-0.54.0/src/ 0000775 0000000 0000000 00000000000 14300500625 0014015 5 ustar 00root root 0000000 0000000 cactoos-0.54.0/src/main/ 0000775 0000000 0000000 00000000000 14300500625 0014741 5 ustar 00root root 0000000 0000000 cactoos-0.54.0/src/main/java/ 0000775 0000000 0000000 00000000000 14300500625 0015662 5 ustar 00root root 0000000 0000000 cactoos-0.54.0/src/main/java/org/ 0000775 0000000 0000000 00000000000 14300500625 0016451 5 ustar 00root root 0000000 0000000 cactoos-0.54.0/src/main/java/org/cactoos/ 0000775 0000000 0000000 00000000000 14300500625 0020104 5 ustar 00root root 0000000 0000000 cactoos-0.54.0/src/main/java/org/cactoos/BiFunc.java 0000664 0000000 0000000 00000003540 14300500625 0022117 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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;
/**
* Function that accepts two arguments.
*
* If you don't want to have any checked exceptions being thrown
* out of your {@link BiFunc}, you can use
* {@link org.cactoos.func.UncheckedBiFunc} decorator. Also
* you may try {@link org.cactoos.func.IoCheckedBiFunc}.
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @param Type of input
* @param Type of output
* @since 0.9
*/
@FunctionalInterface
public interface BiFunc {
/**
* Apply it.
* @param first The first argument
* @param second The second argument
* @return The result
* @throws Exception If fails
*/
Z apply(X first, Y second) throws Exception;
}
cactoos-0.54.0/src/main/java/org/cactoos/BiProc.java 0000664 0000000 0000000 00000003530 14300500625 0022126 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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 org.cactoos.proc.IoCheckedBiProc;
import org.cactoos.proc.UncheckedBiProc;
/**
* Proc that accepts two arguments.
*
* If you don't want to have any checked exceptions being thrown
* out of your {@link BiProc}, you can use
* {@link UncheckedBiProc} decorator. Also
* you may try {@link IoCheckedBiProc}.
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @param Type of input
* @since 0.20
*/
@FunctionalInterface
public interface BiProc {
/**
* Execute it.
* @param first The first argument
* @param second The second argument
* @throws Exception If fails
*/
void exec(X first, Y second) throws Exception;
}
cactoos-0.54.0/src/main/java/org/cactoos/Bytes.java 0000664 0000000 0000000 00000002731 14300500625 0022040 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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;
/**
* Bytes.
*
* There is no thread-safety guarantee.
*
* @see org.cactoos.bytes.BytesOf
* @since 0.1
*/
@FunctionalInterface
public interface Bytes {
/**
* Convert it to the byte array.
* @return The byte array
* @throws Exception If fails
*/
byte[] asBytes() throws Exception;
}
cactoos-0.54.0/src/main/java/org/cactoos/Fallback.java 0000664 0000000 0000000 00000007566 14300500625 0022464 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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 org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;
import org.cactoos.number.MinOf;
import org.cactoos.scalar.InheritanceLevel;
/**
* Fallback from a {@link Throwable}.
*
*
There is no thread-safety guarantee.
*
* @param Type of input
* @since 1.0
*/
public interface Fallback extends Func {
/**
* Calculate level of support of the given exception type.
* @param exception Exception
* @return Level of support: greater or equals to 0 if the target
* is supported and {@link Integer#MIN_VALUE} otherwise
*/
int support(Throwable exception);
/**
* Fallback from exception.
*
* There is no thread-safety guarantee.
*
* @param Type of result
* @since 1.0
*/
final class From implements Fallback {
/**
* The list of exceptions supported by this instance.
*/
private final Iterable> exceptions;
/**
* Function that converts exceptions to the required type.
*/
private final Func func;
/**
* Ctor.
* @param exp Supported exception type
* @param func Function that converts the given exception into required one
*/
@SuppressWarnings("unchecked")
public From(final Class extends Throwable> exp,
final Func func) {
this(new IterableOf<>(exp), func);
}
/**
* Ctor.
* @param exps Supported exceptions types
* @param func Function that converts the given exception into required one
*/
public From(
final Iterable> exps,
final Func func) {
this.exceptions = exps;
this.func = func;
}
@Override
public T apply(final Throwable exp) throws Exception {
return this.func.apply(exp);
}
@Override
public int support(final Throwable exception) {
return new MinOf(
new Mapped<>(
supported -> new InheritanceLevel(exception.getClass(), supported).value(),
this.exceptions
)
).intValue();
}
}
/**
* No fallback. Throw unchecked exception.
*
* @param Type of result.
* @since 1.0
*/
final class None implements Fallback {
@Override
public int support(final Throwable exception) {
return 0;
}
@Override
public X apply(final Throwable input) throws Exception {
throw new IllegalStateException(input);
}
}
}
cactoos-0.54.0/src/main/java/org/cactoos/Func.java 0000664 0000000 0000000 00000004014 14300500625 0021641 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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;
/**
* Function.
*
* 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}.
*
* 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.
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @param Type of output
* @see org.cactoos.func.StickyFunc
* @see org.cactoos.func.UncheckedFunc
* @see org.cactoos.func.IoCheckedFunc
* @since 0.1
*/
@FunctionalInterface
public interface Func {
/**
* Apply it.
* @param input The argument
* @return The result
* @throws Exception If fails
*/
Y apply(X input) throws Exception;
}
cactoos-0.54.0/src/main/java/org/cactoos/Input.java 0000664 0000000 0000000 00000003623 14300500625 0022052 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.io.InputStream;
/**
* Input.
*
* Here is for example how {@link Input} can be used
* in order to read the content of a text file:
*
* String content = new TextOf(
* new InputOf(new File("/tmp/names.txt"))
* ).asString();
*
* Here {@link org.cactoos.io.InputOf} implements
* {@link Input} and behaves like
* one, providing read-only access to
* the encapsulated {@link java.io.File}.
*
* There is no thread-safety guarantee.
*
* @see org.cactoos.io.InputOf
* @since 0.1
*/
@FunctionalInterface
public interface Input {
/**
* Get read access to it.
* @return InputStream to read from
* @throws Exception If something goes wrong
*/
InputStream stream() throws Exception;
}
cactoos-0.54.0/src/main/java/org/cactoos/Output.java 0000664 0000000 0000000 00000004265 14300500625 0022256 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.io.OutputStream;
import org.cactoos.io.OutputTo;
import org.cactoos.io.TeeInput;
/**
* Output.
*
*
Here is for example how {@link Output} can be used
* together with {@link Input} in order to modify the content
* of a text file:
*
* new LengthOf(
* new TeeInput(
* new InputOf(new TextOf("Hello, world!")),
* new OutputTo(new File("/tmp/names.txt"))
* )
* ).asValue();
*
* Here {@link OutputTo} 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.scalar.LengthOf}
* calculates the size of the copied data.
*
* There is no thread-safety guarantee.
*
* @see OutputTo
* @since 0.1
*/
@FunctionalInterface
public interface Output {
/**
* Get write access to it.
* @return OutputStream to write to
* @throws Exception If something goes wrong
*/
OutputStream stream() throws Exception;
}
cactoos-0.54.0/src/main/java/org/cactoos/Proc.java 0000664 0000000 0000000 00000003375 14300500625 0021662 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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 org.cactoos.proc.IoCheckedProc;
import org.cactoos.proc.UncheckedProc;
/**
* Procedure.
*
*
If you don't want to have any checked exceptions being thrown
* out of your {@link Proc}, you can use
* {@link UncheckedProc} decorator. Also
* you may try {@link IoCheckedProc}.
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @see org.cactoos.func.FuncOf
* @since 0.1
*/
@FunctionalInterface
public interface Proc {
/**
* Execute it.
* @param input The argument
* @throws Exception If fails
*/
void exec(X input) throws Exception;
}
cactoos-0.54.0/src/main/java/org/cactoos/Scalar.java 0000664 0000000 0000000 00000003707 14300500625 0022163 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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 org.cactoos.scalar.IoChecked;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.Unchecked;
/**
* Scalar.
*
* If you don't want to have any checked exceptions being thrown
* out of your {@link Scalar}, you can use
* {@link Unchecked} decorator. Also
* you may try {@link IoChecked}.
*
* If you want to cache the result of the {@link Scalar} and
* make sure it doesn't calculate anything twice, you can use
* {@link Sticky} decorator.
*
* There is no thread-safety guarantee.
*
* @param Type of result
* @see Sticky
* @see Unchecked
* @see IoChecked
* @since 0.1
*/
@FunctionalInterface
public interface Scalar {
/**
* Convert it to the value.
* @return The value
* @throws Exception If fails
*/
T value() throws Exception;
}
cactoos-0.54.0/src/main/java/org/cactoos/Text.java 0000664 0000000 0000000 00000003166 14300500625 0021701 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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;
/**
* Text.
*
* If you don't want to have any checked exceptions being thrown
* out of your {@link Text}, you can use
* {@link org.cactoos.text.UncheckedText} decorator.
*
* There is no thread-safety guarantee.
*
* @see org.cactoos.text.TextOf
* @since 0.1
*/
@FunctionalInterface
public interface Text {
/**
* Convert it to the string.
* @return The string
* @throws Exception If fails
*/
String asString() throws Exception;
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/ 0000775 0000000 0000000 00000000000 14300500625 0021232 5 ustar 00root root 0000000 0000000 cactoos-0.54.0/src/main/java/org/cactoos/bytes/Base64Bytes.java 0000664 0000000 0000000 00000004061 14300500625 0024131 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import java.util.Base64;
import org.cactoos.Bytes;
/**
* Decodes all origin bytes using the Base64 encoding scheme.
*
* @since 0.20.2
*/
public final class Base64Bytes implements Bytes {
/**
* Origin bytes.
*/
private final Bytes origin;
/**
* The decoder.
*/
private final Base64.Decoder decoder;
/**
* Ctor uses a RFC4648 {@link Base64.Decoder}.
*
* @param origin Origin bytes
*/
public Base64Bytes(final Bytes origin) {
this(origin, Base64.getDecoder());
}
/**
* Ctor.
*
* @param origin Origin bytes.
* @param dec Decoder to use.
*/
public Base64Bytes(final Bytes origin, final Base64.Decoder dec) {
this.origin = origin;
this.decoder = dec;
}
@Override
public byte[] asBytes() throws Exception {
return this.decoder.decode(this.origin.asBytes());
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/BytesBase64.java 0000664 0000000 0000000 00000004076 14300500625 0024137 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import java.util.Base64;
import org.cactoos.Bytes;
/**
* Encodes all origin bytes using the Base64 encoding scheme.
*
* @since 0.20.2
*/
public final class BytesBase64 implements Bytes {
/**
* Origin bytes.
*/
private final Bytes origin;
/**
* The encoder to use.
*/
private final Base64.Encoder encoder;
/**
* Ctor uses a RFC4648 {@link Base64.Encoder}.
*
* @param origin Origin bytes.
*/
public BytesBase64(final Bytes origin) {
this(origin, Base64.getEncoder());
}
/**
* Ctor.
*
* @param origin Origin bytes.
* @param enc The encoder to use.
*/
public BytesBase64(final Bytes origin, final Base64.Encoder enc) {
this.origin = origin;
this.encoder = enc;
}
@Override
public byte[] asBytes() throws Exception {
return this.encoder.encode(this.origin.asBytes());
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/BytesOf.java 0000664 0000000 0000000 00000022373 14300500625 0023457 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.io.InputOf;
import org.cactoos.iterable.IterableOf;
import org.cactoos.list.ListOf;
/**
* A {@link Bytes} that encapsulates other sources of data.
*
*
There is no thread-safety guarantee.
*
* @since 0.12
*/
public final class BytesOf implements Bytes {
/**
* The bytes.
*/
private final Bytes origin;
/**
* Ctor.
* @param input The input
*/
public BytesOf(final Input input) {
this(new InputAsBytes(input));
}
/**
* Ctor.
* @param input The input
* @since 0.29.2
*/
public BytesOf(final InputStream input) {
this(new InputAsBytes(new InputOf(input)));
}
/**
* Ctor.
* @param file The input
* @since 0.13
*/
public BytesOf(final File file) {
this(new InputOf(file));
}
/**
* Ctor.
* @param path The input
* @since 0.13
*/
public BytesOf(final Path path) {
this(new InputOf(path));
}
/**
* Ctor.
* @param input The input
* @param max Max length of the buffer for reading
*/
public BytesOf(final Input input, final int max) {
this(new InputAsBytes(input, max));
}
/**
* Ctor.
* @param rdr Reader
*/
public BytesOf(final Reader rdr) {
this(new ReaderAsBytes(rdr));
}
/**
* Ctor.
* @param rdr Reader
* @param charset Charset
*/
public BytesOf(final Reader rdr, final Charset charset) {
this(new ReaderAsBytes(rdr, charset));
}
/**
* Ctor.
* @param rdr Reader
* @param charset Charset
*/
public BytesOf(final Reader rdr, final CharSequence charset) {
this(new ReaderAsBytes(rdr, charset));
}
/**
* Ctor.
* @param rdr Reader
* @param charset Charset
* @param max Buffer size
*/
public BytesOf(final Reader rdr, final Charset charset, final int max) {
this(new ReaderAsBytes(rdr, charset, max));
}
/**
* Ctor.
* @param rdr Reader
* @param max Buffer size
* @since 0.13.3
*/
public BytesOf(final Reader rdr, final int max) {
this(new ReaderAsBytes(rdr, max));
}
/**
* Ctor.
* @param rdr Reader
* @param charset Charset
* @param max Buffer size
*/
public BytesOf(final Reader rdr, final CharSequence charset,
final int max) {
this(new ReaderAsBytes(rdr, charset, max));
}
/**
* Ctor.
*
* @param input The source
*/
public BytesOf(final CharSequence input) {
this(input, StandardCharsets.UTF_8);
}
/**
* Ctor.
*
* @param input The source
* @param charset The charset
*/
public BytesOf(final CharSequence input, final Charset charset) {
this(() -> input.toString().getBytes(charset));
}
/**
* Ctor.
*
* @param input The source
* @param charset The charset
*/
public BytesOf(final CharSequence input, final CharSequence charset) {
this(() -> input.toString().getBytes(charset.toString()));
}
/**
* Ctor.
*
* @param chars The chars
*/
public BytesOf(final char... chars) {
this(chars, StandardCharsets.UTF_8);
}
/**
* Ctor.
*
* @param chars The chars
* @param charset The charset
*/
public BytesOf(final char[] chars, final Charset charset) {
this(new String(chars), charset);
}
/**
* Ctor.
*
* @param chars The chars
* @param charset The charset
*/
public BytesOf(final char[] chars, final CharSequence charset) {
this(new String(chars), charset);
}
/**
* Ctor.
* @param text The source
*/
public BytesOf(final Text text) {
this(text, StandardCharsets.UTF_8);
}
/**
* Ctor.
* @param text The source
* @param charset The charset
*/
public BytesOf(final Text text, final Charset charset) {
this(() -> text.asString().getBytes(charset));
}
/**
* Ctor.
* @param text The source
* @param charset The charset
*/
public BytesOf(final Text text, final CharSequence charset) {
this(() -> text.asString().getBytes(charset.toString()));
}
/**
* Ctor.
* @param error The exception to serialize
*/
public BytesOf(final Throwable error) {
this(error, StandardCharsets.UTF_8);
}
/**
* Ctor.
* @param error The exception to serialize
* @param charset Charset
*/
public BytesOf(final Throwable error, final Charset charset) {
this(error, charset.name());
}
/**
* Ctor.
* @param error The exception to serialize
* @param charset Charset
*/
public BytesOf(final Throwable error, final CharSequence charset) {
this(
() -> {
try (
ByteArrayOutputStream baos =
new ByteArrayOutputStream()
) {
error.printStackTrace(
new PrintStream(baos, true, charset.toString())
);
return baos.toByteArray();
}
}
);
}
/**
* Ctor.
* @param strace The stack trace
* @since 0.29
*/
public BytesOf(final StackTraceElement... strace) {
this(strace, StandardCharsets.UTF_8);
}
/**
* Ctor.
* @param strace The stack trace
* @param charset Charset
* @since 0.29
*/
public BytesOf(final StackTraceElement[] strace, final Charset charset) {
this(strace, charset.name());
}
/**
* Ctor.
* @param strace The stack trace
* @param charset Charset
* @since 0.29
*/
public BytesOf(final StackTraceElement[] strace,
final CharSequence charset) {
this(
() -> {
try (
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
PrintStream stream = new PrintStream(
baos, true, charset.toString()
)
) {
for (final StackTraceElement element : strace) {
stream.append(element.toString());
stream.append("\n");
}
return baos.toByteArray();
}
}
);
}
/**
* Ctor.
*
* @param bytes Bytes to encapsulate
*/
public BytesOf(final byte... bytes) {
this(() -> bytes);
}
/**
* Ctor.
*
* @param iterator Iterator of bytes
*/
public BytesOf(final Iterator iterator) {
this(new IterableOf<>(iterator));
}
/**
* Ctor.
*
* @param bytes Iterable of bytes
*/
public BytesOf(final Iterable bytes) {
this(() -> {
final List concrete = new ListOf<>(bytes);
final ByteBuffer buf = ByteBuffer.allocate(
concrete.size()
);
concrete.forEach(buf::put);
return buf.array();
});
}
/**
* Ctor.
*
* @param bytes Collection of bytes
*/
public BytesOf(final Collection bytes) {
this(() -> {
final ByteBuffer buf = ByteBuffer.allocate(
bytes.size()
);
bytes.forEach(buf::put);
return buf.array();
});
}
/**
* Ctor.
*
* @param bytes Bytes to encapsulate
*/
private BytesOf(final Bytes bytes) {
this.origin = bytes;
}
@Override
public byte[] asBytes() throws Exception {
return this.origin.asBytes();
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/CheckedBytes.java 0000664 0000000 0000000 00000004074 14300500625 0024437 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import org.cactoos.Bytes;
import org.cactoos.Func;
import org.cactoos.scalar.Checked;
/**
* Bytes that throws exception of specified type.
*
* @param Exception's type.
* @since 0.31
*/
public final class CheckedBytes implements Bytes {
/**
* Original bytes.
*/
private final Bytes origin;
/**
* Function that wraps exception of {@link #origin} to the required type.
*/
private final Func super Exception, ? extends E> func;
/**
* Ctor.
* @param orig Origin bytes.
* @param fnc Function that wraps exceptions.
*/
public CheckedBytes(final Bytes orig, final Func super Exception, ? extends E> fnc) {
this.origin = orig;
this.func = fnc;
}
@Override
public byte[] asBytes() throws E {
return new Checked<>(
this.origin::asBytes,
this.func
).value();
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/DigestEnvelope.java 0000664 0000000 0000000 00000005720 14300500625 0025016 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.cactoos.Bytes;
import org.cactoos.Input;
/**
* Digest Envelope.
*
* There is no thread-safety guarantee.
*
* @since 0.29
* @checkstyle AbstractClassNameCheck (500 lines)
* @checkstyle DesignForExtensionCheck (500 lines)
*/
@SuppressWarnings("PMD.AbstractNaming")
public abstract class DigestEnvelope implements Bytes {
/**
* The input.
*/
private final Input source;
/**
* The buffer size.
*/
private final int size;
/**
* The algorithm.
*/
private final String algorithm;
/**
* Ctor.
* @param input The input
* @param algrthm The algorithm
*/
public DigestEnvelope(final Input input, final String algrthm) {
this(input, 16 << 10, algrthm);
}
/**
* Ctor.
* @param input The input
* @param max Buffer size
* @param algrthm The algorithm
*/
public DigestEnvelope(
final Input input,
final int max,
final String algrthm
) {
this.source = input;
this.size = max;
this.algorithm = algrthm;
}
@Override
public byte[] asBytes() throws Exception {
try (InputStream stream = this.source.stream()) {
final MessageDigest msg = MessageDigest.getInstance(this.algorithm);
final byte[] buf = new byte[this.size];
while (true) {
final int len = stream.read(buf);
if (len < 0) {
break;
}
msg.update(buf, 0, len);
}
return msg.digest();
} catch (final NoSuchAlgorithmException ex) {
throw new IOException(ex);
}
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/EmptyBytes.java 0000664 0000000 0000000 00000002771 14300500625 0024211 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import org.cactoos.Bytes;
/**
* Bytes with no data.
*
*
There is no thread-safety guarantee.
*
* @since 0.2
*/
public final class EmptyBytes implements Bytes {
/**
* Empty array of bytes.
*/
private static final byte[] EMPTY = {};
@Override
public byte[] asBytes() {
return EmptyBytes.EMPTY;
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/HexOf.java 0000664 0000000 0000000 00000005702 14300500625 0023112 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Iterator;
import org.cactoos.Bytes;
import org.cactoos.Text;
import org.cactoos.iterator.Mapped;
import org.cactoos.text.FormattedText;
/**
* Decodes origin {@link Text} using the hexadecimal encoding scheme.
*
* @since 0.30
*/
public final class HexOf implements Bytes {
/**
* Origin hexadecimal text.
*/
private final Text origin;
/**
* Ctor.
*
* @param origin Hexadecimal text.
*/
public HexOf(final Text origin) {
this.origin = origin;
}
@Override
public byte[] asBytes() throws Exception {
final String hex = this.origin.asString();
if ((hex.length() & 1) == 1) {
throw new IOException("Length of hexadecimal text is odd");
}
final Iterator iter = new Mapped<>(
c -> {
final int result = Character.digit(c, 16);
if (result == -1) {
throw new IOException(
new FormattedText(
"Unexpected character '%c'",
c
).asString()
);
}
return result;
},
hex.chars().mapToObj(c -> (char) c).iterator()
);
final byte[] result = new byte[hex.length() / 2];
int index = 0;
while (index < hex.length()) {
try {
final int most = iter.next();
final int less = iter.next();
result[index >>> 1] = (byte) ((most << 4) + less);
index += 2;
} catch (final UncheckedIOException ex) {
throw ex.getCause();
}
}
return result;
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/InputAsBytes.java 0000664 0000000 0000000 00000005071 14300500625 0024472 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.io.OutputTo;
import org.cactoos.io.TeeInput;
/**
* Input as Byte Array.
*
* This class is for internal use only. Use {@link BytesOf} instead.
*
* There is no thread-safety guarantee.
* @since 0.1
*/
public final class InputAsBytes implements Bytes {
/**
* The input.
*/
private final Input source;
/**
* The buffer size.
*/
private final int size;
/**
* Ctor.
* @param input The input
*/
InputAsBytes(final Input input) {
this(input, 16 << 10);
}
/**
* Ctor.
* @param input The input
* @param max Max length of the buffer for reading
*/
InputAsBytes(final Input input, final int max) {
this.source = input;
this.size = max;
}
@Override
public byte[] asBytes() throws Exception {
try (
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = new TeeInput(
this.source,
new OutputTo(baos)
).stream()
) {
final byte[] buf = new byte[this.size];
while (true) {
if (stream.read(buf) < 0) {
break;
}
}
return baos.toByteArray();
}
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/IoCheckedBytes.java 0000664 0000000 0000000 00000005072 14300500625 0024726 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import java.io.IOException;
import org.cactoos.Bytes;
import org.cactoos.Fallback;
import org.cactoos.scalar.IoChecked;
/**
* Bytes that doesn't throw checked {@link Exception},
* but only throws {@link IOException}.
*
*
There is no thread-safety guarantee.
*
* @since 0.52
*/
public final class IoCheckedBytes implements Bytes {
/**
* Scalar with fallback.
*/
private final IoChecked scalar;
/**
* Ctor.
* @param bts Encapsulated bytes
*/
public IoCheckedBytes(final Bytes bts) {
this(bts, new Fallback.None<>());
}
/**
* Ctor.
* @param bts Encapsulated bytes
* @param fbk Fallback
* @since 0.5
*/
@SuppressWarnings({"unchecked", "PMD.AvoidRethrowingException", "PMD.AvoidCatchingThrowable"})
public IoCheckedBytes(final Bytes bts, final Fallback fbk) {
this.scalar = new IoChecked<>(
() -> {
byte[] ret;
try {
ret = bts.asBytes();
} catch (final IOException ex) {
throw ex;
// @checkstyle IllegalCatchCheck (1 line)
} catch (final Throwable ex) {
ret = fbk.apply(ex);
}
return ret;
}
);
}
@Override
public byte[] asBytes() throws IOException {
return this.scalar.value();
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/Md5DigestOf.java 0000664 0000000 0000000 00000003340 14300500625 0024147 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import org.cactoos.Input;
/**
* MD5 checksum calculation of {@link Input}.
*
* There is no thread-safety guarantee.
*
* @since 0.29
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class Md5DigestOf extends DigestEnvelope {
/**
* Ctor.
* @param input The input
*/
public Md5DigestOf(final Input input) {
super(input, "MD5");
}
/**
* Ctor.
* @param input The input
* @param max Buffer size
*/
public Md5DigestOf(final Input input, final int max) {
super(input, max, "MD5");
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/NoNulls.java 0000664 0000000 0000000 00000003654 14300500625 0023477 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import org.cactoos.Bytes;
/**
* Bytes check for no nulls.
*
* @since 0.11
*/
public final class NoNulls implements Bytes {
/**
* The input.
*/
private final Bytes origin;
/**
* Ctor.
* @param bytes The input
*/
public NoNulls(final Bytes bytes) {
this.origin = bytes;
}
@Override
public byte[] asBytes() throws Exception {
if (this.origin == null) {
throw new IllegalArgumentException(
"NULL instead of a valid bytes"
);
}
final byte[] bytes = this.origin.asBytes();
if (bytes == null) {
throw new IllegalStateException(
"NULL instead of a valid byte array"
);
}
return bytes;
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/ReaderAsBytes.java 0000664 0000000 0000000 00000006715 14300500625 0024603 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.cactoos.Bytes;
/**
* Reader as {@link Bytes}.
*
*
This class is for internal use only. Use {@link BytesOf} instead.
*
* There is no thread-safety guarantee.
*
* @since 0.12
*/
final class ReaderAsBytes implements Bytes {
/**
* The reader.
*/
private final Reader reader;
/**
* The charset.
*/
private final CharSequence charset;
/**
* The buffer size.
*/
private final int size;
/**
* Ctor.
*
* @param rdr Reader
*/
ReaderAsBytes(final Reader rdr) {
this(rdr, StandardCharsets.UTF_8);
}
/**
* Ctor.
*
* @param rdr Reader
* @param cset Charset
*/
ReaderAsBytes(final Reader rdr, final Charset cset) {
this(rdr, cset, 16 << 10);
}
/**
* Ctor.
*
* @param rdr Reader
* @param cset Charset
* @since 0.13.2
*/
ReaderAsBytes(final Reader rdr, final CharSequence cset) {
this(rdr, cset, 16 << 10);
}
/**
* Ctor.
*
* @param rdr Reader
* @param cset Charset
* @param max Buffer size
*/
ReaderAsBytes(final Reader rdr, final Charset cset, final int max) {
this(rdr, cset.name(), max);
}
/**
* Ctor.
* @param rdr Reader
* @param max Buffer size
* @since 0.13.3
*/
ReaderAsBytes(final Reader rdr, final int max) {
this(rdr, StandardCharsets.UTF_8, max);
}
/**
* Ctor.
*
* @param rdr Reader
* @param cset Charset
* @param max Buffer size
*/
ReaderAsBytes(final Reader rdr, final CharSequence cset, final int max) {
this.reader = rdr;
this.charset = cset;
this.size = max;
}
@Override
public byte[] asBytes() throws Exception {
final char[] buffer = new char[this.size];
final StringBuilder builder = new StringBuilder(this.size);
while (true) {
final int done = this.reader.read(buffer, 0, buffer.length);
if (done < 0) {
break;
}
builder.append(buffer, 0, done);
}
this.reader.close();
return builder.toString().getBytes(this.charset.toString());
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/Sha1DigestOf.java 0000664 0000000 0000000 00000003351 14300500625 0024320 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import org.cactoos.Input;
/**
* SHA-1 checksum calculation of {@link Input}.
*
*
There is no thread-safety guarantee.
*
* @since 0.29
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class Sha1DigestOf extends DigestEnvelope {
/**
* Ctor.
* @param input The input
*/
public Sha1DigestOf(final Input input) {
super(input, "SHA-1");
}
/**
* Ctor.
* @param input The input
* @param max Buffer size
*/
public Sha1DigestOf(final Input input, final int max) {
super(input, max, "SHA-1");
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/Sha256DigestOf.java 0000664 0000000 0000000 00000003365 14300500625 0024501 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import org.cactoos.Input;
/**
* SHA-256 checksum calculation of {@link Input}.
*
*
There is no thread-safety guarantee.
*
* @since 0.29
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class Sha256DigestOf extends DigestEnvelope {
/**
* Ctor.
* @param input The input
*/
public Sha256DigestOf(final Input input) {
super(input, "SHA-256");
}
/**
* Ctor.
* @param input The input
* @param max Buffer size
*/
public Sha256DigestOf(final Input input, final int max) {
super(input, max, "SHA-256");
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/UncheckedBytes.java 0000664 0000000 0000000 00000004463 14300500625 0025004 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.bytes;
import org.cactoos.Bytes;
import org.cactoos.Fallback;
import org.cactoos.scalar.ScalarWithFallback;
import org.cactoos.scalar.Unchecked;
/**
* Bytes that doesn't throw checked {@link Exception}.
*
*
There is no thread-safety guarantee.
*
* @since 0.3
* @todo #1615:30m Extract fallback logic for Bytes
* to a separate class in accordance
* to other XXXWithFallback classes.
* Leave only basic exception handling in this class.
*/
public final class UncheckedBytes implements Bytes {
/**
* Scalar with fallback.
*/
private final Unchecked scalar;
/**
* Ctor.
* @param bts Encapsulated bytes
*/
public UncheckedBytes(final Bytes bts) {
this(bts, new Fallback.None<>());
}
/**
* Ctor.
* @param bts Encapsulated bytes
* @param fbk Fallback
* @since 0.5
*/
@SuppressWarnings("unchecked")
public UncheckedBytes(final Bytes bts, final Fallback fbk) {
this.scalar = new Unchecked<>(
new ScalarWithFallback<>(bts::asBytes, fbk)
);
}
@Override
public byte[] asBytes() {
return this.scalar.value();
}
}
cactoos-0.54.0/src/main/java/org/cactoos/bytes/package-info.java 0000664 0000000 0000000 00000002304 14300500625 0024420 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.
*/
/**
* Bytes.
*
* @since 0.20.2
*/
package org.cactoos.bytes;
cactoos-0.54.0/src/main/java/org/cactoos/collection/ 0000775 0000000 0000000 00000000000 14300500625 0022237 5 ustar 00root root 0000000 0000000 cactoos-0.54.0/src/main/java/org/cactoos/collection/CollectionEnvelope.java 0000664 0000000 0000000 00000005762 14300500625 0026705 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.collection;
import java.util.Collection;
import org.cactoos.iterable.IterableEnvelope;
/**
* Base collection.
*
* There is no thread-safety guarantee.
* @param Element type
* @since 0.23
*/
public abstract class CollectionEnvelope
extends IterableEnvelope implements Collection {
/**
* The wrapped collection.
*/
private final Collection col;
/**
* Ctor.
* @param col The wrapped collection
*/
public CollectionEnvelope(final Collection col) {
super(col);
this.col = col;
}
@Override
public final int size() {
return this.col.size();
}
@Override
public final boolean isEmpty() {
return this.col.isEmpty();
}
@Override
public final boolean contains(final Object object) {
return this.col.contains(object);
}
@Override
public final Object[] toArray() {
return this.col.toArray();
}
@Override
public final T[] toArray(final T[] array) {
return this.col.toArray(array);
}
@Override
public final boolean add(final X item) {
return this.col.add(item);
}
@Override
public final boolean remove(final Object object) {
return this.col.remove(object);
}
@Override
public final boolean containsAll(final Collection> list) {
return this.col.containsAll(list);
}
@Override
public final boolean addAll(final Collection extends X> list) {
return this.col.addAll(list);
}
@Override
public final boolean removeAll(final Collection> list) {
return this.col.removeAll(list);
}
@Override
public final boolean retainAll(final Collection> list) {
return this.col.retainAll(list);
}
@Override
public final void clear() {
this.col.clear();
}
}
cactoos-0.54.0/src/main/java/org/cactoos/collection/Immutable.java 0000664 0000000 0000000 00000010136 14300500625 0025022 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.collection;
import java.util.Collection;
import java.util.Iterator;
/**
* Decorator that doesn't allow any mutation of the wrapped {@link Collection}.
*
* There is no thread-safety guarantee.
*
* @param Type of source item
* @since 1.16
* @todo #898:30min Replace all the Collections.unmodifiableCollection
* with the {@link Immutable} from the cactoos codebase.
* That should be done because Elegant Object principles are against static methods.
*/
@SuppressWarnings(
{
"PMD.TooManyMethods",
"PMD.AbstractNaming"
}
)
public final class Immutable implements Collection {
/**
* Original collection.
*/
private final Collection extends X> col;
/**
* Ctor.
* @param src Source collection
*/
public Immutable(final Collection extends X> src) {
this.col = src;
}
@Override
public int size() {
return this.col.size();
}
@Override
public boolean isEmpty() {
return this.col.isEmpty();
}
@Override
public Iterator iterator() {
return new org.cactoos.iterator.Immutable<>(this.col.iterator());
}
@Override
public boolean contains(final Object object) {
return this.col.contains(object);
}
@Override
public Object[] toArray() {
return this.col.toArray();
}
@Override
@SuppressWarnings("PMD.UseVarargs")
public T[] toArray(final T[] array) {
return this.col.toArray(array);
}
@Override
public boolean add(final X item) {
throw new UnsupportedOperationException(
"#add(): the collection is read-only"
);
}
@Override
public boolean remove(final Object object) {
throw new UnsupportedOperationException(
"#remove(): the collection is read-only"
);
}
@Override
public boolean containsAll(final Collection> list) {
return this.col.containsAll(list);
}
@Override
public boolean addAll(final Collection extends X> list) {
throw new UnsupportedOperationException(
"#addAll(): the collection is read-only"
);
}
@Override
public boolean removeAll(final Collection> list) {
throw new UnsupportedOperationException(
"#removeAll(): the collection is read-only"
);
}
@Override
public boolean retainAll(final Collection> list) {
throw new UnsupportedOperationException(
"#retainAll(): the collection is read-only"
);
}
@Override
public void clear() {
throw new UnsupportedOperationException(
"#clear(): the collection is read-only"
);
}
@Override
public String toString() {
return this.col.toString();
}
@Override
public int hashCode() {
return this.col.hashCode();
}
@Override
public boolean equals(final Object obj) {
return this.col.equals(obj);
}
}
cactoos-0.54.0/src/main/java/org/cactoos/collection/NoNulls.java 0000664 0000000 0000000 00000012044 14300500625 0024475 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.collection;
import java.util.Collection;
import java.util.Iterator;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;
/**
* A decorator of {@link Collection} that tolerates no NULLs.
*
* There is no thread-safety guarantee.
*
* @param Element type
* @since 0.27
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class NoNulls implements Collection {
/**
* Original collection.
*/
private final Collection col;
/**
* Ctor without '? extends X' wildcard because of the issue:
*
* - The client gets Collection of X
* - The client adds item of X to it
* - The client queries items of original Collection of ? extends X
* - Runtime exception occurs
*
* @param items Original one.
*/
public NoNulls(final Collection items) {
this.col = items;
}
@Override
public int size() {
return this.col.size();
}
@Override
public boolean isEmpty() {
return this.col.isEmpty();
}
@Override
public Iterator iterator() {
return new org.cactoos.iterator.NoNulls<>(this.col.iterator());
}
@Override
public boolean contains(final Object item) {
if (item == null) {
throw new IllegalArgumentException(
"Argument of #contains(T) is NULL"
);
}
return this.col.contains(item);
}
@Override
public Object[] toArray() {
final Object[] array = this.col.toArray();
for (int idx = 0; idx < array.length; ++idx) {
if (array[idx] == null) {
throw new IllegalStateException(
new UncheckedText(
new FormattedText(
"Item #%d of #toArray() is NULL", idx
)
).asString()
);
}
}
return array;
}
@Override
@SuppressWarnings("PMD.UseVarargs")
public T[] toArray(final T[] array) {
this.col.toArray((Object[]) array);
for (int idx = 0; idx < array.length; ++idx) {
if (array[idx] == null) {
throw new IllegalStateException(
new UncheckedText(
new FormattedText(
"Item #%d of #toArray(array) is NULL", idx
)
).asString()
);
}
}
return array;
}
@Override
public boolean add(final X item) {
if (item == null) {
throw new IllegalStateException(
"Item of #add(T) is NULL"
);
}
return this.col.add(item);
}
@Override
public boolean remove(final Object item) {
if (item == null) {
throw new IllegalStateException(
"Item of #remove(T) is NULL"
);
}
return this.col.remove(item);
}
@Override
public boolean containsAll(final Collection> items) {
return this.col.containsAll(new NoNulls<>(items));
}
@Override
public boolean addAll(final Collection extends X> items) {
return this.col.addAll(new NoNulls<>(items));
}
@Override
public boolean removeAll(final Collection> items) {
return this.col.removeAll(new NoNulls<>(items));
}
@Override
public boolean retainAll(final Collection> items) {
return this.col.retainAll(new NoNulls<>(items));
}
@Override
public void clear() {
this.col.clear();
}
@Override
public String toString() {
return this.col.toString();
}
@Override
public boolean equals(final Object obj) {
return this.col.equals(obj);
}
@Override
public int hashCode() {
return this.col.hashCode();
}
}
cactoos-0.54.0/src/main/java/org/cactoos/collection/package-info.java 0000664 0000000 0000000 00000002324 14300500625 0025427 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.
*/
/**
* Collections, tests.
*
* @since 0.14
*/
package org.cactoos.collection;
cactoos-0.54.0/src/main/java/org/cactoos/experimental/ 0000775 0000000 0000000 00000000000 14300500625 0022601 5 ustar 00root root 0000000 0000000 cactoos-0.54.0/src/main/java/org/cactoos/experimental/Threads.java 0000664 0000000 0000000 00000014401 14300500625 0025036 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.experimental;
import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.cactoos.Func;
import org.cactoos.Scalar;
import org.cactoos.func.UncheckedFunc;
import org.cactoos.iterable.IterableEnvelope;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;
import org.cactoos.list.ListOf;
import org.cactoos.scalar.CallableOf;
/**
* Allows to execute the tasks concurrently, optionally within given timeout.
*
* @param The type of task result item.
* @since 1.0.0
*/
public final class Threads extends IterableEnvelope {
/**
* Ctor.
* @param exc The executor.
* @param tasks The tasks to be executed concurrently.
*/
@SafeVarargs
public Threads(final ExecutorService exc, final Scalar extends T>... tasks) {
this(exc, new IterableOf<>(tasks));
}
/**
* Ctor.
* @param exc The executor.
* @param tasks The tasks to be executed concurrently.
*/
public Threads(final ExecutorService exc, final Iterable extends Scalar extends T>> tasks) {
this(input -> exc.invokeAll(new ListOf<>(input)), tasks);
}
/**
* Ctor.
* @param threads The quantity of threads which will be used within the
* {@link ExecutorService}.
* @param tasks The tasks to be executed concurrently.
* @see Executors#newFixedThreadPool(int)
*/
@SafeVarargs
public Threads(final int threads, final Scalar extends T>... tasks) {
this(threads, new IterableOf<>(tasks));
}
/**
* Ctor.
* @param threads The quantity of threads which will be used within the
* {@link ExecutorService}.
* @param tasks The tasks to be executed concurrently.
*/
public Threads(final int threads, final Iterable extends Scalar extends T>> tasks) {
this(
todo -> {
final ExecutorService executor = Executors.newFixedThreadPool(
threads
);
try {
return executor.invokeAll(new ListOf<>(todo));
} finally {
executor.shutdown();
}
},
tasks
);
}
/**
* Ctor.
* @param exc The executor.
* @param timeout The maximum time to wait.
* @param tasks The tasks to be executed concurrently.
*/
@SafeVarargs
public Threads(
final ExecutorService exc,
final Duration timeout,
final Scalar extends T>... tasks
) {
this(exc, timeout, new IterableOf<>(tasks));
}
/**
* Ctor.
* @param exc The executor.
* @param timeout The maximum time to wait.
* @param tasks The tasks to be executed concurrently.
*/
public Threads(
final ExecutorService exc,
final Duration timeout,
final Iterable extends Scalar extends T>> tasks
) {
this(
input -> exc.invokeAll(
new ListOf<>(input),
timeout.toNanos(), TimeUnit.NANOSECONDS
),
tasks
);
}
/**
* Ctor.
* @param threads The quantity of threads which will be used within the
* {@link ExecutorService}.
* @param timeout The maximum time to wait.
* @param tasks The tasks to be executed concurrently.
* @see Executors#newFixedThreadPool(int)
*/
@SafeVarargs
public Threads(
final int threads,
final Duration timeout,
final Scalar extends T>... tasks
) {
this(threads, timeout, new IterableOf<>(tasks));
}
/**
* Ctor.
* @param threads The quantity of threads which will be used within the
* {@link ExecutorService}.
* @param timeout The maximum time to wait.
* @param tasks The tasks to be executed concurrently.
* @see Executors#newFixedThreadPool(int)
*/
public Threads(
final int threads,
final Duration timeout,
final Iterable extends Scalar extends T>> tasks
) {
this(
todo -> {
final ExecutorService executor = Executors.newFixedThreadPool(
threads
);
try {
return executor.invokeAll(
new ListOf<>(todo),
timeout.toNanos(), TimeUnit.NANOSECONDS
);
} finally {
executor.shutdown();
}
},
tasks
);
}
/**
* Ctor.
* @param fnc The function to map each task into {@link Future}.
* @param tasks The tasks to be executed concurrently.
*/
private Threads(
final Func>, Iterable>> fnc,
final Iterable extends Scalar extends T>> tasks
) {
super(
() -> new Mapped<>(
Future::get,
new UncheckedFunc<>(fnc).apply(new Mapped<>(CallableOf::new, tasks))
).iterator()
);
}
}
cactoos-0.54.0/src/main/java/org/cactoos/experimental/package-info.java 0000664 0000000 0000000 00000003113 14300500625 0025766 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.
*/
/**
* We are running experiments with the APIs provided in this package. We
* encourage you to try them out and submit any questions and feedback
* here.
*
* Be warned, however, that the stability of this package is
* not guaranteed.
* APIs may come, change, and go, between any release, for any reason
* whatsoever.
*
* @since 1.0.0
*/
package org.cactoos.experimental;
cactoos-0.54.0/src/main/java/org/cactoos/func/ 0000775 0000000 0000000 00000000000 14300500625 0021037 5 ustar 00root root 0000000 0000000 cactoos-0.54.0/src/main/java/org/cactoos/func/Async.java 0000664 0000000 0000000 00000005446 14300500625 0022770 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import org.cactoos.Func;
import org.cactoos.Proc;
/**
* Func that runs in the background.
*
* If you want your piece of code to be executed in the background,
* use {@link Async} as following:
*
* int length = new AsyncFunc(
* input -> input.length()
* ).apply("Hello, world!").get();
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @param Type of output
* @since 0.10
*/
public final class Async implements Func>, Proc {
/**
* The func.
*/
private final Func func;
/**
* The executor service.
*/
private final ExecutorService executor;
/**
* Ctor.
* @param fnc The func
*/
public Async(final Func fnc) {
this(fnc, Executors.defaultThreadFactory());
}
/**
* Ctor.
* @param fnc The func
* @param fct Factory
*/
public Async(final Func fnc, final ThreadFactory fct) {
this(fnc, Executors.newSingleThreadExecutor(fct));
}
/**
* Ctor.
* @param fnc The func
* @param exec Executor Service
*/
public Async(final Func fnc, final ExecutorService exec) {
this.func = fnc;
this.executor = exec;
}
@Override
public Future apply(final X input) {
return this.executor.submit(
() -> this.func.apply(input)
);
}
@Override
public void exec(final X input) {
this.apply(input);
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/BiFuncEnveloppe.java 0000664 0000000 0000000 00000003426 14300500625 0024733 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.BiFunc;
/**
* Enveloppe of {@link BiFunc}.
*
* @param Type of input
* @param Type of input
* @param Type of output
* @since 0.50
*/
public abstract class BiFuncEnveloppe implements BiFunc {
/**
* BiFunc to decorate.
*/
private final BiFunc origin;
/**
* Ctor.
* @param func The function
*/
public BiFuncEnveloppe(final BiFunc func) {
this.origin = func;
}
@Override
public final Z apply(final X first, final Y second) throws Exception {
return this.origin.apply(first, second);
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/BiFuncNoNulls.java 0000664 0000000 0000000 00000004626 14300500625 0024373 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.BiFunc;
/**
* BiFunc check for no nulls.
*
* @param Type of input
* @param Type of input
* @param Type of output
* @since 0.11
*/
public final class BiFuncNoNulls implements BiFunc {
/**
* The function.
*/
private final BiFunc origin;
/**
* Ctor.
* @param func The function
*/
public BiFuncNoNulls(final BiFunc func) {
this.origin = func;
}
@Override
public Z apply(final X first, final Y second) throws Exception {
if (this.origin == null) {
throw new IllegalArgumentException(
"NULL instead of a valid function"
);
}
if (first == null) {
throw new IllegalArgumentException(
"NULL instead of a valid first argument"
);
}
if (second == null) {
throw new IllegalArgumentException(
"NULL instead of a valid second argument"
);
}
final Z result = this.origin.apply(first, second);
if (result == null) {
throw new IllegalStateException(
"NULL instead of a valid result"
);
}
return result;
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/BiFuncOf.java 0000664 0000000 0000000 00000005177 14300500625 0023347 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.BiFunc;
import org.cactoos.BiProc;
import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.Scalar;
/**
* Represents many possible inputs as {@link BiFunc}.
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @param Type of input
* @param Type of output
* @since 0.20
*/
public final class BiFuncOf extends BiFuncEnveloppe {
/**
* Ctor.
* @param scalar The scalar
*/
public BiFuncOf(final Scalar scalar) {
this((first, second) -> scalar.value());
}
/**
* Ctor.
* @param fnc The func
*/
public BiFuncOf(final Func fnc) {
this((first, second) -> fnc.apply(first));
}
/**
* Ctor.
* @param proc The proc
* @param result Result to return
*/
public BiFuncOf(final Proc proc, final Z result) {
this(
(first, second) -> {
proc.exec(first);
return result;
}
);
}
/**
* Ctor.
* @param proc The proc
* @param result Result to return
*/
public BiFuncOf(final BiProc proc, final Z result) {
this(
(first, second) -> {
proc.exec(first, second);
return result;
}
);
}
/**
* Ctor.
* @param fnc The func
*/
public BiFuncOf(final BiFunc fnc) {
super(
fnc::apply
);
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/Chained.java 0000664 0000000 0000000 00000004636 14300500625 0023246 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import java.util.Collections;
import org.cactoos.Func;
/**
* Composed function.
*
* @param Type of input.
* @param Intermediate type.
* @param Type of output.
* @since 0.7
*/
public final class Chained implements Func {
/**
* Before function.
*/
private final Func before;
/**
* Functions.
*/
private final Iterable> funcs;
/**
* After function.
*/
private final Func after;
/**
* Ctor.
* @param bfr Before function
* @param list Functions
* @param atr After function
*/
public Chained(final Func bfr, final Iterable> list,
final Func atr) {
this.before = bfr;
this.funcs = list;
this.after = atr;
}
/**
* Ctor.
* @param bfr Before function
* @param atr After function
*/
public Chained(final Func bfr, final Func atr) {
this(bfr, Collections.emptyList(), atr);
}
@Override
public Z apply(final X input) throws Exception {
Y temp = this.before.apply(input);
for (final Func func : this.funcs) {
temp = func.apply(temp);
}
return this.after.apply(temp);
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/CheckedBiFunc.java 0000664 0000000 0000000 00000004331 14300500625 0024320 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.BiFunc;
import org.cactoos.Func;
import org.cactoos.scalar.Checked;
/**
* BiFunc that throws exception of specified type.
*
* @param Type of input
* @param Type of input
* @param Type of output
* @param Exception's type
* @since 0.32
*/
public final class CheckedBiFunc implements
BiFunc {
/**
* Original BiFunc.
*/
private final BiFunc origin;
/**
* Function that wraps exception of {@link #origin} to the required type.
*/
private final Func func;
/**
* Ctor.
* @param original Original BiFunc
* @param fnc Function that wraps exceptions.
*/
public CheckedBiFunc(final BiFunc original,
final Func fnc) {
this.origin = original;
this.func = fnc;
}
@Override
public Z apply(final X first, final Y second) throws E {
return new Checked<>(
() -> this.origin.apply(first, second),
this.func
).value();
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/CheckedFunc.java 0000664 0000000 0000000 00000004156 14300500625 0024052 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.Func;
import org.cactoos.scalar.Checked;
/**
* Func that throws exception of specified type.
*
* @param Type of input
* @param Type of output
* @param Exception's type
* @since 0.32
*/
public final class CheckedFunc implements
Func {
/**
* Original func.
*/
private final Func origin;
/**
* Function that wraps exception of {@link #origin} to the required type.
*/
private final Func func;
/**
* Ctor.
* @param original Original func
* @param fnc Function that wraps exceptions.
*/
public CheckedFunc(final Func original,
final Func fnc) {
this.origin = original;
this.func = fnc;
}
@Override
public Y apply(final X input) throws E {
return new Checked<>(
() -> this.origin.apply(input),
this.func
).value();
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/Flattened.java 0000664 0000000 0000000 00000003152 14300500625 0023611 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.Func;
import org.cactoos.Scalar;
/**
* {@link Func} from {@link Func} of {@link Scalar}.
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @param Type of output
* @since 0.48
*/
public final class Flattened extends FuncEnvelope {
/**
* Ctor.
* @param sclr The func
*/
public Flattened(final Func> sclr) {
super(x -> sclr.apply(x).value());
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/FuncEnvelope.java 0000664 0000000 0000000 00000003351 14300500625 0024275 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.Func;
/**
* Envelope for Func.
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @param Type of output
* @since 0.41
*/
public abstract class FuncEnvelope implements Func {
/**
* The delegate function.
*/
private final Func func;
/**
* Ctor.
* @param func The function
*/
public FuncEnvelope(final Func func) {
this.func = func;
}
@Override
public final Y apply(final X input) throws Exception {
return this.func.apply(input);
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/FuncNoNulls.java 0000664 0000000 0000000 00000004206 14300500625 0024112 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import java.io.IOException;
import org.cactoos.Func;
/**
* Func check for no nulls.
*
* @param Type of input
* @param Type of output
* @since 0.10
*/
public final class FuncNoNulls implements Func {
/**
* The function.
*/
private final Func func;
/**
* Ctor.
* @param fnc The function
*/
public FuncNoNulls(final Func fnc) {
this.func = fnc;
}
@Override
public Y apply(final X input) throws Exception {
if (this.func == null) {
throw new IllegalArgumentException(
"NULL instead of a valid function"
);
}
if (input == null) {
throw new IllegalArgumentException(
"NULL instead of a valid input"
);
}
final Y result = this.func.apply(input);
if (result == null) {
throw new IOException("NULL instead of a valid result");
}
return result;
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/FuncOf.java 0000664 0000000 0000000 00000004373 14300500625 0023071 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.Scalar;
/**
* Represents many possible inputs as {@link Func}.
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @param Type of output
* @since 0.12
*/
public final class FuncOf implements Func {
/**
* The func.
*/
private final Func super X, ? extends Y> func;
/**
* Ctor.
* @param proc The proc
* @param result Result to return
*/
public FuncOf(final Proc super X> proc, final Y result) {
this(
input -> {
proc.exec(input);
return result;
}
);
}
/**
* Ctor.
* @param scalar Origin scalar
*/
public FuncOf(final Scalar extends Y> scalar) {
this(input -> scalar.value());
}
/**
* Ctor.
* @param fnc Func
*/
public FuncOf(final Func super X, ? extends Y> fnc) {
this.func = fnc;
}
@Override
public Y apply(final X input) throws Exception {
return this.func.apply(input);
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/FuncWithFallback.java 0000664 0000000 0000000 00000010662 14300500625 0025056 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import java.sql.SQLException;
import java.sql.SQLRecoverableException;
import org.cactoos.Fallback;
import org.cactoos.Func;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.InheritanceLevel;
import org.cactoos.scalar.ScalarWithFallback;
/**
* Func with fallbacks that enable it to recover from errors.
*
* You may register several fallbacks, each for any type of exception
* whatsoever. If the decorated {@link Func} throws an exception that has
* an IS-A relationship with a registered fallback's exception, then that
* fallback's alternative {@link Func} will be used to provide a result.
*
*
Example scenario: you need to fetch product data
* from a database which may potentially not be available (SQLException).
* As a fallback, you then fetch the data from a local cache that is
* guaranteed not to fail. This is a sketch of what this code may look like:
*
*
* {@code
* final Product product = new FuncWithFallback<>(
* id -> new SqlProduct().apply(id),
* new Fallback.From<>(
* SQLException.class,
* id -> new CachedProduct().apply(id)
* )
* ).apply(id);
* }
*
*
* If you register several fallback plans for exception types belonging to
* the same hierarchy, then the fallback plan whose exception type has the
* closest {@link InheritanceLevel} to the exception thrown will be used.
*
*
Example scenario: sometimes {@code SqlProduct} from
* above will throw {@link SQLRecoverableException} (a sub class of
* {@link SQLException}). In such cases you may want to simply retry the same
* {@link Func}:
*
*
* {@code
* final Product product = new FuncWithFallback<>(
* id -> new SqlProduct().apply(id),
* new IterableOf<>(
* new Fallback.From<>(
* SQLException.class,
* id -> new CachedProduct().apply(id)
* ),
* new Fallback.From<>(
* SQLRecoverableException.class,
* id -> new SqlProduct().apply(id) // run it again
* )
* )
* ).apply(id);
* }
*
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @param Type of output
* @see ScalarWithFallback
* @since 0.2
*/
public final class FuncWithFallback implements Func {
/**
* The func.
*/
private final Func super X, ? extends Y> func;
/**
* The fallbacks.
*/
private final Iterable extends Fallback extends Y>> fallbacks;
/**
* Ctor.
* @param fnc The func
* @param fbks The fallbacks
*/
@SafeVarargs
public FuncWithFallback(final Func super X, ? extends Y> fnc,
final Fallback extends Y>... fbks) {
this(fnc, new IterableOf<>(fbks));
}
/**
* Ctor.
* @param fnc The func
* @param fbks The fallbacks
*/
public FuncWithFallback(final Func super X, ? extends Y> fnc,
final Iterable extends Fallback extends Y>> fbks) {
this.func = fnc;
this.fallbacks = fbks;
}
@Override
public Y apply(final X input) throws Exception {
return new ScalarWithFallback<>(
() -> this.func.apply(input),
this.fallbacks
).value();
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/IoCheckedBiFunc.java 0000664 0000000 0000000 00000003751 14300500625 0024615 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import java.io.IOException;
import org.cactoos.BiFunc;
import org.cactoos.scalar.IoChecked;
/**
* Func that doesn't throw checked {@link Exception}, but throws
* {@link IOException} instead.
*
* There is no thread-safety guarantee.
* @param Type of input
* @param Type of input
* @param Type of output
* @since 0.13
*/
public final class IoCheckedBiFunc implements BiFunc {
/**
* Original func.
*/
private final BiFunc func;
/**
* Ctor.
* @param fnc Encapsulated func
*/
public IoCheckedBiFunc(final BiFunc fnc) {
this.func = fnc;
}
@Override
public Z apply(final X first, final Y second) throws IOException {
return new IoChecked<>(
() -> this.func.apply(first, second)
).value();
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/IoCheckedFunc.java 0000664 0000000 0000000 00000003611 14300500625 0024335 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import java.io.IOException;
import org.cactoos.Func;
import org.cactoos.scalar.IoChecked;
/**
* Func that doesn't throw checked {@link Exception}, but throws
* {@link IOException} instead.
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @param Type of output
* @since 0.4
*/
public final class IoCheckedFunc implements Func {
/**
* Original func.
*/
private final Func func;
/**
* Ctor.
* @param fnc Encapsulated func
*/
public IoCheckedFunc(final Func fnc) {
this.func = fnc;
}
@Override
public Y apply(final X input) throws IOException {
return new IoChecked<>(() -> this.func.apply(input)).value();
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/Repeated.java 0000664 0000000 0000000 00000004464 14300500625 0023443 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.Func;
/**
* Func that repeats its calculation a few times before
* returning the last result.
*
* @param Type of input
* @param Type of output
* @since 0.6
*/
public final class Repeated implements Func {
/**
* Original func.
*/
private final Func super X, ? extends Y> func;
/**
* How many times to run.
*/
private final int times;
/**
* Ctor.
*
* If {@code max} is equal or less than zero {@link #apply(Object)}
* will return an exception.
*
* @param fnc Func original
* @param max How many times
*/
public Repeated(final Func super X, ? extends Y> fnc, final int max) {
this.func = fnc;
this.times = max;
}
@Override
public Y apply(final X input) throws Exception {
if (this.times <= 0) {
throw new IllegalArgumentException(
"The number of repetitions must be at least 1"
);
}
Y result = null;
for (int idx = 0; idx < this.times; ++idx) {
result = this.func.apply(input);
}
return result;
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/Retry.java 0000664 0000000 0000000 00000010023 14300500625 0023003 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import java.time.Duration;
import org.cactoos.Func;
/**
* Func that will try a few times before throwing an exception.
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @param Type of output
* @since 0.8
*/
public final class Retry implements Func {
/**
* Original func.
*/
private final Func func;
/**
* Exit condition.
*/
private final Func exit;
/**
* Wait between executions.
*/
private final Duration wait;
/**
* Ctor.
* @param fnc Func original
*/
public Retry(final Func fnc) {
this(fnc, 3);
}
/**
* Ctor.
* @param fnc Func original
* @param attempts Maximum number of attempts
*/
public Retry(final Func fnc, final int attempts) {
this(fnc, attempts, Duration.ZERO);
}
/**
* Ctor.
*
* @param fnc Func original
* @param attempts Maximum number of attempts
* @param wait The executions of the function
*/
public Retry(final Func fnc, final int attempts,
final Duration wait) {
this(fnc, attempt -> attempt >= attempts, wait);
}
/**
* Ctor.
*
* @param fnc Func original
* @param ext Exit condition, returns TRUE if there is no more reason to try
*/
public Retry(final Func fnc, final Func ext) {
this(fnc, ext, Duration.ZERO);
}
/**
* Ctor.
*
* @param fnc Func original
* @param ext Exit condition, returns TRUE if there is no more reason to try
* @param wait The executions of the function
*/
public Retry(final Func fnc, final Func ext,
final Duration wait) {
this.func = fnc;
this.exit = ext;
this.wait = wait;
}
@Override
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public Y apply(final X input) throws Exception {
int attempt = 0;
Exception error = new IllegalArgumentException(
"An immediate exit, didn't have a chance to try at least once"
);
while (!this.exit.apply(attempt)) {
try {
return this.func.apply(input);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
error = ex;
break;
// @checkstyle IllegalCatchCheck (1 line)
} catch (final Exception ex) {
error = ex;
}
if (!this.wait.isZero() && !this.wait.isNegative()) {
try {
Thread.sleep(this.wait.toMillis());
} catch (final InterruptedException ex) {
error = ex;
break;
}
}
++attempt;
}
throw error;
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/SolidBiFunc.java 0000664 0000000 0000000 00000003573 14300500625 0024053 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.BiFunc;
/**
* BiFunc that is thread-safe and sticky.
*
* Objects of this class are thread safe.
*
* @param Type of first input
* @param Type of second input
* @param Type of output
* @since 0.24
*/
public final class SolidBiFunc extends BiFuncEnveloppe {
/**
* Ctor.
* @param fnc Func original
*/
public SolidBiFunc(final BiFunc fnc) {
this(fnc, Integer.MAX_VALUE);
}
/**
* Ctor.
* @param fnc Func original
* @param max Max buffer length
* @since 0.26
*/
public SolidBiFunc(final BiFunc fnc, final int max) {
super(new SyncBiFunc<>(new StickyBiFunc<>(fnc, max)));
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/SolidFunc.java 0000664 0000000 0000000 00000003774 14300500625 0023603 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.Func;
/**
* Func that is thread-safe and sticky.
*
* Objects of this class are thread safe.
*
* @param Type of input
* @param Type of output
* @since 0.24
*/
public final class SolidFunc implements Func {
/**
* Original func.
*/
private final Func func;
/**
* Ctor.
* @param fnc Original function
*/
public SolidFunc(final Func fnc) {
this(fnc, Integer.MAX_VALUE);
}
/**
* Ctor.
* @param fnc Original function
* @param max Max caching buffer length
* @since 0.26
*/
public SolidFunc(final Func fnc, final int max) {
this.func = new SyncFunc<>(new StickyFunc<>(fnc, max));
}
@Override
public Y apply(final X input) throws Exception {
return this.func.apply(input);
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/StickyBiFunc.java 0000664 0000000 0000000 00000005712 14300500625 0024244 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import java.util.LinkedHashMap;
import java.util.Map;
import org.cactoos.BiFunc;
import org.cactoos.map.MapEntry;
import org.cactoos.scalar.Sticky;
/**
* Func that accepts two arguments and caches previously calculated values
* and doesn't recalculate again.
*
* Pay attention that this class is not thread-safe. It is highly
* recommended to always decorate it with {@link SyncBiFunc}.
*
* This {@link BiFunc} decorator technically is an in-memory
* cache.
*
* There is no thread-safety guarantee.
* @param Type of input
* @param Type of input
* @param Type of output
* @see Sticky
* @since 0.13
*/
public final class StickyBiFunc implements BiFunc {
/**
* Original func.
*/
private final BiFunc func;
/**
* Cache.
*/
private final Map, Z> cache;
/**
* Maximum cache size.
*/
private final int size;
/**
* Ctor.
* @param fnc Func original
*/
public StickyBiFunc(final BiFunc fnc) {
this(fnc, Integer.MAX_VALUE);
}
/**
* Ctor.
* @param fnc Func original
* @param max Maximum buffer size
* @since 0.26
*/
public StickyBiFunc(final BiFunc fnc, final int max) {
this.func = fnc;
this.cache = new LinkedHashMap<>(0);
this.size = max;
}
@Override
public Z apply(final X first, final Y second) throws Exception {
final Map.Entry key = new MapEntry<>(first, second);
while (this.cache.size() > this.size) {
this.cache.remove(this.cache.keySet().iterator().next());
}
if (!this.cache.containsKey(key)) {
this.cache.put(key, this.func.apply(first, second));
}
return this.cache.get(key);
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/StickyFunc.java 0000664 0000000 0000000 00000004576 14300500625 0024000 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.BiFunc;
import org.cactoos.Func;
import org.cactoos.scalar.Sticky;
/**
* Func that caches previously calculated values and doesn't
* recalculate again.
*
* This {@link Func} decorator technically is an in-memory
* cache.
*
* Pay attention that this class is not thread-safe. It is highly
* recommended to always decorate it with {@link SyncFunc}.
*
* There is no thread-safety guarantee.
*
* @param Type of input
* @param Type of output
* @see Sticky
* @since 0.1
*/
public final class StickyFunc implements Func {
/**
* Sticky bi-func.
*/
private final BiFunc func;
/**
* Ctor.
* @param fnc Func original
*/
public StickyFunc(final Func fnc) {
this(fnc, Integer.MAX_VALUE);
}
/**
* Ctor.
* @param fnc Func original
* @param max Maximum cache size
* @since 0.26
*/
public StickyFunc(final Func fnc, final int max) {
this.func = new StickyBiFunc<>(
(first, second) -> fnc.apply(first),
max
);
}
@Override
public Y apply(final X input) throws Exception {
return this.func.apply(input, true);
}
}
cactoos-0.54.0/src/main/java/org/cactoos/func/SyncBiFunc.java 0000664 0000000 0000000 00000004177 14300500625 0023716 0 ustar 00root root 0000000 0000000 /*
* The MIT License (MIT)
*
* Copyright (c) 2017-2022 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.func;
import org.cactoos.BiFunc;
/**
* BiFunc that is thread-safe.
*
* Objects of this class are thread safe.
*
* @param Type of first input
* @param Type of second input
* @param Type of output
* @since 0.18
*/
public final class SyncBiFunc implements BiFunc