53 Commits
Author SHA1 Message Date
Shekhar Gulati 13f0299f2e [Gradle Release Plugin] - pre tag commit: '0.4.0'. 2017-06-30 11:27:10 +05:30
Shekhar Gulati e7343fd155 Resolved #57
This bug is invalid
2017-06-30 11:22:57 +05:30
Shekhar Gulati dee9daee86 Resolved #61
This bug was not valid
2017-06-30 11:19:08 +05:30
Shekhar Gulati a25cba4c63 Resolved #79 2017-06-23 21:23:36 +05:30
Shekhar Gulati db5a8b5d65 Resolved #81 2017-06-23 21:04:12 +05:30
Shekhar Gulati 840198f97c Resolved #85 2017-06-17 05:21:28 +05:30
Shekhar Gulati e39978cb9c Resolved #86.
Also, rewrote underscored implementation to use regex
2017-06-17 05:11:51 +05:30
Shekhar Gulati 88d9664aea Resolved #87 2017-06-17 04:06:46 +05:30
Shekhar Gulati 5e5440119d #88 Added Errorprone to project 2017-06-17 03:50:21 +05:30
Shekhar Gulati f5d9a0b535 #88 Added Checkstyle to project 2017-06-17 03:36:42 +05:30
Shekhar Gulati 5eca5814de Resolved #83 2017-06-17 03:17:05 +05:30
Shekhar Gulati 9b4f0ac791 Code Refactoring
1. Moved all private methods to the end
2. Improved zip implementation so that we leverage Java 8 features
3. Using *Tests convention instead of Test for unit test cases
2017-06-17 03:01:11 +05:30
César Aguilera 90322a7339 Implementation of zip function #72 (#92)
* #72: First test passing

* #72: Second test passing

* #72: Third test passing

* #72: Added extra assertion

* #72: Fixed wrong test

* #72: Fixed wrong test

* #72: Added fourth test (passing)

* #72: fixed typo in test name

* #72: added fifth test

* #72: fixed test names

* #72: refactored for multiple inputs

* #72: added extra test cases and documentation

