Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
functionalj.lens.lenses.StringAccess Maven / Gradle / Ivy
// ============================================================================
// Copyright (c) 2017-2019 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
//
// 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 NONINFRINGEMENT. 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 functionalj.lens.lenses;
import static functionalj.functions.StrFuncs.toStr;
import static java.util.stream.Collectors.joining;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
import functionalj.function.Func1;
import functionalj.lens.lenses.java.time.LocalDateAccess;
import functionalj.lens.lenses.java.time.LocalDateTimeAccess;
import functionalj.list.FuncList;
import functionalj.result.Result;
import lombok.val;
@SuppressWarnings("javadoc")
@FunctionalInterface
public interface StringAccess
extends
ObjectAccess,
ConcreteAccess> {
@Override
public default StringAccess newAccess(Function access) {
return access::apply;
}
// Extra
public default BooleanAccess thatIsBlank() {
return booleanAccess(true, str->str.trim().isEmpty());
}
public default BooleanAccess thatIsNotBlank() {
return booleanAccess(true, str->!str.trim().isEmpty());
}
// java.lang.String
public default IntegerAccess compareToIgnoreCase(String anotherString) {
return intAccess(-1, str->str.compareToIgnoreCase(anotherString));
}
public default StringAccess concat(Object ... suffixes) {
return stringAccess("", str -> {
val eachToString = __internal__.stringFrom(str);
String suffix = Stream.of(suffixes).map(eachToString).collect(joining());
return str + suffix;
});
}
public default StringAccess prefix(Object ... prefixes) {
return stringAccess("", str -> {
val eachToString = __internal__.stringFrom(str);
String prefix = Stream.of(prefixes).map(eachToString).collect(joining());
return prefix + str;
});
}
public default StringAccess suffix(Object ... suffixes) {
return stringAccess("", str -> {
val eachToString = __internal__.stringFrom(str);
String prefix = Stream.of(suffixes).map(eachToString).collect(joining());
return str + prefix;
});
}
public default StringAccess wrapBy(Object prefix, Object suffix) {
return stringAccess("", str -> {
val eachToString = __internal__.stringFrom(str);
String prefixStr = eachToString.apply(prefix);
String suffixStr = eachToString.apply(suffix);
return prefixStr + str + suffixStr;
});
}
public default BooleanAccess thatContentEquals(CharSequence charSequence) {
return booleanAccess(false, str->str.contentEquals(charSequence));
}
public default BooleanAccess thatContains(CharSequence charSequence) {
return booleanAccess(false, str->str.contains(charSequence));
}
public default BooleanAccess thatNotContains(CharSequence charSequence) {
return booleanAccess(false, str->!str.contains(charSequence));
}
public default BooleanAccess thatContainsIgnoreCase(CharSequence charSequence) {
return booleanAccess(false, str->str.toLowerCase().contains(charSequence.toString().toLowerCase()));
}
public default BooleanAccess thatNotContainsIgnoreCase(CharSequence charSequence) {
return booleanAccess(false, str->!str.toLowerCase().contains(charSequence.toString().toLowerCase()));
}
public default BooleanAccess thatEndsWith(String suffix) {
boolean isSuffixEmpty = (suffix == null) || suffix.isEmpty();
return booleanAccess(isSuffixEmpty, str->str.endsWith(suffix));
}
public default StringAccess format(String format, Object... args) {
return stringAccess(null, str->{
val eachToString = __internal__.stringFrom(str);
val argStrs = Stream.of(args).map(eachToString).toArray();
return String.format(format, (Object[])argStrs);
});
}
public default BooleanAccess thatEqualsIgnoreCase(String anotherString) {
boolean isAnotherStringEmpty = (anotherString == null) || anotherString.isEmpty();
return booleanAccess(isAnotherStringEmpty, str->str.equalsIgnoreCase(anotherString));
}
public default IntegerAccess indexOf(int ch) {
return intAccess(-1, str->str.indexOf(ch));
}
public default IntegerAccess indexOf(int ch, int fromIndex) {
return intAccess(-1, str->str.indexOf(ch, fromIndex));
}
public default IntegerAccess indexOf(String needle) {
return intAccess(-1, str->str.indexOf(needle));
}
public default IntegerAccess indexOf(String needle, int fromIndex) {
return intAccess(-1, str->str.indexOf(needle, fromIndex));
}
public default BooleanAccess thatIsEmpty() {
return booleanAccess(true, str->str.isEmpty());
}
public default BooleanAccess thatIsNotEmpty() {
return booleanAccess(true, str->!str.isEmpty());
}
public default IntegerAccess lastIndexOf(int ch) {
return intAccess(-1, str->str.lastIndexOf(ch));
}
public default IntegerAccess lastIndexOf(int ch, int fromIndex) {
return intAccess(-1, str->str.lastIndexOf(ch, fromIndex));
}
public default IntegerAccess lastIndexOf(String needle) {
return intAccess(-1, str->str.lastIndexOf(needle));
}
public default IntegerAccess lastIndexOf(String needle, int fromIndex) {
return intAccess(-1, str->str.lastIndexOf(needle, fromIndex));
}
public default IntegerAccess length() {
return intAccess(0, str->str.length());
}
public default BooleanAccess thatMatches(String regex) {
return booleanAccess(false, str->str.matches(regex));
}
public default BooleanAccess thatMatchesIgnoreCase(String regex) {
return booleanAccess(false, str->str.toLowerCase().matches(regex.toLowerCase()));
}
public default StringAccess replace(char oldChar, char newChar) {
return stringAccess(null, str->str.replace(oldChar, newChar));
}
public default StringAccess replace(CharSequence target, CharSequence replacement) {
return stringAccess(null, str->str.replace(target, replacement));
}
public default StringAccess replaceAll(String regEx, String replacement) {
return stringAccess(null, str->str.replaceAll(regEx, replacement));
}
public default StringAccess replaceFirst(String regEx, String replacement) {
return stringAccess(null, str->str.replaceFirst(regEx, replacement));
}
public default FuncListAccess> split(String regex) {
return FuncListAccess.of(host -> {
val strValue = StringAccess.this.apply(host);
return FuncList.from(strValue.split(regex));
}, func -> StringAccess.this.newAccess(func));
}
public default BooleanAccess thatStartsWith(String prefix) {
return booleanAccess(false, str->str.startsWith(prefix));
}
public default BooleanAccess thatStartsWith(String prefix, int offset) {
return booleanAccess(false, str->str.startsWith(prefix, offset));
}
public default StringAccess substring(int beginIndex) {
return stringAccess(null, str->str.substring(beginIndex));
}
public default StringAccess substring(int beginIndex, int endIndex) {
return stringAccess(null, str->str.substring(beginIndex, endIndex));
}
public default StringAccess toLowerCase() {
return stringAccess(null, str->str.toLowerCase());
}
public default StringAccess toLowerCase(Locale locale) {
return stringAccess(null, str->str.toLowerCase(locale));
}
public default StringAccess toUpperCase() {
return stringAccess(null, str->str.toUpperCase());
}
public default StringAccess toUpperCase(Locale locale) {
return stringAccess(null, str->str.toUpperCase(locale));
}
public default StringAccess trim() {
return stringAccess(null, str->str.trim());
}
public default IntegerAccess toInteger() {
return host -> {
val valueStr = apply(host);
return Integer.parseInt(valueStr);
};
}
public default ResultAccess> asInteger() {
return ResultAccess.of(host -> {
val valueStr = apply(host);
return Result.from(()->Integer.parseInt(valueStr));
}, func -> (IntegerAccess)(func::apply));
}
public default IntegerAccess toInteger(int radix) {
return host -> {
val valueStr = apply(host);
return Integer.parseInt(valueStr, radix);
};
}
public default ResultAccess> asInteger(int radix) {
return ResultAccess.of(host -> {
val valueStr = apply(host);
return Result.from(()->Integer.parseInt(valueStr, radix));
}, func -> (IntegerAccess)(func::apply));
}
public default LongAccess toLong() {
return host -> {
val valueStr = apply(host);
return Long.parseLong(valueStr);
};
}
public default ResultAccess> asLong() {
return ResultAccess.of(host -> {
val valueStr = apply(host);
return Result.from(()->Long.parseLong(valueStr));
}, func -> (LongAccess)(func::apply));
}
public default LongAccess toLong(int radix) {
return host -> {
val valueStr = apply(host);
return Long.parseLong(valueStr, radix);
};
}
public default ResultAccess> asLong(int radix) {
return ResultAccess.of(host -> {
val valueStr = apply(host);
return Result.from(()->Long.parseLong(valueStr, radix));
}, func -> (LongAccess)(func::apply));
}
public default DoubleAccess toDouble() {
return host -> {
val valueStr = apply(host);
return Double.parseDouble(valueStr);
};
}
public default ResultAccess> asDouble() {
return ResultAccess.of(host -> {
val valueStr = apply(host);
return Result.from(()->Double.parseDouble(valueStr));
}, func -> (DoubleAccess)(func::apply));
}
public default BigIntegerAccess toBigInteger() {
return host -> {
val valueStr = apply(host);
return new BigInteger(valueStr);
};
}
public default ResultAccess> asBigInteger() {
return ResultAccess.of(host -> {
val valueStr = apply(host);
return Result.from(()->new BigInteger(valueStr));
}, func -> (BigIntegerAccess)(func::apply));
}
public default BigDecimalAccess toBigDecimal() {
return host -> {
val valueStr = apply(host);
return new BigDecimal(valueStr);
};
}
public default ResultAccess> asBigDecimal() {
return ResultAccess.of(host -> {
val valueStr = apply(host);
return Result.from(()->new BigDecimal(valueStr));
}, func -> (BigDecimalAccess)(func::apply));
}
public default LocalDateAccess toLocalDate() {
return host -> {
val valueStr = apply(host);
return LocalDate.parse(valueStr);
};
}
public default ResultAccess> asLocalDate() {
return ResultAccess.of(host -> {
val valueStr = apply(host);
return Result.from(()->LocalDate.parse(valueStr));
}, func -> (LocalDateAccess)(func::apply));
}
public default LocalDateAccess toLocalDate(DateTimeFormatter formatter) {
return host -> {
val valueStr = apply(host);
return LocalDate.parse(valueStr, formatter);
};
}
public default ResultAccess> asLocalDate(DateTimeFormatter formatter) {
return ResultAccess.of(host -> {
val valueStr = apply(host);
return Result.from(()->LocalDate.parse(valueStr, formatter));
}, func -> (LocalDateAccess)(func::apply));
}
public default LocalDateTimeAccess toLocalDateTime() {
return host -> {
val valueStr = apply(host);
return LocalDateTime.parse(valueStr);
};
}
public default ResultAccess> asLocalDateTime() {
return ResultAccess.of(host -> {
val valueStr = apply(host);
return Result.from(()->LocalDateTime.parse(valueStr));
}, func -> (LocalDateTimeAccess)(func::apply));
}
public default LocalDateTimeAccess toLocalDateTime(DateTimeFormatter formatter) {
return host -> {
val valueStr = apply(host);
return LocalDateTime.parse(valueStr, formatter);
};
}
public default ResultAccess> asLocalDateTime(DateTimeFormatter formatter) {
return ResultAccess.of(host -> {
val valueStr = apply(host);
return Result.from(()->LocalDateTime.parse(valueStr, formatter));
}, func -> (LocalDateTimeAccess)(func::apply));
}
public static StringAccess $(Object ... objs) {
return str -> {
val eachToString = __internal__.stringFrom(str);
return Stream.of(objs).map(eachToString).collect(joining());
};
}
public static final class __internal__ {
private static Func1 stringFrom(String str) {
return each -> stringFrom(each, str);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private static String stringFrom(Object each, String str) {
if (each instanceof Supplier) {
Supplier supplier = (Supplier)each;
Object newValue = supplier.get();
return toStr(newValue);
}
if (each instanceof Function) {
Function function = (Function)each;
Object newValue = function.apply(str);
return toStr(newValue);
}
return toStr(each);
}
}
}