Skip to content

Commit

Permalink
Merge branch 'master' of github.com:yegor256/cactoos
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 4, 2018
2 parents 07826a5 + 08b0d32 commit 9435f29
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 226 deletions.
215 changes: 0 additions & 215 deletions src/main/java/org/cactoos/func/FuncWithFallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
*/
package org.cactoos.func;

import java.util.concurrent.Callable;
import org.cactoos.Func;
import org.cactoos.Proc;

/**
* Func with a fallback plan.
Expand Down Expand Up @@ -65,219 +63,6 @@ public FuncWithFallback(final Func<X, Y> fnc,
this(fnc, fbk, input -> input);
}

/**
* Ctor.
* @param proc The proc
* @param fbk The fallback
* @since 0.12
*/
public FuncWithFallback(final Proc<X> proc,
final Func<Throwable, Y> fbk) {
this(new FuncOf<>(proc), fbk);
}

/**
* Ctor.
* @param proc The proc
* @param fbk The fallback
* @since 0.12
*/
public FuncWithFallback(final Proc<X> proc,
final Proc<Throwable> fbk) {
this(new FuncOf<>(proc), new FuncOf<>(fbk));
}

/**
* Ctor.
* @param proc The proc
* @param fbk The fallback
* @since 0.12
*/
public FuncWithFallback(final Func<X, Y> proc,
final Proc<Throwable> fbk) {
this(proc, new FuncOf<>(fbk));
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Func<X, Y> fnc,
final Proc<Throwable> fbk, final Func<Y, Y> flw) {
this(fnc, new FuncOf<>(fbk), flw);
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Func<X, Y> fnc,
final Proc<Throwable> fbk, final Proc<Y> flw) {
this(fnc, new FuncOf<>(fbk), new FuncOf<>(flw));
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Func<X, Y> fnc,
final Func<Throwable, Y> fbk, final Proc<Y> flw) {
this(fnc, fbk, new FuncOf<>(flw));
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Proc<X> fnc,
final Func<Throwable, Y> fbk, final Func<Y, Y> flw) {
this(new FuncOf<>(fnc), fbk, flw);
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Proc<X> fnc,
final Proc<Throwable> fbk, final Func<Y, Y> flw) {
this(new FuncOf<>(fnc), fbk, flw);
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Proc<X> fnc,
final Proc<Throwable> fbk, final Proc<Y> flw) {
this(new FuncOf<>(fnc), fbk, flw);
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Proc<X> fnc,
final Func<Throwable, Y> fbk, final Proc<Y> flw) {
this(new FuncOf<>(fnc), fbk, flw);
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Callable<Y> fnc,
final Func<Throwable, Y> fbk, final Func<Y, Y> flw) {
this(new FuncOf<>(fnc), fbk, flw);
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Callable<Y> fnc,
final Proc<Throwable> fbk, final Func<Y, Y> flw) {
this(new FuncOf<>(fnc), fbk, flw);
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Callable<Y> fnc,
final Proc<Throwable> fbk, final Proc<Y> flw) {
this(new FuncOf<>(fnc), fbk, flw);
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Callable<Y> fnc,
final Func<Throwable, Y> fbk, final Proc<Y> flw) {
this(new FuncOf<>(fnc), fbk, flw);
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Runnable fnc,
final Func<Throwable, Y> fbk, final Proc<Y> flw) {
this(new FuncOf<>(fnc), fbk, flw);
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Runnable fnc,
final Func<Throwable, Y> fbk, final Func<Y, Y> flw) {
this(new FuncOf<>(fnc), fbk, flw);
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Runnable fnc,
final Proc<Throwable> fbk, final Func<Y, Y> flw) {
this(new FuncOf<>(fnc), fbk, flw);
}

/**
* Ctor.
* @param fnc The func
* @param fbk The fallback
* @param flw The follow up func
* @since 0.12
*/
public FuncWithFallback(final Runnable fnc,
final Proc<Throwable> fbk, final Proc<Y> flw) {
this(new FuncOf<>(fnc), fbk, flw);
}

/**
* Ctor.
* @param fnc The func
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/io/WriterTo.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public WriterTo(final File file) {

/**
* Ctor.
* @param stream The outpup
* @param stream The output
*/
public WriterTo(final OutputStream stream) {
this(new OutputTo(stream));
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/org/cactoos/text/HexOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.text;

import java.io.IOException;
import org.cactoos.Bytes;
import org.cactoos.Text;

/**
* Hexadecimal representation of Bytes.
*
* <p>There is no thread-safety guarantee.
*
* @author Fabricio Cabral (fabriciofx@gmail.com)
* @version $Id$
* @since 0.28
*/
@SuppressWarnings("PMD.ConstructorShouldDoInitialization")
public final class HexOf implements Text {

/**
* The hexadecimal chars.
*/
private static final char[] HEX_CHARS = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
};

/**
* The Bytes.
*/
private final Bytes bytes;

/**
* Ctor.
* @param source The bytes
*/
public HexOf(final Bytes source) {
this.bytes = source;
}

@Override
public String asString() throws IOException {
final byte[] bts = this.bytes.asBytes();
final char[] hex = new char[bts.length * 2];
int chr = -1;
for (int idx = 0; idx < bts.length; ++idx) {
// @checkstyle MagicNumber (3 line)
final int value = 0xff & bts[idx];
hex[++chr] = HexOf.HEX_CHARS[value >>> 4];
hex[++chr] = HexOf.HEX_CHARS[value & 0x0f];
}
return new String(hex);
}

@Override
public int compareTo(final Text text) {
return new UncheckedText(this).compareTo(text);
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/cactoos/collection/FilteredTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class FilteredTest {
public void behavesAsCollection() throws Exception {
MatcherAssert.assertThat(
"Can't behave as a collection",
new Filtered<>(i -> i < 2, 1, 2, 0, -1),
new Filtered<Integer>(i -> i < 2, 1, 2, 0, -1),
new BehavesAsCollection<>(-1)
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/org/cactoos/func/FuncWithFallbackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import java.io.IOException;
import org.cactoos.FuncApplies;
import org.cactoos.Proc;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
Expand Down Expand Up @@ -53,11 +52,11 @@ public void usesMainFunc() throws Exception {
}

@Test
public void usesCallback() throws Exception {
public void usesFallback() throws Exception {
MatcherAssert.assertThat(
"Can't use the callback in case of exception",
new FuncWithFallback<>(
(Proc<Integer>) input -> {
input -> {
throw new IOException("Failure");
},
ex -> "Never mind"
Expand Down
Loading

0 comments on commit 9435f29

Please sign in to comment.