Skip to content

Commit

Permalink
Merge branch 'master' into ListOfIterableNotUseSize
Browse files Browse the repository at this point in the history
  • Loading branch information
Timmeey committed Jun 11, 2017
2 parents da64c70 + 13dd4ef commit f0eb57b
Show file tree
Hide file tree
Showing 17 changed files with 545 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,10 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static
- [Andriy Kryvtsun](https://github.com/englishman)
- [Vseslav Sekorin](https://github.com/VsSekorin)
- [Andrey Valyaev](https://github.com/DronMDF)
- [Dušan Rychnovský](https://github.com/dusan-rychnovsky) [Blog](http://blog.dusanrychnovsky.cz/)
- [Tim Hinkes](https://github.com/timmeey) [Blog](https://blog.timmeey.de)


## License (MIT)

Copyright (c) 2017 Yegor Bugayenko
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/cactoos/func/IoCheckedFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,18 @@ public IoCheckedFunc(final Func<X, Y> fnc) {
}

@Override
@SuppressWarnings("PMD.AvoidCatchingGenericException")
@SuppressWarnings
(
{
"PMD.AvoidCatchingGenericException",
"PMD.AvoidRethrowingException"
}
)
public Y apply(final X input) throws IOException {
try {
return this.func.apply(input);
} catch (final IOException ex) {
throw ex;
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new IOException(ex);
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/org/cactoos/text/StringAsUrl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* 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 java.io.UncheckedIOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.cactoos.Scalar;
import org.cactoos.Text;

/**
* String as URL.
*
* <p>There is no thread-safety guarantee.
*
* @author Fabricio Cabral (fabriciofx@gmail.com)
* @version $Id$
* @since 0.4
*/
public final class StringAsUrl implements Scalar<String> {

/**
* The source.
*/
private final Text source;

/**
* The encoding.
*/
private final Charset encoding;

/**
* Ctor.
* @param url The URL as String
*/
public StringAsUrl(final String url) {
this(url, StandardCharsets.UTF_8);
}

/**
* Ctor.
* @param url The URL as Text
*/
public StringAsUrl(final Text url) {
this(url, StandardCharsets.UTF_8);
}

/**
* Ctor.
* @param url The URL as String
* @param encoding The encoding
*/
public StringAsUrl(final String url, final Charset encoding) {
this(new StringAsText(url), encoding);
}

/**
* Ctor.
* @param url The URL as Text
* @param encoding The encoding
*/
public StringAsUrl(final Text url, final Charset encoding) {
this.source = url;
this.encoding = encoding;
}

@Override
public String asValue() {
try {
return URLEncoder.encode(
this.source.asString(),
this.encoding.name()
);
} catch (final IOException ex) {
throw new UncheckedIOException(ex);
}
}

}
102 changes: 102 additions & 0 deletions src/main/java/org/cactoos/text/UrlAsString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* 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 java.io.UncheckedIOException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.cactoos.Scalar;
import org.cactoos.Text;

/**
* URL as String.
*
* <p>There is no thread-safety guarantee.
*
* @author Fabricio Cabral (fabriciofx@gmail.com)
* @version $Id$
* @since 0.4
*/
public final class UrlAsString implements Scalar<String> {

/**
* The source.
*/
private final Text source;

/**
* The encoding.
*/
private final Charset encoding;

/**
* Ctor.
* @param url The URL as String
*/
public UrlAsString(final String url) {
this(url, StandardCharsets.UTF_8);
}

/**
* Ctor.
* @param url The URL as Text
*/
public UrlAsString(final Text url) {
this(url, StandardCharsets.UTF_8);
}

/**
* Ctor.
* @param url The URL as String
* @param encoding The encoding
*/
public UrlAsString(final String url, final Charset encoding) {
this(new StringAsText(url), encoding);
}

/**
* Ctor.
* @param url The URL as Text
* @param encoding The encoding
*/
public UrlAsString(final Text url, final Charset encoding) {
this.source = url;
this.encoding = encoding;
}

@Override
public String asValue() {
try {
return URLDecoder.decode(
this.source.asString(),
this.encoding.name()
);
} catch (final IOException ex) {
throw new UncheckedIOException(ex);
}
}

}
58 changes: 58 additions & 0 deletions src/test/java/org/cactoos/func/IoCheckedFuncTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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.func;

import java.io.IOException;
import org.cactoos.Func;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Test case for {@link IoCheckedFunc}.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.4
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class IoCheckedFuncTest {

@Test
public void rethrowsCheckedToUncheckedException() {
final IOException exception = new IOException("intended");
try {
new IoCheckedFunc<>(
(Func<Integer, String>) i -> {
throw exception;
}
).apply(1);
} catch (final IOException ex) {
MatcherAssert.assertThat(
ex, Matchers.is(exception)
);
}
}

}
58 changes: 58 additions & 0 deletions src/test/java/org/cactoos/func/IoCheckedProcTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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.func;

import java.io.IOException;
import org.cactoos.Proc;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Test case for {@link IoCheckedProc}.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.4
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class IoCheckedProcTest {

@Test
public void rethrowsCheckedToUncheckedException() {
final IOException exception = new IOException("intended");
try {
new IoCheckedProc<>(
(Proc<Integer>) i -> {
throw exception;
}
).exec(1);
} catch (final IOException ex) {
MatcherAssert.assertThat(
ex, Matchers.is(exception)
);
}
}

}
Loading

0 comments on commit f0eb57b

Please sign in to comment.