* #72: addressed @shekhargulati comments
2017-06-14 20:41:54 +05:30
Shekhar Gulati 375da9342d Resolved #84
Merge branch 'deeseja-master'
2017-05-20 23:27:22 +05:30
Shekhar Gulati 4369f904c9 #84 improved implementation of underscored function to incorporate whitespace, dashed strings. Also, cleaned up the code. 2017-05-20 23:24:21 +05:30
Andy Deese d8962a9c7e Merge branch 'master' of https://github.com/deeseja/strman-java 2017-05-20 12:16:12 -04:00
Andy Deese a4d2c2aa41 Added underscored method with tests and documentation 2017-05-20 12:15:20 -04:00
Andy Deese 6bbcb53138 Added underscored method with tests and documentation 2017-05-20 12:01:13 -04:00
Andy Deese d04e679b07 Adding isBlank method (#89)
* added isBlank method, along with junit test and readme update

* removing local files

* adding gitignore with Eclipse specific files ignored

* removed unused imports from Strman.java and split tests out to match current test convention
2017-05-20 06:57:21 +05:30
Andy Deese 04b9f8a7b4 removed unused imports from Strman.java and split tests out to match current test convention 2017-05-19 09:49:13 -04:00
Andy Deese f1e8e83cc0 adding gitignore with Eclipse specific files ignored 2017-05-19 09:28:47 -04:00
Andy Deese fad14c5f40 removing local files 2017-05-19 09:24:56 -04:00
Andy Deese 1877634234 added isBlank method, along with junit test and readme update 2017-05-19 09:18:15 -04:00
Shekhar Gulati cf1d373442 Updated README.md 2017-05-12 16:11:52 +05:30
Shekhar Gulati 7bcc0df6a5 Updated documentation to version 0.3.0 2017-05-12 16:01:33 +05:30
Shekhar Gulati d6dbe785cb Added changelog.md file 2017-05-12 15:43:26 +05:30
Shekhar Gulati 4f0cfaf75b Updated version to 0.3.0 in the README.md file 2017-05-12 15:35:31 +05:30
Shekhar Gulati 33b26a0244 [Gradle Release Plugin] - new version commit: ' 0.4.0-SNAPSHOT'. 2017-05-12 15:26:19 +05:30
Shekhar Gulati 5487cda192 [Gradle Release Plugin] - pre tag commit: '0.3.0'. 2017-05-12 15:25:46 +05:30
Shekhar Gulati 4d94035f17 Formatted code to move private methods down 2017-05-12 15:22:41 +05:30
César Aguilera 84b3303718 Feature required in #73 (#77) 2017-05-09 18:16:11 +05:30
wli75 605eb4f2a5 fixed containsAll (#76) 2017-03-07 20:09:18 +05:30
Shekhar Gulati 61ca9fc984 Resolved #66 2016-08-24 15:32:45 +05:30
Shekhar Gulati bacbe46529 Resolved #65 2016-08-24 15:02:00 +05:30
Shekhar Gulati 9b60adf732 Resolved #68 2016-08-24 14:26:16 +05:30
Shekhar Gulati 2ae1c02b5d Resolved #69 2016-08-24 12:56:18 +05:30
Shekhar Gulati f635543679 #71 updated documentation 2016-08-24 12:16:19 +05:30
Shekhar Gulati 21f8bb9f8e Resolved#71 2016-08-24 12:11:04 +05:30
Shekhar Gulati f282dddca3 Resolved #54 and Resolved #59 2016-07-27 23:19:41 +05:30
Shekhar Gulati 3594042ab5 Resolved #53 2016-07-25 15:29:43 +05:30
Marcin Zajączkowski 6bbe0a9167 Simplify Maven and Gradle configuration (#70) 2016-07-19 20:58:57 +05:30
Shekhar Gulati ec7cb02800 cleanup and added tests 2016-07-19 18:13:13 +05:30
Shekhar Gulati acdb15d6fe Updated README.md with latest version 2016-06-26 22:49:37 +05:30
Shekhar Gulati 9c2317434e [Gradle Release Plugin] - new version commit: '0.3.0-SNAPSHOT'. 2016-06-26 22:41:33 +05:30
Shekhar Gulati 555f50b2b1 [Gradle Release Plugin] - pre tag commit: '0.2.0'. 2016-06-26 22:41:08 +05:30
Shekhar Gulati bf081e6b48 Resolved #51. Incorporated suggestions of johnsdouglass 2016-06-26 22:36:05 +05:30
Shekhar Gulati ab6c36c078 Resolved #50 2016-06-26 22:29:03 +05:30
Shekhar Gulati dfc05347bf Resolved #49 2016-06-26 22:10:03 +05:30
Shekhar Gulati e251cc07d8 fixed README.md 2016-05-25 16:50:33 +05:30
Shekhar Gulati 68ff5f4ebf Merge pull request #48 from jvitkauskas/fix-readme-b64dec
Fix base64Decode example
2016-05-23 00:14:42 +05:30
Julius Vitkauskas 6a4866fed8 Fix base64Decode example 2016-05-22 21:34:31 +03:00
Shekhar Gulati 3c7ec0d113 removed version information from the build.gradle 2016-05-22 21:22:07 +05:30
Shekhar Gulati 43b6b3ad17 [Gradle Release Plugin] - new version commit: '0.2.0-SNAPSHOT'. 2016-05-22 21:05:23 +05:30
11 changed files with 1078 additions and 93 deletions
+5
View File
@@ -71,3 +71,8 @@ hs_err_pid*
!gradle/wrapper/gradle-wrapper.jar
/bin/
.classpath
.project
.settings
+119 -37
View File
@@ -1,7 +1,7 @@
strman-java [![Build Status](https://travis-ci.org/shekhargulati/strman-java.svg?branch=master)](https://travis-ci.org/shekhargulati/strman-java) [![codecov.io](https://codecov.io/github/shekhargulati/strman-java/coverage.svg?branch=master)](https://codecov.io/github/shekhargulati/strman-java?branch=master) [![License](https://img.shields.io/:license-mit-blue.svg)](./LICENSE.txt)
------
A Java 8 library for working with String. It is inspired by [dleitee/strman](https://github.com/dleitee/strman).
A Java 8 library for working with String. You can refer to Javadocs online [http://shekhargulati.github.io/strman-java/](http://shekhargulati.github.io/strman-java/).
Getting Started
--------
@@ -15,8 +15,7 @@ For Apache Maven users, please add following to your pom.xml.
<dependency>
<groupId>com.shekhargulati</groupId>
<artifactId>strman</artifactId>
<version>0.1.0</version>
<type>jar</type>
<version>0.3.0</version>
</dependency>
</dependencies>
```
@@ -24,11 +23,11 @@ For Apache Maven users, please add following to your pom.xml.
Gradle users can add following to their build.gradle file.
```
compile(group: 'com.shekhargulati', name: 'strman', version: '0.1.0', ext: 'jar'){
transitive=true
}
compile(group: 'com.shekhargulati', name: 'strman', version: '0.3.0')
```
To learn what we added in the latest version please refer to `./changelog.md`.
## Available Functions
These are the available functions in current version of library:
@@ -38,7 +37,7 @@ These are the available functions in current version of library:
Appends Strings to value
```java
import static strman.append
import static strman.Strman.append
append("f", "o", "o", "b", "a", "r")
// result => "foobar"
```
@@ -48,7 +47,7 @@ append("f", "o", "o", "b", "a", "r")
Append an array of String to value
```java
import static strman.appendArray
import static strman.Strman.appendArray
appendArray("f", new String[]{"o", "o", "b", "a", "r"}
// result => "foobar"
```
@@ -58,7 +57,7 @@ appendArray("f", new String[]{"o", "o", "b", "a", "r"}
Get the character at index. This method will take care of negative indexes.
```java
import static strman.at
import static strman.Strman.at
at("foobar", 0)
// result => Optional("f")
```
@@ -68,7 +67,7 @@ at("foobar", 0)
Returns an array with strings between start and end.
```java
import static strman.between
import static strman.Strman.between
between("[abc][def]", "[", "]")
// result => ["abc","def"]
```
@@ -78,17 +77,27 @@ between("[abc][def]", "[", "]")
Returns a String array consisting of the characters in the String.
```java
import static strman.chars
import static strman.Strman.chars
chars("title")
// result => ["t", "i", "t", "l", "e"]
```
## charsCount
Counts the number of occurrences of each character in the string
```java
import static strman.Strman.charsCount
charsCount("abca")
// result => Map(("a",2),("b",1),("c",1))
```
## collapseWhitespace
Replace consecutive whitespace characters with a single space.
```java
import static strman.collapseWhitespace
import static strman.Strman.collapseWhitespace
collapseWhitespace("foo bar")
// result => "foo bar"
```
@@ -98,7 +107,7 @@ collapseWhitespace("foo bar")
Verifies that the needle is contained in the value.
```java
import static strman.contains
import static strman.Strman.contains
contains("foo bar","foo")
// result => true
@@ -111,7 +120,7 @@ contains("foo bar","FOO", false) // turning off case sensitivity
Verifies that all needles are contained in value
```java
import static strman.containsAll
import static strman.Strman.containsAll
containsAll("foo bar", new String[]{"foo", "bar"})
// result => true
@@ -124,7 +133,7 @@ containsAll("foo bar", new String[]{"FOO", "bar"},false)
Verifies that one or more of needles are contained in value.
```java
import static strman.containsAny
import static strman.Strman.containsAny
containsAny("bar foo", new String[]{"FOO", "BAR", "Test"}, true)
// result => true
```
@@ -134,7 +143,7 @@ containsAny("bar foo", new String[]{"FOO", "BAR", "Test"}, true)
Count the number of times substr appears in value
```java
import static strman.countSubstr
import static strman.Strman.countSubstr
countSubstr("aaaAAAaaa", "aaa")
// result => 2
countSubstr("aaaAAAaaa", "aaa", false, false)
@@ -146,7 +155,7 @@ countSubstr("aaaAAAaaa", "aaa", false, false)
Test if value ends with search.
```java
import static strman.endsWith
import static strman.Strman.endsWith
endsWith("foo bar", "bar")
// result => true
endsWith("foo Bar", "BAR", false)
@@ -158,7 +167,7 @@ endsWith("foo Bar", "BAR", false)
Ensures that the value begins with prefix. If it doesn't exist, it's prepended.
```java
import static strman.ensureLeft
import static strman.Strman.ensureLeft
ensureLeft("foobar", "foo")
// result => "foobar"
ensureLeft("bar", "foo")
@@ -172,9 +181,9 @@ ensureLeft("foobar", "FOO", false)
Decodes data encoded with MIME base64
```java
import static strman.base64Decode
import static strman.Strman.base64Decode
base64Decode("c3RybWFu")
// result => "strma"
// result => "strman"
```
## base64Encode
@@ -182,7 +191,7 @@ base64Decode("c3RybWFu")
Encodes data with MIME base64.
```java
import static strman.base64Encode
import static strman.Strman.base64Encode
base64Encode("strman")
// result => "c3RybWFu"
```
@@ -192,7 +201,7 @@ base64Encode("strman")
Convert binary unicode (16 digits) string to string chars
```java
import static strman.binDecode
import static strman.Strman.binDecode
binDecode("0000000001000001")
// result => "A"
```
@@ -202,7 +211,7 @@ binDecode("0000000001000001")
Convert string chars to binary unicode (16 digits)
```java
import static strman.binEncode
import static strman.Strman.binEncode
binEncode("A")
// result => "0000000001000001"
```
@@ -212,7 +221,7 @@ binEncode("A")
Convert decimal unicode (5 digits) string to string chars
```java
import static strman.decDecode
import static strman.Strman.decDecode
decDecode("00065")
// result => "A"
```
@@ -222,7 +231,7 @@ decDecode("00065")
Convert string chars to decimal unicode (5 digits)
```java
import static strman.decEncode
import static strman.Strman.decEncode
decEncode("A")
// result => "00065"
```
@@ -232,7 +241,7 @@ decEncode("A")
Ensures that the value ends with suffix. If it doesn't, it's appended.
```java
import static strman.ensureRight
import static strman.Strman.ensureRight
ensureRight("foo", "bar")
// result => "foobar"
@@ -248,7 +257,7 @@ ensureRight("fooBAR", "bar", false)
Returns the first n chars of String
```java
import static strman.first
import static strman.Strman.first
first("foobar", 3)
// result => "foo"
```
@@ -258,7 +267,7 @@ first("foobar", 3)
Return the first char of String
```java
import static strman.head
import static strman.Strman.head
head("foobar")
// result => "f"
```
@@ -268,7 +277,7 @@ head("foobar")
Convert hexadecimal unicode (4 digits) string to string chars
```java
import static strman.hexDecode
import static strman.Strman.hexDecode
hexDecode("0041")
// result => "A"
```
@@ -278,7 +287,7 @@ hexDecode("0041")
Convert string chars to hexadecimal unicode (4 digits)
```java
import static strman.hexEncode
import static strman.Strman.hexEncode
hexEncode("A")
// result => "0041"
```
@@ -288,7 +297,7 @@ hexEncode("A")
Tests if two Strings are inequal
```java
import static strman.inequal
import static strman.Strman.inequal
inequal("a", "b")
// result => true
```
@@ -298,7 +307,7 @@ inequal("a", "b")
Inserts 'substr' into the 'value' at the 'index' provided.
```java
import static strman.insert
import static strman.Strman.insert
insert("fbar", "oo", 1)
// result => "foobar"
```
@@ -308,7 +317,7 @@ insert("fbar", "oo", 1)
Return the last n chars of String
```java
import static strman.last
import static strman.Strman.last
last("foobarfoo", 3)
// result => "foo"
```
@@ -318,7 +327,7 @@ last("foobarfoo", 3)
Returns a new string of a given length such that the beginning of the string is padded.
```java
import static strman.leftPad
import static strman.Strman.leftPad
leftPad("1", "0", 5)
// result => "00001"
```
@@ -328,7 +337,7 @@ leftPad("1", "0", 5)
This method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from the offset.
```java
import static strman.lastIndexOf
import static strman.Strman.lastIndexOf
lastIndexOf("foobarfoobar", "F", false)
// result => 6
```
@@ -338,7 +347,7 @@ lastIndexOf("foobarfoobar", "F", false)
Removes all spaces on left
```java
import static strman.leftTrim
import static strman.Strman.leftTrim
leftTrim(" strman")
// result => "strman"
```
@@ -499,6 +508,17 @@ transliterate("fóõ bár")
// result => "foo bar"
```
## trimEnd
Removes trailing characters from string.
```
trimEnd(" abc ")
// result => Optional(" abc")
trimEnd("")
// result Optional.empty()
```
## surround
Surrounds a 'value' with the given 'prefix' and 'suffix'.
@@ -562,6 +582,68 @@ toSnakeCase("hello world")
// result => "hello_world"
```
## upperFirst
Converts the first character of string to upper case.
```java
upperFirst("fred")
// result => "Fred"
```
## words
Splits a String to words
```java
words("This is a string, with words!")
// result => ["This", "is", "a", "string", "with", "words"]
```
## isEnclosedBetween
Verifies whether String is enclosed by encloser
```java
isEnclosedBetween("{{shekhar}}", "{{", "}}")
// result => true
```
## join
Join concatenates all the elements of the strings array into a single String. The separator string is placed between elements in the resulting string.
```java
join(new String[]{"hello","world","123"}, ";")
// result => "hello;world;123")
```
## isBlank
isEmpty returns true if a string is blank or null and false otherwise.
```java
isBlank("")
// result => true)
isBlank(null)
// result => true)
isBlank("test")
// result => false)
```
## underscored
`underscored` function converts a camel or dashed string into an underscored one
```java
underscored("MozTransform")
// result => "moz_transform")
```
## Inspiration
This library is inspired by [dleitee/strman](https://github.com/dleitee/strman).
License
-------
strman is licensed under the MIT License - see the `LICENSE` file for details.
strman is licensed under the MIT License - see the `LICENSE` file for details.
+4 -2
View File
@@ -1,15 +1,17 @@
plugins {
id 'net.researchgate.release' version '2.3.3'
id "net.ltgt.errorprone" version "0.0.10"
}
group 'com.shekhargulati'
version '0.1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: "jacoco"
apply from: rootProject.file('gradle/checkstyle.gradle')
apply plugin: "net.ltgt.errorprone"
sourceCompatibility = 1.8
@@ -93,4 +95,4 @@ jacocoTestReport {
}
}
check.dependsOn jacocoTestReport
check.dependsOn jacocoTestReport
+63
View File
@@ -0,0 +1,63 @@
# Version 0.3.0
* __Feature required in #73 (#77)__: Added `charsCount` function
[César Aguilera](mailto:cesar.aguilera.p@gmail.com) - Tue, 9 May 2017 18:16:11 +0530
​
* __fixed bug in containsAll (#76)__
[wli75](mailto:wli75@illinois.edu) - Tue, 7 Mar 2017 20:09:18 +0530
​
* __Resolved #66__: Added `trimEnd` function
[Shekhar Gulati](mailto:shekhargulati84@gmail.com) - Wed, 24 Aug 2016 15:32:45 +0530
​
* __Resolved #65__: Added `trimStart`function
[Shekhar Gulati](mailto:shekhargulati84@gmail.com) - Wed, 24 Aug 2016 15:02:00 +0530
​
* __Resolved #68__: Added `upperFirst` function
[Shekhar Gulati](mailto:shekhargulati84@gmail.com) - Wed, 24 Aug 2016 14:26:16 +0530
​
* __Resolved #69__: Added `words` function
[Shekhar Gulati](mailto:shekhargulati84@gmail.com) - Wed, 24 Aug 2016 12:56:18 +0530
​
* __Resolved#71__: Added `isEnclosedBetween` function
[Shekhar Gulati](mailto:shekhargulati84@gmail.com) - Wed, 24 Aug 2016 12:11:04 +0530
​
* __Resolved #54 and Resolved #59__: Added `capitalize` and `lowerFirst` functions
[Shekhar Gulati](mailto:shekhargulati84@gmail.com) - Wed, 27 Jul 2016 23:19:41 +0530
​
* __Resolved #53__: Added `join` function
[Shekhar Gulati](mailto:shekhargulati84@gmail.com) - Mon, 25 Jul 2016 15:29:43 +0530
​
* __Simplify Maven and Gradle configuration (#70)__
[Marcin Zajączkowski](mailto:mszpak@wp.pl) - Tue, 19 Jul 2016 20:58:57 +0530
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
</suppressions>
+124
View File
@@ -0,0 +1,124 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="com.puppycrawl.tools.checkstyle.Checker">
<!-- Root Checks -->
<module name="com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck">
<property name="lineSeparator" value="lf"/>
</module>
<module name="SuppressionFilter">
<property name="file" value="etc/checkstyle-exclude.xml" />
</module>
<!-- TreeWalker Checks -->
<module name="com.puppycrawl.tools.checkstyle.TreeWalker">
<!-- Annotations -->
<module name="com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck">
<property name="elementStyle" value="compact" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.annotation.MissingOverrideCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.annotation.PackageAnnotationCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationLocationCheck">
<property name="allowSamelineSingleParameterlessAnnotation"
value="false" />
</module>
<!-- Block Checks -->
<module name="com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck">
<property name="option" value="text" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck">
<property name="tokens" value="LITERAL_CATCH"/>
<property name="option" value="alone" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.blocks.AvoidNestedBlocksCheck" />
<!-- Class Design -->
<module name="com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.design.InterfaceIsTypeCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.design.HideUtilityClassConstructorCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.design.MutableExceptionCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.design.InnerTypeLastCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck" />
<!-- Coding -->
<module name="com.puppycrawl.tools.checkstyle.checks.coding.CovariantEqualsCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.coding.EmptyStatementCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanExpressionCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanReturnCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.coding.StringLiteralEqualityCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.coding.NestedForDepthCheck">
<property name="max" value="3" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.coding.NestedIfDepthCheck">
<property name="max" value="3" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.coding.NestedTryDepthCheck">
<property name="max" value="3" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.coding.MultipleVariableDeclarationsCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck">
<property name="checkMethods" value="false" />
<property name="validateOnlyOverlapping" value="false" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.coding.OneStatementPerLineCheck" />
<!-- Imports -->
<module name="com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck">
<property name="excludes" value="javax.persistence, java.util.stream.Collectors, java.util.*,org.hamcrest.CoreMatchers, org.junit.Assert, strman.Strman"/>
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck">
<property name="processJavadoc" value="true" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck">
<property name="groups" value="java,/^javax?\./,*" />
<property name="ordered" value="true" />
<property name="separated" value="true" />
<property name="option" value="bottom" />
<property name="sortStaticImportsAlphabetically" value="true" />
</module>
<!-- Miscellaneous -->
<module name="com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck">
<property name="tokens" value="BLOCK_COMMENT_BEGIN"/>
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.UpperEllCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.ArrayTypeStyleCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheck" />
<!-- Modifiers -->
<module name="com.puppycrawl.tools.checkstyle.checks.modifier.RedundantModifierCheck" />
<!-- Regexp -->
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="maximum" value="0"/>
<property name="format" value="org\.junit\.Assert\.assert" />
<property name="message"
value="Please use AssertJ imports." />
<property name="ignoreComments" value="true" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck">
<property name="format" value="[ \t]+$" />
<property name="illegalPattern" value="true" />
<property name="message" value="Trailing whitespace" />
</module>
<!-- Whitespace -->
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck" >
<property name="tokens" value="BNOT, DEC, DOT, INC, LNOT, UNARY_MINUS, UNARY_PLUS, ARRAY_DECLARATOR"/>
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.TypecastParenPadCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck" />
<module name="com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck" />
</module>
</module>
+1 -1
View File
@@ -1 +1 @@
version=0.1.0
version= 0.4.0
+10
View File
@@ -0,0 +1,10 @@
apply plugin: 'checkstyle'
checkstyle {
configFile rootProject.file('etc/checkstyle.xml')
showViolations true
}
dependencies {
checkstyle 'com.puppycrawl.tools:checkstyle:7.7'
}
+1 -1
View File
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip
+367 -42
View File
@@ -1,4 +1,3 @@
/*
*
* * The MIT License
@@ -27,23 +26,26 @@
package strman;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.util.stream.Collectors.joining;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.*;
/**
* A String manipulation library without any dependencies
*/
public abstract class Strman {
private static final Predicate<String> NULL_STRING_PREDICATE = str -> str == null;
private static final Predicate<String> NULL_STRING_PREDICATE = Objects::isNull;
private static final Supplier<String> NULL_STRING_MSG_SUPPLIER = () -> "'value' should be not null.";
private static final String[] EMPTY_ARRAY = new String[0];
private Strman() {
}
@@ -88,7 +90,7 @@ public abstract class Strman {
* @return an Optional String if found else empty
*/
public static Optional<String> at(final String value, int index) {
if (value == null || value.isEmpty()) {
if (isNullOrEmpty(value)) {
return Optional.empty();
}
int length = value.length();
@@ -113,7 +115,8 @@ public abstract class Strman {
validate(end, NULL_STRING_PREDICATE, () -> "'end' should be not null.");
String[] parts = value.split(end);
return Arrays.stream(parts).map(subPart -> subPart.substring(subPart.indexOf(start) + start.length())).toArray(String[]::new);
return Arrays.stream(parts).map(subPart -> subPart.substring(subPart.indexOf(start) + start.length()))
.toArray(String[]::new);
}
/**
@@ -127,7 +130,6 @@ public abstract class Strman {
return value.split("");
}
/**
* Replace consecutive whitespace characters with a single space.
*
@@ -139,7 +141,6 @@ public abstract class Strman {
return value.trim().replaceAll("\\s\\s+", " ");
}
/**
* Verifies that the needle is contained in the value. The search is case insensitive
*
@@ -176,7 +177,7 @@ public abstract class Strman {
*/
public static boolean containsAll(final String value, final String[] needles) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Arrays.stream(needles).allMatch(needle -> contains(value, needle, true));
return Arrays.stream(needles).allMatch(needle -> contains(value, needle, false));
}
/**
@@ -236,9 +237,11 @@ public abstract class Strman {
* @param allowOverlapping boolean to take into account overlapping
* @return count of times substring exists
*/
public static long countSubstr(final String value, final String subStr, final boolean caseSensitive, boolean allowOverlapping) {
public static long countSubstr(final String value, final String subStr, final boolean caseSensitive,
boolean allowOverlapping) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return countSubstr(caseSensitive ? value : value.toLowerCase(), caseSensitive ? subStr : subStr.toLowerCase(), allowOverlapping, 0L);
return countSubstr(caseSensitive ? value : value.toLowerCase(), caseSensitive ? subStr : subStr.toLowerCase(),
allowOverlapping, 0L);
}
/**
@@ -275,7 +278,8 @@ public abstract class Strman {
* @param caseSensitive true or false
* @return true or false
*/
public static boolean endsWith(final String value, final String search, final int position, final boolean caseSensitive) {
public static boolean endsWith(final String value, final String search, final int position,
final boolean caseSensitive) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
int remainingLength = position - search.length();
if (caseSensitive) {
@@ -321,7 +325,7 @@ public abstract class Strman {
*/
public static String base64Decode(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return new String(Base64.getDecoder().decode(value));
return new String(Base64.getDecoder().decode(value), StandardCharsets.UTF_8);
}
/**
@@ -332,7 +336,7 @@ public abstract class Strman {
*/
public static String base64Encode(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Base64.getEncoder().encodeToString(value.getBytes());
return Base64.getEncoder().encodeToString(value.getBytes(StandardCharsets.UTF_8));
}
/**
@@ -406,9 +410,8 @@ public abstract class Strman {
* @param n Number of chars to return
* @return The first n chars
*/
public static String first(final String value, final int n) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return value.substring(0, n);
public static Optional<String> first(final String value, final int n) {
return Optional.ofNullable(value).filter(v -> !v.isEmpty()).map(v -> v.substring(0, n));
}
/**
@@ -417,7 +420,7 @@ public abstract class Strman {
* @param value The input String
* @return The first char
*/
public static String head(final String value) {
public static Optional<String> head(final String value) {
return first(value, 1);
}
@@ -430,7 +433,7 @@ public abstract class Strman {
*/
public static String format(final String value, String... params) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
Pattern p = Pattern.compile("\\{(\\w+)\\}");
Pattern p = Pattern.compile("\\{(\\w+)}");
Matcher m = p.matcher(value);
String result = value;
while (m.find()) {
@@ -488,6 +491,18 @@ public abstract class Strman {
* @param second The second String
* @return true if first and second are not equal false otherwise
*/
public static boolean unequal(final String first, final String second) {
return !Objects.equals(first, second);
}
/**
* Tests if two Strings are inequal
*
* @param first The first String
* @param second The second String
* @return true if first and second are not equal false otherwise
* @deprecated use unequal instead
*/
public static boolean inequal(final String first, final String second) {
return !Objects.equals(first, second);
}
@@ -517,7 +532,12 @@ public abstract class Strman {
*/
public static boolean isUpperCase(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Objects.equals(value, value.toUpperCase());
for (int i = 0; i < value.length(); i++) {
if (Character.isLowerCase(value.charAt(i))) {
return false;
}
}
return true;
}
/**
@@ -528,7 +548,12 @@ public abstract class Strman {
*/
public static boolean isLowerCase(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Objects.equals(value, value.toLowerCase());
for (int i = 0; i < value.length(); i++) {
if (Character.isUpperCase(value.charAt(i))) {
return false;
}
}
return true;
}
/**
@@ -613,7 +638,8 @@ public abstract class Strman {
* @param caseSensitive whether search should be case sensitive
* @return Return position of the last occurrence of 'needle'.
*/
public static int lastIndexOf(final String value, final String needle, final int offset, final boolean caseSensitive) {
public static int lastIndexOf(final String value, final String needle, final int offset,
final boolean caseSensitive) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
validate(needle, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
if (caseSensitive) {
@@ -709,11 +735,10 @@ public abstract class Strman {
public static String removeLeft(final String value, final String prefix, final boolean caseSensitive) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
validate(prefix, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
BiFunction<String, String, String> fx = (f, s) -> f.startsWith(s) ? f.replaceFirst(s, "") : f;
if (caseSensitive) {
return fx.apply(value, prefix);
return value.startsWith(prefix) ? value.substring(prefix.length()) : value;
}
return fx.apply(value.toLowerCase(), prefix.toLowerCase());
return value.toLowerCase().startsWith(prefix.toLowerCase()) ? value.substring(prefix.length()) : value;
}
/**
@@ -749,7 +774,8 @@ public abstract class Strman {
public static String removeRight(final String value, final String suffix, final boolean caseSensitive) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
validate(suffix, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return endsWith(value, suffix, caseSensitive) ? value.substring(0, value.toLowerCase().lastIndexOf(suffix.toLowerCase())) : value;
return endsWith(value, suffix, caseSensitive) ? value
.substring(0, value.toLowerCase().lastIndexOf(suffix.toLowerCase())) : value;
}
/**
@@ -784,13 +810,15 @@ public abstract class Strman {
* @param caseSensitive whether search should be case sensitive or not
* @return String replaced with 'newvalue'.
*/
public static String replace(final String value, final String search, final String newValue, final boolean caseSensitive) {
public static String replace(final String value, final String search, final String newValue,
final boolean caseSensitive) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
validate(search, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
if (caseSensitive) {
return value.replace(search, newValue);
}
return value.toLowerCase().replace(search.toLowerCase(), newValue);
return Pattern.compile(search, Pattern.CASE_INSENSITIVE).matcher(value)
.replaceAll(Matcher.quoteReplacement(newValue));
}
/**
@@ -881,10 +909,22 @@ public abstract class Strman {
* @return Words Array
*/
public static String[] words(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return value.split("\\W+");
return words(value, "\\s+");
}
/**
* Splits a String to words by delimiter, \s+ by default
*
* @param value The input String
* @param delimiter delimiter for splitting input String
* @return words array
*/
public static String[] words(final String value, final String delimiter) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return value.split(delimiter);
}
/**
* Truncate the unsecured form string, cutting the independent string of required position.
*
@@ -924,7 +964,8 @@ public abstract class Strman {
*/
public static String htmlEncode(final String html) {
validate(html, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return html.chars().mapToObj(c -> "\\u" + String.format("%04x", c).toUpperCase()).map(e -> HtmlEntities.encodedEntities.get(e)).collect(joining());
return html.chars().mapToObj(c -> "\\u" + String.format("%04x", c).toUpperCase())
.map(HtmlEntities.encodedEntities::get).collect(joining());
}
/**
@@ -968,7 +1009,7 @@ public abstract class Strman {
public static String slugify(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
String transliterated = transliterate(collapseWhitespace(value.trim().toLowerCase()));
return Arrays.stream(words(transliterated.replace("&", "-and-"))).collect(joining("-"));
return Arrays.stream(words(transliterated.replace("&", "-and-"), "\\W+")).collect(joining("-"));
}
/**
@@ -989,7 +1030,6 @@ public abstract class Strman {
return result;
}
/**
* Surrounds a 'value' with the given 'prefix' and 'suffix'.
*
@@ -1011,7 +1051,9 @@ public abstract class Strman {
* @return String in camelCase.
*/
public static String toCamelCase(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
if (value == null || value.length() == 0) {
return "";
}
String str = toStudlyCase(value);
return str.substring(0, 1).toLowerCase() + str.substring(1);
}
@@ -1025,7 +1067,7 @@ public abstract class Strman {
public static String toStudlyCase(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
String[] words = collapseWhitespace(value.trim()).split("\\s*(_|-|\\s)\\s*");
return Arrays.stream(words).filter(w -> !w.trim().isEmpty()).map(w -> head(w).toUpperCase() + tail(w)).collect(joining());
return Arrays.stream(words).filter(w -> !w.trim().isEmpty()).map(Strman::upperFirst).collect(joining());
}
/**
@@ -1034,22 +1076,21 @@ public abstract class Strman {
* @param value The input String
* @return String tail
*/
public static String tail(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return last(value, value.length() - 1);
public static Optional<String> tail(final String value) {
return Optional.ofNullable(value).filter(v -> !v.isEmpty()).map(v -> last(v, v.length() - 1));
}
/**
* Decamelize String
*
* @param value The input String
* @param chr string to use
* @param chr string to use
* @return String decamelized.
*/
public static String toDecamelize(final String value, final String chr) {
String camelCasedString = toCamelCase(value);
String[] words = camelCasedString.split("(?=\\p{Upper})");
return Arrays.stream(words).map(w -> w.toLowerCase()).collect(joining(Optional.ofNullable(chr).orElse(" ")));
return Arrays.stream(words).map(String::toLowerCase).collect(joining(Optional.ofNullable(chr).orElse(" ")));
}
/**
@@ -1074,8 +1115,7 @@ public abstract class Strman {
public static String decode(final String value, final int digits, final int radix) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Arrays
.stream(value.split("(?<=\\G.{" + digits + "})"))
return Arrays.stream(value.split("(?<=\\G.{" + digits + "})"))
.map(data -> String.valueOf(Character.toChars(Integer.parseInt(data, radix))))
.collect(joining());
}
@@ -1085,6 +1125,285 @@ public abstract class Strman {
return value.chars().mapToObj(ch -> leftPad(Integer.toString(ch, radix), "0", digits)).collect(joining());
}
/**
* Join concatenates all the elements of the strings array into a single String. The separator string is placed between elements in the resulting string.
*
* @param strings The input array to concatenate
* @param separator The separator to use
* @return Concatenated String
*/
public static String join(final String[] strings, final String separator) throws IllegalArgumentException {
if (strings == null) {
throw new IllegalArgumentException("Input array 'strings' can't be null");
}
if (separator == null) {
throw new IllegalArgumentException("separator can't be null");
}
StringJoiner joiner = new StringJoiner(separator);
for (String el : strings) {
joiner.add(el);
}
return joiner.toString();
}
/**
* Converts the first character of string to upper case and the remaining to lower case.
*
* @param input The string to capitalize
* @return The capitalized string
*/
public static String capitalize(final String input) throws IllegalArgumentException {
if (input == null) {
throw new IllegalArgumentException("input can't be null");
}
if (input.length() == 0) {
return "";
}
return head(input).map(String::toUpperCase).map(h -> tail(input).map(t -> h + t.toLowerCase()).orElse(h)).get();
}
/**
* Converts the first character of string to lower case.
*
* @param input The string to convert
* @return The converted string
*/
public static String lowerFirst(final String input) throws IllegalArgumentException {
if (input == null) {
throw new IllegalArgumentException("input can't be null");
}
if (input.length() == 0) {
return "";
}
return head(input).map(String::toLowerCase).map(h -> tail(input).map(t -> h + t).orElse(h)).get();
}
/**
* Verifies whether String is enclosed by encloser
*
* @param input The input String
* @param encloser String which encloses input String
* @return true if enclosed false otherwise
*/
public static boolean isEnclosedBetween(final String input, final String encloser) {
return isEnclosedBetween(input, encloser, encloser);
}
/**
* Verifies whether String is enclosed by encloser
*
* @param input The input String
* @param leftEncloser String which encloses input String at left start
* @param rightEncloser String which encloses input String at the right end
* @return true if enclosed false otherwise
*/
public static boolean isEnclosedBetween(final String input, final String leftEncloser, String rightEncloser) {
if (input == null) {
throw new IllegalArgumentException("input can't be null");
}
if (leftEncloser == null) {
throw new IllegalArgumentException("leftEncloser can't be null");
}
if (rightEncloser == null) {
throw new IllegalArgumentException("rightEncloser can't be null");
}
return input.startsWith(leftEncloser) && input.endsWith(rightEncloser);
}
/**
* Converts the first character of string to upper case.
*
* @param input The string to convert.
* @return Returns the converted string.
*/
public static String upperFirst(String input) {
if (input == null) {
throw new IllegalArgumentException("input can't be null");
}
return head(input).map(String::toUpperCase).map(h -> tail(input).map(t -> h + t).orElse(h)).get();
}
/**
* Removes leading whitespace from string.
*
* @param input The string to trim.
* @return Returns the trimmed string.
*/
public static Optional<String> trimStart(final String input) {
return Optional.ofNullable(input).filter(v -> !v.isEmpty()).map(Strman::leftTrim);
}
/**
* Removes leading characters from string.
*
* @param input The string to trim.
* @param chars The characters to trim.
* @return Returns the trimmed string.
*/
public static Optional<String> trimStart(final String input, String... chars) {
return Optional.ofNullable(input).filter(v -> !v.isEmpty()).map(v -> {
String pattern = String.format("^[%s]+", join(chars, "\\"));
return v.replaceAll(pattern, "");
});
}
/**
* Removes trailing whitespace from string.
*
* @param input The string to trim.
* @return Returns the trimmed string.
*/
public static Optional<String> trimEnd(final String input) {
return Optional.ofNullable(input).filter(v -> !v.isEmpty()).map(Strman::rightTrim);
}
/**
* Removes trailing characters from string.
*
* @param input The string to trim.
* @param chars The characters to trim.
* @return Returns the trimmed string.
*/
public static Optional<String> trimEnd(final String input, String... chars) {
return Optional.ofNullable(input).filter(v -> !v.isEmpty()).map(v -> {
String pattern = String.format("[%s]+$", join(chars, "\\"));
return v.replaceAll(pattern, "");
});
}
/**
* Counts the number of occurrences of each character in the string
*
* @param input The input string
* @return A map containing the number of occurrences of each character in the string
*/
public static Map<Character, Long> charsCount(String input) {
if (isNullOrEmpty(input)) {
return Collections.emptyMap();
}
return input.chars().mapToObj(c -> (char) c).collect(groupingBy(identity(), counting()));
}
/**
* Checks if string is empty. This is a null safe check and will return true when string is null.
*
* @param input The input string
* @return true if input string is null or empty
*/
public static boolean isBlank(String input) {
return input == null || input.isEmpty();
}
/**
* Changes passed in string to all lower case and adds underscore between words.
*
* @param input The input string
* @return the input string in all lower case with underscores between words
*/
public static String underscored(final String input) {
if (input == null || input.length() == 0) {
return "";
}
return input.trim().replaceAll("([a-z\\d])([A-Z]+)", "$1_$2").replaceAll("[-\\s]+", "_").toLowerCase();
}
/**
* Aggregates the contents of n strings into a single list of tuples.
*
* @param inputs A list of strings.
* @return A list of strings if none of the strings in the input is null or empty.
* An empty list otherwise.
*/
public static List<String> zip(String... inputs) {
if (inputs.length == 0) {
return Collections.emptyList();
}
OptionalInt min = Arrays.stream(inputs).mapToInt(str -> str == null ? 0 : str.length()).min();
if (!min.isPresent()) {
return Collections.emptyList();
}
return IntStream.range(0, min.getAsInt())
.mapToObj(elementIndex -> Arrays.stream(inputs)
.map(input -> String.valueOf(input.charAt(elementIndex)))
.collect(joining()))
.collect(toList());
}
/**
* Split lines to an array
*
* @param input The input String
* @return lines in an array
*/
public static String[] lines(String input) {
if (input == null) {
return EMPTY_ARRAY;
}
return input.split("\r\n?|\n");
}
/**
* Converts a underscored or camelized string into an dasherized one.
*
* @param input The input String
* @return dasherized String.
*/
public static String dasherize(String input) {
return toKebabCase(input);
}
/**
* Converts an underscored, camelized, or dasherized string into a humanized one. Also removes beginning and ending whitespace.
*
* @param input The input String
* @return humanized version of String
*/
public static String humanize(final String input) {
if (input == null || input.length() == 0) {
return "";
}
return upperFirst(underscored(input).replaceAll("_", " "));
}
/**
* Returns a copy of the string in which all the case-based characters have had their case swapped.
*
* @param input Input string
* @return String with all the case swapped
*/
public static String swapCase(String input) {
if (input == null || input.length() == 0) {
return "";
}
StringBuilder resultBuilder = new StringBuilder();
for (char ch : input.toCharArray()) {
if (Character.isUpperCase(ch)) {
resultBuilder.append(Character.toLowerCase(ch));
} else {
resultBuilder.append(Character.toUpperCase(ch));
}
}
return resultBuilder.toString();
}
public static String[] chop(String input, int step) {
if (input == null || input.length() == 0) {
return EMPTY_ARRAY;
}
if (step == 0) {
return new String[]{input};
}
int strLength = input.length();
int iterations = strLength % step == 0 ? strLength / step : strLength / step + 1;
return IntStream.iterate(0, i -> i + step)
.limit(iterations)
.mapToObj(i -> input.substring(i, (i + step) < strLength ? i + step : strLength))
.toArray(String[]::new);
}
private static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
if (predicate.test(value)) {
throw new IllegalArgumentException(supplier.get());
@@ -1104,4 +1423,10 @@ public abstract class Strman {
}
return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count);
}
private static boolean isNullOrEmpty(String input) {
return input == null || input.isEmpty();
}
}
@@ -25,21 +25,30 @@
*/
package strman;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.junit.Ignore;
import org.junit.Test;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.collection.IsArrayContaining.hasItemInArray;
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;
import static org.hamcrest.collection.IsArrayWithSize.emptyArray;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.Assert.*;
import static strman.Strman.*;
import static strman.Strman.endsWith;
import static strman.Strman.format;
public class StrmanTest {
public class StrmanTests {
@Test
public void append_shouldAppendStringsToEndOfValue() throws Exception {
@@ -343,7 +352,7 @@ public class StrmanTest {
final String[] fixture = {
"foo", "foobar"
};
Arrays.stream(fixture).forEach(el -> assertThat(first(el, 3), equalTo("foo")));
Arrays.stream(fixture).forEach(el -> assertThat(first(el, 3), equalTo(Optional.of("foo"))));
}
@Test
@@ -352,7 +361,7 @@ public class StrmanTest {
"foo", "foobar"
};
Arrays.stream(fixture).forEach(el -> assertThat(head(el), equalTo("f")));
Arrays.stream(fixture).forEach(el -> assertThat(head(el), equalTo(Optional.of("f"))));
}
@Test
@@ -409,9 +418,9 @@ public class StrmanTest {
@Test
public void inequal_shouldTestInequalityOfStrings() throws Exception {
assertThat(inequal("a", "b"), equalTo(true));
assertThat(inequal("a", "a"), equalTo(false));
assertThat(inequal("0", "1"), equalTo(true));
assertThat(unequal("a", "b"), equalTo(true));
assertThat(unequal("a", "a"), equalTo(false));
assertThat(unequal("0", "1"), equalTo(true));
}
@Test
@@ -799,7 +808,8 @@ public class StrmanTest {
" de_camelize"
};
Arrays.stream(fixture).forEach(el -> assertThat(String.format("toKebabCase(%s) should be de-camelize", el), toKebabCase(el), equalTo("de-camelize")));
Arrays.stream(fixture).forEach(el ->
assertThat(String.format("toKebabCase(%s) should be de-camelize", el), toKebabCase(el), equalTo("de-camelize")));
}
@Test
@@ -819,5 +829,361 @@ public class StrmanTest {
Arrays.stream(fixture).forEach(el -> assertThat(String.format("toSnakeCase(%s) should be de_camelize", el), toSnakeCase(el), equalTo("de_camelize")));
}
@Test
public void snakeCase_shouldConvertAStringToSnakecase() throws Exception {
String[] input = {
"Foo Bar",
"fooBar"
};
}
Arrays.stream(input).forEach(el ->
assertThat(String.format("%s should be foo_bar", el), toSnakeCase(el), is(equalTo("foo_bar"))));
}
@Test
public void unequal_shouldTestInequalityOfStrings() throws Exception {
assertThat(unequal("a", "b"), equalTo(true));
assertThat(unequal("a", "a"), equalTo(false));
assertThat(unequal("0", "1"), equalTo(true));
}
@Test
public void removeLeft_shouldNotLowercaseWhenCaseInsensitive() throws Exception {
String result = removeLeft("This HAS A THIS IN FRONT", "THIS ", false);
assertThat(result, is("HAS A THIS IN FRONT"));
}
@Test
public void replace_shouldNotLowercaseWhenCaseInsensitive() throws Exception {
String result = replace("One and two and THREE and Four", "and", "&", false);
assertThat(result, is("One & two & THREE & Four"));
}
@Test
public void removeRight_shouldNotLowercaseWhenCaseInsensitive() throws Exception {
String result = removeRight("Remove the END at the end", " END", false);
assertThat(result, is("Remove the END at the"));
}
@Test
public void transliterate_shouldDeburrTheString() throws Exception {
String result = transliterate("déjà vu");
assertThat(result, is(equalTo("deja vu")));
}
@Ignore
public void htmlEncode_shouldConvertCharactersToTheirHtmlEntities() throws Exception {
String result = htmlEncode("fred, barney, & pebbles");
assertThat(result, is(equalTo("fred, barney, &amp; pebbles")));
}
@Test
public void kebabCase_shouldConvertAStringToKebabCase() throws Exception {
String[] input = {
"Foo Bar",
"fooBar"
};
Arrays.stream(input).forEach(el ->
assertThat(String.format("%s should be foo-bar", el), toKebabCase(el), is(equalTo("foo-bar"))));
}
@Test
public void join_shouldJoinArrayOfStringIntoASingleString() throws Exception {
String[] strings = {
"hello",
"world",
"123"
};
assertThat(join(strings, ";"), is(equalTo("hello;world;123")));
}
@Test(expected = IllegalArgumentException.class)
public void join_shouldThrowIllegalArgumentExceptionWhenSeparatorIsNull() throws Exception {
String[] strings = {
"hello",
"world",
"123"
};
join(strings, null);
}
@Test
public void join_shouldReturnEmptyStringWhenInputArrayIsEmpty() throws Exception {
String[] emptyArray = {};
assertThat(join(emptyArray, ","), is(equalTo("")));
}
@Test
public void capitalize_shouldCapitalizeFirstCharacterOfString() throws Exception {
String[] strings = {
"FRED",
"fRED",
"fred"
};
Arrays.stream(strings).forEach(el -> assertThat(String.format("%s should be Fred", el), capitalize(el), equalTo("Fred")));
}
@Test
public void lowerFirst_shouldLowercasedFirstCharacterOfString() throws Exception {
assertThat(lowerFirst("FRED"), is(equalTo("fRED")));
assertThat(lowerFirst("fred"), is(equalTo("fred")));
assertThat(lowerFirst("Fred"), is(equalTo("fred")));
}
@Test
public void isEnclosedBetween_shouldChekcWhetherStringIsEnclosed() throws Exception {
assertThat(isEnclosedBetween("{{shekhar}}", "{{", "}}"), is(true));
assertThat(isEnclosedBetween("shekhar", "{{", "}}"), is(false));
assertThat(isEnclosedBetween("*shekhar*", "*"), is(true));
assertThat(isEnclosedBetween("shekhar", "*"), is(false));
}
@Test(expected = IllegalArgumentException.class)
public void isEnclosedBetween_shouldThrowIllegalArgumentExceptionWhenEncloserIsNull() throws Exception {
assertThat(isEnclosedBetween("shekhar", null), is(false));
}
@Test
public void words_shouldConvertTextToWords() throws Exception {
final String line = "This is a string, with words!";
assertThat(words(line), arrayContaining("This", "is", "a", "string,", "with", "words!"));
}
@Test
public void upperFirst_shouldConvertFirstCharToUpperCase() throws Exception {
assertThat(upperFirst("fred"), is("Fred"));
}
@Test
public void upperFirst_shouldReturnSameStringIfFirstCharIsUpperCase() throws Exception {
assertThat(upperFirst("FRED"), is("FRED"));
}
@Test
public void trimStart_shouldRemoveAllWhitespaceAtStart() throws Exception {
assertThat(trimStart(" abc "), is(Optional.of("abc ")));
assertThat(trimStart("abc "), is(Optional.of("abc ")));
assertThat(trimStart("abc"), is(Optional.of("abc")));
assertThat(trimStart(""), is(Optional.empty()));
assertThat(trimStart(null), is(Optional.empty()));
}
@Test
public void trimStart_shouldRemoveSpecialCharactersAtStart() throws Exception {
assertThat(trimStart("-_-abc-_-", "_", "-"), is(Optional.of("abc-_-")));
assertThat(trimStart("-_-!abc-_-", "_", "-", "!"), is(Optional.of("abc-_-")));
assertThat(trimStart("-_-#abc-_-", "_", "-", "!", "#"), is(Optional.of("abc-_-")));
}
@Test
public void trimEnd_shouldRemoveAllTrailingWhitespace() throws Exception {
assertThat(trimEnd(" abc "), is(Optional.of(" abc")));
assertThat(trimEnd("abc "), is(Optional.of("abc")));
assertThat(trimEnd("abc"), is(Optional.of("abc")));
assertThat(trimEnd(""), is(Optional.empty()));
assertThat(trimEnd(null), is(Optional.empty()));
}
@Test
public void trimEnd_shouldRemoveAllTrailingSpecialCharacters() throws Exception {
assertThat(trimEnd("-_-abc-_-", "_", "-"), is(Optional.of("-_-abc")));
assertThat(trimEnd("-_-abc!-_-", "_", "-", "!"), is(Optional.of("-_-abc")));
assertThat(trimEnd("-_-abc#-_-", "_", "-", "!", "#"), is(Optional.of("-_-abc")));
}
@Test
public void charsCount_shouldReturnEmptyWhenInputStringIsNull() {
assertThat(charsCount(null), equalTo(Collections.emptyMap()));
}
@Test
public void charsCount_shouldReturnEmptyWhenInputStringIsEmpty() {
assertThat(charsCount(""), equalTo(Collections.emptyMap()));
}
@Test
public void charsCount_shouldReturnCharsCountWhenInputIsASimpleString() {
Map<Character, Long> expectedOutput = new HashMap<Character, Long>() {{
put('a', 1L);
put('b', 1L);
put('c', 1L);
}};
assertThat(charsCount("abc"), equalTo(expectedOutput));
}
@Test
public void charsCount_shouldReturnCharsCountWhenInputIsAComplexString() {
Map<Character, Long> expectedOutput = new HashMap<Character, Long>() {{
put('a', 1L);
put('b', 2L);
put('c', 3L);
put('A', 1L);
put('B', 2L);
put('C', 3L);
put('-', 10L);
}};
assertThat(charsCount("-----abbcccCCCBBA-----"), equalTo(expectedOutput));
}
@Test
public void isBlank_shouldReturnTrueIfNull() {
assertTrue(isBlank(null));
}
@Test
public void isBlank_shouldReturnTrueIfEmpty() {
assertTrue(isBlank(""));
}
@Test
public void isBlank_shouldReturnFalseIfNotEmpty() {
assertFalse(isBlank("ac"));
}
@Test
public void underscored_shouldReturnUnderscoredString() {
assertThat(underscored("MozTransform"), equalTo("moz_transform"));
}
@Test
public void underscored_shouldReturnEmptyStringIfEmptyStringPassedIn() {
assertThat(underscored(""), equalTo(""));
}
@Test
public void underscored_shouldReturnNullIfNullPassedIn() {
assertThat(underscored(null), equalTo(""));
}
@Test
public void underscored_shouldUnderscoreInputString() throws Exception {
// assertThat(underscored("foo-bar-baz"), equalTo("foo_bar_baz"));
assertThat(underscored("fooBarBaz"), equalTo("foo_bar_baz"));
// assertThat(underscored("FooBarBaz"), equalTo("foo_bar_baz"));
// assertThat(underscored(" foo bar baz "), equalTo("foo_bar_baz"));
}
@Test
public void zip_shouldReturnEmptyList_whenNullOrEmptyIsPassedIn() {
assertThat(zip("a", null), is(empty()));
assertThat(zip("", "a"), is(empty()));
}
@Test
public void zip_shouldReturnListOfOneElement_forSimplestValidInput() {
assertThat(zip("a", "b"), equalTo(asList("ab")));
}
@Test
public void zip_shouldReturnExpectedListOfPairs_whenBothInputsHaveSameSize() {
assertThat(zip("abc", "def"), equalTo(asList("ad", "be", "cf")));
assertThat(zip("ABC", "DEF"), equalTo(asList("AD", "BE", "CF")));
}
@Test
public void zip_shouldReturnExpectedListOfPairs_whenFirstInputIsBiggerThanSecond() {
assertThat(zip("abc", "d"), equalTo(asList("ad")));
assertThat(zip("ABCDE", "FGH"), equalTo(asList("AF", "BG", "CH")));
}
@Test
public void zip_shouldReturnExpectedListOfPairs_whenSecondInputIsBiggerThanFirst() {
assertThat(zip("d", "abc"), equalTo(asList("da")));
assertThat(zip("FGH", "ABCDE"), equalTo(asList("FA", "GB", "HC")));
}
@Test
public void zip_shouldReturnExpectedListOfTriplets_whenThreeInputs() {
assertThat(zip("abc", "def", "ghi"), equalTo(asList("adg", "beh", "cfi")));
}
@Test
public void zip_shouldReturnExpectedListOfTuples_whenMoreThanThreeInputs() {
assertThat(zip("abc", "def", "ghi", "123"), equalTo(asList("adg1", "beh2", "cfi3")));
}
@Test
public void zip_shouldReturnEmptyList_whenNotEnoughInputs() {
assertThat(zip(), is(empty()));
}
@Test
public void zip_shouldReturnInputInListForm_whenOnlyOneInput() {
assertThat(zip("zip"), is(equalTo(asList("z", "i", "p"))));
assertThat(zip("z"), is(equalTo(singletonList("z"))));
}
@Test
public void lines_shouldReturnEmptyArrayWhenInputIsNull() throws Exception {
assertThat(lines(null), emptyArray());
}
@Test
public void lines_shouldReturnArrayWithOneEmptyElementWhenInputIsEmptyString() throws Exception {
assertThat(lines(""), arrayWithSize(1));
assertThat(lines(""), hasItemInArray(""));
}
@Test
public void lines_shouldSplitToLines() throws Exception {
assertThat(lines("Hello\r\nWorld").length, equalTo(2));
assertThat(lines("Hello\rWorld").length, equalTo(2));
assertThat(lines("Hello World").length, equalTo(1));
assertThat(lines("\r\n\n\r ").length, equalTo(4));
assertThat(lines("Hello\r\r\nWorld").length, equalTo(3));
assertThat(lines("Hello\r\rWorld").length, equalTo(3));
}
@Test
public void humanize_shouldHumanizeStrings() throws Exception {
assertThat(humanize("the_humanize_method"), equalTo("The humanize method"));
assertThat(humanize("ThehumanizeMethod"), equalTo("Thehumanize method"));
assertThat(humanize("ThehumanizeMethod"), equalTo("Thehumanize method"));
assertThat(humanize("the humanize method"), equalTo("The humanize method"));
assertThat(humanize("the humanize_id method_id"), equalTo("The humanize id method id"));
assertThat(humanize("the humanize method "), equalTo("The humanize method"));
assertThat(humanize(" capitalize dash-CamelCase_underscore trim "), equalTo("Capitalize dash camel case underscore trim"));
assertThat(humanize(""), equalTo(""));
assertThat(humanize(null), equalTo(""));
}
@Test
public void dasherize_shouldDasherizeInputString() throws Exception {
assertThat(dasherize("the_dasherize_string_method"), equalTo("the-dasherize-string-method"));
assertThat(dasherize("TheDasherizeStringMethod"), equalTo("the-dasherize-string-method"));
assertThat(dasherize("thisIsATest"), equalTo("this-is-a-test"));
assertThat(dasherize("this Is A Test"), equalTo("this-is-a-test"));
assertThat(dasherize("thisIsATest123"), equalTo("this-is-a-test123"));
assertThat(dasherize("123thisIsATest"), equalTo("123this-is-a-test"));
assertThat(dasherize("the dasherize string method"), equalTo("the-dasherize-string-method"));
assertThat(dasherize("the dasherize string method "), equalTo("the-dasherize-string-method"));
assertThat(dasherize("input with a-dash"), equalTo("input-with-a-dash"));
assertThat(dasherize(""), equalTo(""));
assertThat(dasherize(null), equalTo(""));
}
@Test
public void swapCase_shouldSwapCaseOfCharacters() throws Exception {
assertThat(swapCase("AaBbCcDdEe"), equalTo("aAbBcCdDeE"));
assertThat(swapCase("Hello World"), equalTo("hELLO wORLD"));
assertThat(swapCase(""), equalTo(""));
assertThat(swapCase(null), equalTo(""));
}
@Test
public void chop_shouldChopStringByStep() throws Exception {
assertThat(chop(null, 2).length, equalTo(0));
assertThat(chop("whitespace", 2).length, equalTo(5));
assertThat(chop("whitespace", 3).length, equalTo(4));
assertThat(chop("whitespace", 0)[0].length(), equalTo(10));
}
}