Created
September 12, 2016 20:57
-
-
Save Lokutus/a08114d15346f89b5a66fedf86ef0353 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns defined substring from left, right, leftBack, rightBack, middle, using index or delimiter. | |
* <p/> | |
* Example:<br/> | |
* <code> | |
* Stringer.from("ABCDE-12345/2016").right("-").left("/"); | |
* </code> | |
* <p/> | |
* <code> | |
* Stringer.from("ABCDE-12345/2016").middle("-", "/"); | |
* </code> | |
* <p/> | |
* <code> | |
* Stringer.from("ABCDE-12345/2016").right(4); | |
* </code> | |
* | |
* @author JiKra | |
*/ | |
public class Stringer { | |
private static final String EMPTY_VALUE = ""; | |
private String value; | |
/** | |
* The class constructor. | |
* | |
* @param value a value to be handled by this class. | |
* @throws NullPointerException if the value is null | |
*/ | |
public Stringer(String value) { | |
if (value == null) | |
throw new NullPointerException("value"); | |
this.value = value; | |
} | |
/** | |
* Retrieve the most left part of the value using the defined length.<br/> | |
* If the requested length is larger then the value length, return the whole value.<br/> | |
* If the requested length is 0 or smaller, return an empty value.<br/> | |
* If the value is null, throws NPE.<br/> | |
* | |
* @param value a value to be concatenated from left | |
* @param length a length of the value to be returned | |
* @return the most left part of the value | |
* @throws NullPointerException if the value is null | |
*/ | |
public static String left(String value, int length) { | |
if (length <= 0) | |
return EMPTY_VALUE; | |
if (value.length() < length) | |
return value; | |
return value.substring(0, length); | |
} | |
/** | |
* Find the position of the defined substring and retrieve the most left part of the value to it from beginning.<br/> | |
* If the defined substring is not found, position is 0, therefore return an empty value.<br/> | |
* If the defined substring is an empty value, return an empty value.<br/> | |
* If the value or delimiter is null, throws NPE.<br/> | |
* | |
* @param value a value to be concatenated from left | |
* @param delimiter a substring to return the most left part to it from beginning | |
* @return the most left part of the value to defined delimiter position | |
* @throws NullPointerException if the value or the delimiter is null | |
*/ | |
public static String left(String value, String delimiter) { | |
int index = value.indexOf(delimiter); | |
return Stringer.left(value, index); | |
} | |
/** | |
* Retrieve the most left part of the value using the defined length.<br/> | |
* If the requested length is larger then the value length, return the whole value.<br/> | |
* If the requested length is 0 or smaller, return an empty value.<br/> | |
* If the value is null, throws NPE.<br/> | |
* | |
* @param length a length of the value to be returned | |
* @return the most left part of the value | |
* @throws NullPointerException if the value is null | |
*/ | |
public Stringer left(int length) { | |
value = Stringer.left(value, length); | |
return this; | |
} | |
/** | |
* Find the position of the defined substring and retrieve the most left part of the value to it from beginning.<br/> | |
* If the defined substring is not found, position is 0, therefore return an empty value.<br/> | |
* If the defined substring is an empty value, return an empty value.<br/> | |
* If the value or delimiter is null, throws NPE.<br/> | |
* | |
* @param delimiter a substring to return the most left part to it from beginning | |
* @return the most left part of the value to defined delimiter position | |
* @throws NullPointerException if the value or the delimiter is null | |
*/ | |
public Stringer left(String delimiter) { | |
value = Stringer.left(value, delimiter); | |
return this; | |
} | |
/** | |
* Find the last position of the defined substring and retrieve the most left part of the value to it from beginning.<br/> | |
* If the defined substring is not found, position is 0, therefore return an empty value.<br/> | |
* If the defined substring is an empty value, return an empty value.<br/> | |
* If the value or delimiter is null, throws NPE.<br/> | |
* | |
* @param value a value to be concatenated from left | |
* @param delimiter a substring to return the most left part to it from beginning | |
* @return the most left part of the value to defined delimiter last position | |
* @throws NullPointerException if the value or the delimiter or value is null | |
*/ | |
public static String leftBack(String value, String delimiter) { | |
int index = value.lastIndexOf(delimiter); | |
return Stringer.left(value, index); | |
} | |
/** | |
* Find the last position of the defined substring and retrieve the most left part of the value to it from beginning.<br/> | |
* If the defined substring is not found, position is 0, therefore return an empty value.<br/> | |
* If the defined substring is an empty value, return an empty value.<br/> | |
* If the value or delimiter is null, throws NPE.<br/> | |
* | |
* @param delimiter a substring to return the most left part to it from beginning | |
* @return the most left part of the value to defined delimiter last position | |
* @throws NullPointerException if the value or the delimiter is null | |
*/ | |
public Stringer leftBack(String delimiter) { | |
value = Stringer.leftBack(value, delimiter); | |
return this; | |
} | |
/** | |
* Retrieve the most right part of the value using the defined length.<br/> | |
* If the requested length is larger then the value length, return the whole value.<br/> | |
* If the requested length is 0 or smaller, return an empty value.<br/> | |
* If the value is null, throws NPE.<br/> | |
* | |
* @param value a value to be concatenated from right | |
* @param length a length of the value to be returned | |
* @return the most right part of the value | |
* @throws NullPointerException if the value is null | |
*/ | |
public static String right(String value, int length) { | |
if (length <= 0) | |
return EMPTY_VALUE; | |
if (value.length() < length) | |
return value; | |
return value.substring(value.length() - length); | |
} | |
/** | |
* Find the position of the defined substring and retrieve the most right part of the value to it from end.<br/> | |
* If the defined substring is not found, position is 0, therefore return an empty value.<br/> | |
* If the defined substring is an empty value, return an empty value.<br/> | |
* If the value or delimiter is null, throws NPE.<br/> | |
* | |
* @param value a value to be concatenated from right | |
* @param delimiter a substring to return the most right part to it from end | |
* @return the most right part of the value to defined delimiter position | |
* @throws NullPointerException if the value or the delimiter is null | |
*/ | |
public static String right(String value, String delimiter) { | |
int index = value.indexOf(delimiter); | |
int length = delimiter.length(); | |
return value.substring(value.length() - (value.length() - (index + length))); | |
} | |
/** | |
* Retrieve the most right part of the value using the defined length.<br/> | |
* If the requested length is larger then the value length, return the whole value.<br/> | |
* If the requested length is 0 or smaller, return an empty value.<br/> | |
* If the value is null, throws NPE.<br/> | |
* | |
* @param length a length of the value to be returned | |
* @return the most right part of the value | |
* @throws NullPointerException if the value is null | |
*/ | |
public Stringer right(int length) { | |
value = Stringer.right(value, length); | |
return this; | |
} | |
/** | |
* Find the position of the defined substring and retrieve the most right part of the value to it from beginning.<br/> | |
* If the defined substring is not found, position is 0, therefore return an empty value.<br/> | |
* If the defined substring is an empty value, return an empty value.<br/> | |
* If the value or delimiter is null, throws NPE.<br/> | |
* | |
* @param delimiter a substring to return the most right part to it from beginning | |
* @return the most right part of the value to defined delimiter position | |
* @throws NullPointerException if the value or the delimiter is null | |
*/ | |
public Stringer right(String delimiter) { | |
value = Stringer.right(value, delimiter); | |
return this; | |
} | |
/** | |
* Find the first position of the defined substring and retrieve the most right part of the value to it from the end.<br/> | |
* If the defined substring is not found, position is 0, therefore return an empty value.<br/> | |
* If the defined substring is an empty value, return an empty value.<br/> | |
* If the value or delimiter is null, throws NPE.<br/> | |
* | |
* @param value a value to be concatenated from right | |
* @param delimiter a substring to return the most right part to it from the end | |
* @return the most right part of the value to defined delimiter first position | |
* @throws NullPointerException if the value or the delimiter or value is null | |
*/ | |
public static String rightBack(String value, String delimiter) { | |
int index = value.lastIndexOf(delimiter); | |
return Stringer.right(value, value.length() - index - delimiter.length()); | |
} | |
/** | |
* Find the first position of the defined substring and retrieve the most right part of the value to it from the end.<br/> | |
* If the defined substring is not found, position is 0, therefore return an empty value.<br/> | |
* If the defined substring is an empty value, return an empty value.<br/> | |
* If the value or delimiter is null, throws NPE.<br/> | |
* | |
* @param delimiter a substring to return the most right part to it from the end | |
* @return the most right part of the value to defined delimiter first position | |
* @throws NullPointerException if the value or the delimiter or value is null | |
*/ | |
public Stringer rightBack(String delimiter) { | |
value = Stringer.rightBack(value, delimiter); | |
return this; | |
} | |
/** | |
* Using index retrieve the middle part of the value by length. | |
* | |
* @param value a value to be cut from right | |
* @param index index to cut value from | |
* @param length length of the substring to be cut | |
* @return middle part of the value | |
*/ | |
public static String middle(String value, int index, int length) { | |
return value.substring(index, index + length); | |
} | |
/** | |
* Using index retrieve the middle part of the value by length. | |
* | |
* @param index index to cut value from | |
* @param length length of the substring to be cut | |
* @return middle part of the value | |
*/ | |
public Stringer middle(int index, int length) { | |
value = Stringer.middle(value, index, length); | |
return this; | |
} | |
/** | |
* Using delimiter index retrieve the middle part of the value by length. | |
* | |
* @param value a value to be cut from right | |
* @param delimiter a substring to return the substring from | |
* @param length length of the substring to be cut | |
* @return middle part of the value | |
*/ | |
public static String middle(String value, String delimiter, int length) { | |
int index = value.indexOf(delimiter); | |
return Stringer.middle(value, index, length + delimiter.length()); | |
} | |
/** | |
* Using delimiter index retrieve the middle part of the value by length. | |
* | |
* @param delimiter a substring to return the substring from | |
* @param length length of the substring to be cut | |
* @return middle part of the value | |
*/ | |
public Stringer middle(String delimiter, int length) { | |
value = Stringer.middle(value, delimiter, length); | |
return this; | |
} | |
/** | |
* Using delimiter index retrieve the middle part of the value by length. | |
* | |
* @param value a value to be cut from right | |
* @param delimiterFrom a substring to return the substring from | |
* @param delimiterTo a substring to return the substring from | |
* @return middle part of the value | |
*/ | |
public static String middle(String value, String delimiterFrom, String delimiterTo) { | |
int indexFrom = value.indexOf(delimiterFrom) + delimiterFrom.length(); | |
int indexTo = value.indexOf(delimiterTo, indexFrom); | |
int length = indexTo - indexFrom; | |
return Stringer.middle(value, indexFrom, length); | |
} | |
/** | |
* Using delimiter index retrieve the middle part of the value by length. | |
* | |
* @param delimiterFrom a substring to return the substring from | |
* @param delimiterTo a substring to return the substring from | |
* @return middle part of the value | |
*/ | |
public Stringer middle(String delimiterFrom, String delimiterTo) { | |
value = Stringer.middle(value, delimiterFrom, delimiterTo); | |
return this; | |
} | |
/** | |
* Factory method to construct Stringer class. | |
* | |
* @param value | |
* @return Stringer object | |
*/ | |
public static Stringer from(String value) { | |
return new Stringer(value); | |
} | |
public String getValue() { | |
return value; | |
} | |
public int length() { | |
return value.length(); | |
} | |
public boolean isEmpty() { | |
return value.isEmpty(); | |
} | |
public String build() { | |
return toString(); | |
} | |
@Override | |
public String toString() { | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment