Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Capitalized #1465

Merged
merged 7 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/main/java/org/cactoos/text/Capitalized.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2020 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 org.cactoos.Text;

/**
* Text in capitalized case, changed the first character to title case, no other
* characters are changed.
*
* @since 0.46
*/
public class Capitalized extends TextEnvelope {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kokodyn May be we should name it Title instead?
Should it make all non-first characters lower-cased?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will create ticket for "Capitalized" issue and we can discuss further,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created issue for this:
#1466

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kokodyn The class should be final

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


/**
* Ctor.
*
* @param text The text
*/
public Capitalized(final String text) {
this(new TextOf(text));
}

/**
* Ctor.
*
* @param text The text
*/
public Capitalized(final Text text) {
super(
new TextOf(
() -> {
String capitalized = text.asString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kokodyn Let's remove mutable variable from here. I guess Ternary can be used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done but I am not fully convinced to using Ternary. It seems be more complicated than previous version.

if (!capitalized.isEmpty()) {
final Text first = new Sub(capitalized, 0, 1);
final Text upper = new Upper(first);
if (!upper.equals(first)) {
capitalized = new StringBuilder().append(upper).append(new Sub(text, 1))
.toString();
}
}
return capitalized;
}));
}
}
93 changes: 93 additions & 0 deletions src/test/java/org/cactoos/text/CapitalizedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2020 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 org.junit.Test;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kokodyn Please use Junit 5 and make class final, but not public

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope I did it in proper way, I need to update path to @test annotation to allow Eclipse to handle it.

import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.TextIs;

/**
* Test case for {@link Capitalized}.
* @since 0.46
*/
public class CapitalizedTest {
/**
* Text starting with upper case character.
*/
private static final String UPPER_CASE = "Abc";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kokodyn I believe this constant is not necessary. If it causes Qulice complain, it would be better to suppress that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


@Test
public void capitalizeEmptyText() {
new Assertion<>(
"Can't capitalize an empty text",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kokodyn I suggest must instead of cannot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use "Can't" because as I see it it used commonly in other tests in similar context.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kokodyn please let's use must as it is the default we use in cactoos (there are still examples where can't is used but this should be changed bit by bit)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

new Capitalized(new TextOf("")),
new TextIs("")
).affirm();
}

@Test
public void capitalizeSingleLowerCaseText() {
new Assertion<>(
"Can't capitalize single lower case text",
new Capitalized(new TextOf("f")),
new TextIs("F")
).affirm();
}

@Test
public void capitalizeSingleUpperCaseText() {
new Assertion<>(
"Can't capitalize single upper case text",
new Capitalized(new TextOf("F")),
new TextIs("F")
).affirm();
}

@Test
public void capitalizeTextStartingWithUpperCaseCharacter() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kokodyn public is not necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

new Assertion<>(
"Can't capitalize text starting with upper case character",
new Capitalized(new TextOf(CapitalizedTest.UPPER_CASE)),
new TextIs(CapitalizedTest.UPPER_CASE)
).affirm();
}

@Test
public void capitalizeTextStartingWithLowerCaseCharacter() {
new Assertion<>(
"Can't capitalize text starting with lower case character",
new Capitalized(new TextOf("xyz")),
new TextIs("Xyz")
).affirm();
}

@Test
public void capitalizeString() {
new Assertion<>(
"Can't capitalize string",
new Capitalized("foo"),
new TextIs("Foo")
).affirm();
}
}