Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 9, 2018
2 parents 36c5ee3 + a60101c commit 0314515
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/main/java/org/cactoos/text/TextEnvelope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 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.Scalar;
import org.cactoos.Text;
import org.cactoos.scalar.IoCheckedScalar;
import org.cactoos.scalar.UncheckedScalar;

/**
* Text envelope that provides {@link #equals(Object)} and {@link #hashCode()}
* methods.
* @author Paulo Lobo (pauloeduardolobo@gmail.com)
* @version $Id$
* @since 0.32
* @todo #788:30min Refactor all classes in text package to extend from
* TextEnvelope, allowing direct comparison with string objects using hashCode
* and equals methods.
* @checkstyle AbstractClassNameCheck (500 lines)
*/
@SuppressWarnings("PMD.AbstractNaming")
public abstract class TextEnvelope implements Text {

/**
* String value of the envelope.
*/
private final IoCheckedScalar<String> origin;

/**
* Ctor.
* @param text Text representing the text value.
*/
public TextEnvelope(final Text text) {
this(new IoCheckedScalar<>(() -> text.asString()));
}

/**
* Ctor.
* @param scalar Scalar representing the text value.
*/
public TextEnvelope(final Scalar<String> scalar) {
this.origin = new IoCheckedScalar<>(scalar);
}

@Override
public final String asString() throws IOException {
return this.origin.value();
}

@Override
public final int hashCode() {
return new UncheckedScalar<>(this.origin).value().hashCode();
}

@Override
public final boolean equals(final Object obj) {
return new UncheckedScalar<>(this.origin).value().equals(obj);
}
}
115 changes: 115 additions & 0 deletions src/test/java/org/cactoos/text/TextEnvelopeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 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.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.cactoos.Scalar;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.Test;

/**
* Tests for {@link TextEnvelope}.
* @author Paulo Lobo (pauloeduardolobo@gmail.com)
* @version $Id$
* @since 0.32
*/
public final class TextEnvelopeTest {
/**
* Test for {@link TextEnvelope#asString()} method. Must assert that
* the envelope asString value is equal to its string value.
* @throws Exception Throws from asString.
*/
@Test
public void testAsString() throws Exception {
final String text = "asString";
MatcherAssert.assertThat(
"Envelope value does not match String value",
new TextEnvelopeDummy(text).asString(),
new IsEqual<>(text)
);
}
/**
* Test for {@link TextEnvelope#equals(Object)} method. Must assert
* that the envelope value is equal to its string value.
*/
@Test
public void testEquals() {
final String text = "equals";
MatcherAssert.assertThat(
"Envelope value does not match its represented String value",
new TextEnvelopeDummy(text),
new IsEqual<>(text)
);
}
/**
* Test for {@link TextEnvelope#hashCode()} method. Must assert that
* the {@link TextEnvelope} hashCode is equals to the hashCode of
* the String it represents.
*/
@Test
public void testHashCode() {
final String hash = "hashCode";
MatcherAssert.assertThat(
"Enveloped hashCode does not match its represented String hashcode",
new TextEnvelopeDummy(hash).hashCode(),
new IsEqual<>(hash.hashCode())
);
}

/**
* Dummy class for {@link TextEnvelope} testing.
*/
private final class TextEnvelopeDummy extends TextEnvelope {

/**
* Ctor.
*
* @param scalar Text to be enveloped.
*/
TextEnvelopeDummy(final Scalar<String> scalar) {
super(scalar);
}
/**
* Ctor.
*
* @param input The String
*/
TextEnvelopeDummy(final String input) {
this(input, StandardCharsets.UTF_8);
}

/**
* Ctor.
*
* @param input The String
* @param cset The Charset
*/
TextEnvelopeDummy(final String input,
final Charset cset) {
this(() -> new String(input.getBytes(cset), cset));
}
}
}

0 comments on commit 0314515

Please sign in to comment.