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.
com.github.underscore.U Maven / Gradle / Ivy
/*
* The MIT License (MIT)
*
* Copyright 2015-2024 Valentyn Kolesnikov
*
* 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 com.github.underscore;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.zip.GZIPInputStream;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.NodeList;
@SuppressWarnings({
"java:S135",
"java:S1168",
"java:S3655",
"java:S3740",
"java:S3776",
"java:S4423",
"java:S4830",
"java:S5843",
"java:S5996",
"java:S5998"
})
public class U extends Underscore {
private static final int DEFAULT_TRUNC_LENGTH = 30;
private static final String DEFAULT_TRUNC_OMISSION = "...";
private static final java.util.regex.Pattern RE_LATIN_1 =
java.util.regex.Pattern.compile("[\\xc0-\\xd6\\xd8-\\xde\\xdf-\\xf6\\xf8-\\xff]");
private static final java.util.regex.Pattern RE_PROP_NAME =
java.util.regex.Pattern.compile(
"[^.\\[\\]]+|\\[(?:(-?\\d+(?:\\.\\d+)?)|([\"'])((?:(?!\2)\\[^\\]|\\.)*?)\2)\\]|"
+ "(?=(\\.|\\[\\])(?:\4|$))");
private static final Map DEBURRED_LETTERS = new LinkedHashMap<>();
private static final Map> DEFAULT_HEADER_FIELDS = new HashMap<>();
private static final Set SUPPORTED_HTTP_METHODS =
new HashSet<>(Arrays.asList("GET", "POST", "PUT", "DELETE"));
private static final int BUFFER_LENGTH_1024 = 1024;
private static final int RESPONSE_CODE_400 = 400;
private static final String ROOT = "root";
private static final String UPPER = "[A-Z\\xc0-\\xd6\\xd8-\\xde\\u0400-\\u04FF]";
private static final String LOWER = "[a-z\\xdf-\\xf6\\xf8-\\xff]+";
private static final String SELF_CLOSING = "-self-closing";
private static final String NIL_KEY = "-nil";
private static final String OMIT_XML_DECL = "#omit-xml-declaration";
private static final java.util.regex.Pattern RE_WORDS =
java.util.regex.Pattern.compile(
UPPER
+ "+(?="
+ UPPER
+ LOWER
+ ")|"
+ UPPER
+ "?"
+ LOWER
+ "|"
+ UPPER
+ "+|\\d+");
static {
String[] deburredLetters =
new String[] {
"\u00c0", "A", "\u00c1", "A", "\u00c2", "A", "\u00c3", "A", "\u00c4", "A",
"\u00c5", "A", "\u00e0", "a", "\u00e1", "a", "\u00e2", "a", "\u00e3", "a",
"\u00e4", "a", "\u00e5", "a", "\u00c7", "C", "\u00e7", "c", "\u00d0", "D",
"\u00f0", "d", "\u00c8", "E", "\u00c9", "E", "\u00ca", "E", "\u00cb", "E",
"\u00e8", "e", "\u00e9", "e", "\u00ea", "e", "\u00eb", "e", "\u00cC", "I",
"\u00cd", "I", "\u00ce", "I", "\u00cf", "I", "\u00eC", "i", "\u00ed", "i",
"\u00ee", "i", "\u00ef", "i", "\u00d1", "N", "\u00f1", "n", "\u00d2", "O",
"\u00d3", "O", "\u00d4", "O", "\u00d5", "O", "\u00d6", "O", "\u00d8", "O",
"\u00f2", "o", "\u00f3", "o", "\u00f4", "o", "\u00f5", "o", "\u00f6", "o",
"\u00f8", "o", "\u00d9", "U", "\u00da", "U", "\u00db", "U", "\u00dc", "U",
"\u00f9", "u", "\u00fa", "u", "\u00fb", "u", "\u00fc", "u", "\u00dd", "Y",
"\u00fd", "y", "\u00ff", "y", "\u00c6", "Ae", "\u00e6", "ae", "\u00de", "Th",
"\u00fe", "th", "\u00df", "ss"
};
for (int index = 0; index < deburredLetters.length; index += 2) {
DEBURRED_LETTERS.put(deburredLetters[index], deburredLetters[index + 1]);
}
DEFAULT_HEADER_FIELDS.put(
"Content-Type", Arrays.asList("application/json", "charset=utf-8"));
}
public enum XmlToJsonMode {
REPLACE_SELF_CLOSING_WITH_NULL,
REPLACE_SELF_CLOSING_WITH_STRING,
REPLACE_EMPTY_VALUE_WITH_NULL,
REPLACE_EMPTY_TAG_WITH_NULL,
REPLACE_EMPTY_TAG_WITH_STRING,
REMOVE_FIRST_LEVEL,
WITHOUT_NAMESPACES,
REPLACE_MINUS_WITH_AT,
REPLACE_EMPTY_TAG_WITH_NULL_AND_MINUS_WITH_AT
}
public enum JsonToXmlMode {
FORCE_ATTRIBUTE_USAGE,
DEFINE_ROOT_NAME,
REPLACE_NULL_WITH_EMPTY_VALUE,
REPLACE_EMPTY_STRING_WITH_EMPTY_VALUE,
ADD_ROOT,
REMOVE_ARRAY_ATTRIBUTE,
REMOVE_ATTRIBUTES
}
public U(final Iterable iterable) {
super(iterable);
}
public U(final String string) {
super(string);
}
public static class Chain extends Underscore.Chain {
public Chain(final T item) {
super(item);
}
public Chain(final List list) {
super(list);
}
public Chain(final Map map) {
super(map);
}
@Override
public Chain first() {
return new Chain<>(Underscore.first(value()));
}
@Override
public Chain first(int n) {
return new Chain<>(Underscore.first(value(), n));
}
@Override
public Chain firstOrNull() {
return new Chain<>(Underscore.firstOrNull(value()));
}
@Override
public Chain firstOrNull(final Predicate pred) {
return new Chain<>(Underscore.firstOrNull(value(), pred));
}
@Override
public Chain initial() {
return new Chain<>(Underscore.initial(value()));
}
@Override
public Chain initial(int n) {
return new Chain<>(Underscore.initial(value(), n));
}
@Override
public Chain last() {
return new Chain<>(Underscore.last(value()));
}
@Override
public Chain last(int n) {
return new Chain<>(Underscore.last(value(), n));
}
@Override
public Chain lastOrNull() {
return new Chain<>(Underscore.lastOrNull(value()));
}
@Override
public Chain lastOrNull(final Predicate pred) {
return new Chain<>(Underscore.lastOrNull(value(), pred));
}
@Override
public Chain rest() {
return new Chain<>(Underscore.rest(value()));
}
@Override
public Chain rest(int n) {
return new Chain<>(Underscore.rest(value(), n));
}
@Override
public Chain compact() {
return new Chain<>(Underscore.compact(value()));
}
@Override
public Chain compact(final T falsyValue) {
return new Chain<>(Underscore.compactList(value(), falsyValue));
}
@Override
public Chain flatten() {
return new Chain<>(Underscore.flatten(value()));
}
@Override
public Chain map(final Function super T, F> func) {
return new Chain<>(Underscore.map(value(), func));
}
@Override
public Chain mapMulti(final BiConsumer super T, ? super Consumer> mapper) {
return new Chain<>(Underscore.mapMulti(value(), mapper));
}
@Override
public Chain mapIndexed(final BiFunction func) {
return new Chain<>(Underscore.mapIndexed(value(), func));
}
@Override
public Chain filter(final Predicate pred) {
return new Chain<>(Underscore.filter(value(), pred));
}
@Override
public Chain filterIndexed(final PredicateIndexed pred) {
return new Chain<>(Underscore.filterIndexed(value(), pred));
}
@Override
public Chain rejectIndexed(final PredicateIndexed pred) {
return new Chain<>(Underscore.rejectIndexed(value(), pred));
}
@Override
public Chain reject(final Predicate pred) {
return new Chain<>(Underscore.reject(value(), pred));
}
@Override
public Chain filterFalse(final Predicate pred) {
return new Chain<>(Underscore.filterFalse(value(), pred));
}
@Override
public Chain reduce(final BiFunction func, final F zeroElem) {
return new Chain<>(Underscore.reduce(value(), func, zeroElem));
}
@Override
public Chain> reduce(final BinaryOperator func) {
return new Chain<>(Underscore.reduce(value(), func));
}
@Override
public Chain reduceRight(final BiFunction func, final F zeroElem) {
return new Chain<>(Underscore.reduceRight(value(), func, zeroElem));
}
@Override
public Chain> reduceRight(final BinaryOperator func) {
return new Chain<>(Underscore.reduceRight(value(), func));
}
@Override
public Chain> find(final Predicate pred) {
return new Chain<>(Underscore.find(value(), pred));
}
@Override
public Chain> findLast(final Predicate pred) {
return new Chain<>(Underscore.findLast(value(), pred));
}
@Override
@SuppressWarnings("unchecked")
public Chain max() {
return new Chain<>(Underscore.max((Collection) value()));
}
@Override
public > Chain max(final Function func) {
return new Chain<>(Underscore.max(value(), func));
}
@Override
@SuppressWarnings("unchecked")
public Chain min() {
return new Chain<>(Underscore.min((Collection) value()));
}
@Override
public > Chain min(final Function func) {
return new Chain<>(Underscore.min(value(), func));
}
@Override
@SuppressWarnings("unchecked")
public Chain sort() {
return new Chain<>(Underscore.sort((List) value()));
}
@Override
@SuppressWarnings("unchecked")
public > Chain sortWith(final Comparator comparator) {
return new Chain<>(Underscore.sortWith((List) value(), comparator));
}
@Override
public > Chain sortBy(final Function func) {
return new Chain<>(Underscore.sortBy(value(), func));
}
@Override
@SuppressWarnings("unchecked")
public Chain> sortBy(final K key) {
return new Chain<>(Underscore.sortBy((List>) value(), key));
}
@Override
public Chain>> groupBy(final Function func) {
return new Chain<>(Underscore.groupBy(value(), func));
}
@Override
public Chain> associateBy(final Function func) {
return new Chain<>(Underscore.associateBy(value(), func));
}
@Override
public Chain>> groupBy(
final Function func, final BinaryOperator binaryOperator) {
return new Chain<>(Underscore.groupBy(value(), func, binaryOperator));
}
@Override
public Chain>> indexBy(final String property) {
return new Chain<>(Underscore.indexBy(value(), property));
}
@Override
public Chain> countBy(final Function func) {
return new Chain<>(Underscore.countBy(value(), func));
}
@Override
public Chain> countBy() {
return new Chain<>(Underscore.countBy(value()));
}
@Override
public Chain shuffle() {
return new Chain<>(Underscore.shuffle(value()));
}
@Override
public Chain sample() {
return new Chain<>(Underscore.sample(value()));
}
@Override
public Chain sample(final int howMany) {
return new Chain<>(newArrayList(Underscore.sample(value(), howMany)));
}
@Override
public Chain tap(final Consumer func) {
Underscore.tap(value(), func);
return new Chain<>(value());
}
@Override
public Chain forEach(final Consumer func) {
Underscore.forEach(value(), func);
return new Chain<>(value());
}
@Override
public Chain forEachRight(final Consumer func) {
Underscore.forEachRight(value(), func);
return new Chain<>(value());
}
@Override
public Chain every(final Predicate pred) {
return new Chain<>(Underscore.every(value(), pred));
}
@Override
public Chain some(final Predicate pred) {
return new Chain<>(Underscore.some(value(), pred));
}
@Override
public Chain count(final Predicate pred) {
return new Chain<>(Underscore.count(value(), pred));
}
@Override
public Chain contains(final T elem) {
return new Chain<>(Underscore.contains(value(), elem));
}
@Override
public Chain containsWith(final T elem) {
return new Chain<>(Underscore.containsWith(value(), elem));
}
@Override
public Chain invoke(final String methodName, final List args) {
return new Chain<>(Underscore.invoke(value(), methodName, args));
}
@Override
public Chain invoke(final String methodName) {
return new Chain<>(Underscore.invoke(value(), methodName));
}
@Override
public Chain pluck(final String propertyName) {
return new Chain<>(Underscore.pluck(value(), propertyName));
}
@Override
public Chain where(final List> properties) {
return new Chain<>(Underscore.where(value(), properties));
}
@Override
public Chain> findWhere(final List> properties) {
return new Chain<>(Underscore.findWhere(value(), properties));
}
@Override
public Chain uniq() {
return new Chain<>(Underscore.uniq(value()));
}
@Override
public Chain uniq(final Function func) {
return new Chain<>(newArrayList(Underscore.uniq(value(), func)));
}
@Override
public Chain distinct() {
return new Chain<>(Underscore.uniq(value()));
}
@Override
@SuppressWarnings("unchecked")
public Chain distinctBy(final Function func) {
return new Chain<>(newArrayList((Iterable) Underscore.uniq(value(), func)));
}
@Override
@SuppressWarnings("unchecked")
public Chain union(final List... lists) {
return new Chain<>(Underscore.union(value(), lists));
}
@Override
@SuppressWarnings("unchecked")
public Chain intersection(final List... lists) {
return new Chain<>(Underscore.intersection(value(), lists));
}
@Override
@SuppressWarnings("unchecked")
public Chain difference(final List... lists) {
return new Chain<>(Underscore.difference(value(), lists));
}
@Override
public Chain range(final int stop) {
return new Chain<>(Underscore.range(stop));
}
@Override
public Chain range(final int start, final int stop) {
return new Chain<>(Underscore.range(start, stop));
}
@Override
public Chain range(final int start, final int stop, final int step) {
return new Chain<>(Underscore.range(start, stop, step));
}
@Override
public Chain> chunk(final int size) {
return new Chain<>(Underscore.chunk(value(), size, size));
}
@Override
public Chain> chunk(final int size, final int step) {
return new Chain<>(Underscore.chunk(value(), size, step));
}
@Override
public Chain> chunkFill(final int size, final T fillValue) {
return new Chain<>(Underscore.chunkFill(value(), size, size, fillValue));
}
@Override
public Chain> chunkFill(final int size, final int step, final T fillValue) {
return new Chain<>(Underscore.chunkFill(value(), size, step, fillValue));
}
@Override
public Chain cycle(final int times) {
return new Chain<>(Underscore.cycle(value(), times));
}
@Override
public Chain interpose(final T element) {
return new Chain<>(Underscore.interpose(value(), element));
}
@Override
public Chain interposeByList(final Iterable interIter) {
return new Chain<>(Underscore.interposeByList(value(), interIter));
}
@Override
@SuppressWarnings("unchecked")
public Chain concat(final List... lists) {
return new Chain<>(Underscore.concat(value(), lists));
}
@Override
public Chain slice(final int start) {
return new Chain<>(Underscore.slice(value(), start));
}
@Override
public Chain slice(final int start, final int end) {
return new Chain<>(Underscore.slice(value(), start, end));
}
public Chain> set(final String path, Object value) {
U.set(map(), path, value);
return new Chain<>(map());
}
public Chain> set(final List paths, Object value) {
U.set(map(), paths, value);
return new Chain<>(map());
}
@Override
public Chain reverse() {
return new Chain<>(Underscore.reverse(value()));
}
@Override
public Chain join() {
return new Chain<>(Underscore.join(value()));
}
@Override
public Chain join(final String separator) {
return new Chain<>(Underscore.join(value(), separator));
}
@Override
public Chain skip(final int numberToSkip) {
return new Chain<>(value().subList(numberToSkip, value().size()));
}
@Override
public Chain limit(final int size) {
return new Chain<>(value().subList(0, size));
}
@Override
@SuppressWarnings("unchecked")
public Chain> toMap() {
return new Chain<>(Underscore.toMap((Iterable>) value()));
}
public Chain drop() {
return new Chain<>(Underscore.drop(value()));
}
public Chain drop(final Integer n) {
return new Chain<>(Underscore.drop(value(), n));
}
public Chain dropRight() {
return new Chain<>(U.dropRight(value()));
}
public Chain dropRight(final Integer n) {
return new Chain<>(U.dropRight(value(), n));
}
public Chain dropWhile(final Predicate pred) {
return new Chain<>(U.dropWhile(value(), pred));
}
public Chain dropRightWhile(final Predicate pred) {
return new Chain<>(U.dropRightWhile(value(), pred));
}
@SuppressWarnings("unchecked")
public Chain fill(final Object value) {
return new Chain<>(U.fill((List) value(), value));
}
@SuppressWarnings("unchecked")
public Chain fill(final Object value, final Integer start, final Integer end) {
return new Chain<>(U.fill((List) value(), value, start, end));
}
public Chain flattenDeep() {
return new Chain<>(U.flattenDeep(value()));
}
@SuppressWarnings("unchecked")
public Chain pull(final Object... values) {
return new Chain<>(U.pull((List) value(), values));
}
@SuppressWarnings("unchecked")
public Chain pullAt(final Integer... indexes) {
return new Chain<>(U.pullAt((List) value(), indexes));
}
public Chain remove(final Predicate pred) {
return new Chain<>(U.remove(value(), pred));
}
public Chain take() {
return new Chain<>(U.take(value()));
}
public Chain takeRight() {
return new Chain<>(U.takeRight(value()));
}
public Chain take(final Integer n) {
return new Chain<>(U.take(value(), n));
}
public Chain takeRight(final Integer n) {
return new Chain<>(U.takeRight(value(), n));
}
public Chain takeWhile(final Predicate pred) {
return new Chain<>(U.takeWhile(value(), pred));
}
public Chain takeRightWhile(final Predicate pred) {
return new Chain<>(U.takeRightWhile(value(), pred));
}
@SuppressWarnings("unchecked")
public Chain xor(final List list) {
return new Chain<>(U.xor(value(), list));
}
public Chain at(final Integer... indexes) {
return new Chain<>(U.at(value(), indexes));
}
@SuppressWarnings("unchecked")
public Chain sum() {
return new Chain<>(U.sum((List) value()));
}
public Chain sum(final Function func) {
return new Chain<>(U.sum(value(), func));
}
@SuppressWarnings("unchecked")
public Chain mean() {
return new Chain<>(U.mean((List) value()));
}
@SuppressWarnings("unchecked")
public Chain median() {
return new Chain<>(U.median((List) value()));
}
public Chain camelCase() {
return new Chain<>(U.camelCase((String) item()));
}
public Chain lowerFirst() {
return new Chain<>(U.lowerFirst((String) item()));
}
public Chain upperFirst() {
return new Chain<>(U.upperFirst((String) item()));
}
public Chain capitalize() {
return new Chain<>(U.capitalize((String) item()));
}
public Chain deburr() {
return new Chain<>(U.deburr((String) item()));
}
public Chain endsWith(final String target) {
return new Chain<>(U.endsWith((String) item(), target));
}
public Chain endsWith(final String target, final Integer position) {
return new Chain<>(U.endsWith((String) item(), target, position));
}
public Chain kebabCase() {
return new Chain<>(U.kebabCase((String) item()));
}
public Chain repeat(final int length) {
return new Chain<>(U.repeat((String) item(), length));
}
public Chain pad(final int length) {
return new Chain<>(U.pad((String) item(), length));
}
public Chain pad(final int length, final String chars) {
return new Chain<>(U.pad((String) item(), length, chars));
}
public Chain padStart(final int length) {
return new Chain<>(U.padStart((String) item(), length));
}
public Chain padStart(final int length, final String chars) {
return new Chain<>(U.padStart((String) item(), length, chars));
}
public Chain padEnd(final int length) {
return new Chain<>(U.padEnd((String) item(), length));
}
public Chain padEnd(final int length, final String chars) {
return new Chain<>(U.padEnd((String) item(), length, chars));
}
public Chain snakeCase() {
return new Chain<>(U.snakeCase((String) item()));
}
public Chain startCase() {
return new Chain<>(U.startCase((String) item()));
}
public Chain startsWith(final String target) {
return new Chain<>(U.startsWith((String) item(), target));
}
public Chain startsWith(final String target, final Integer position) {
return new Chain<>(U.startsWith((String) item(), target, position));
}
public Chain trim() {
return new Chain<>(U.trim((String) item()));
}
public Chain trim(final String chars) {
return new Chain<>(U.trim((String) item(), chars));
}
public Chain trimStart() {
return new Chain<>(U.trimStart((String) item()));
}
public Chain trimStart(final String chars) {
return new Chain<>(U.trimStart((String) item(), chars));
}
public Chain trimEnd() {
return new Chain<>(U.trimEnd((String) item()));
}
public Chain trunc() {
return new Chain<>(U.trunc((String) item()));
}
public Chain trunc(final int length) {
return new Chain<>(U.trunc((String) item(), length));
}
public Chain trimEnd(final String chars) {
return new Chain<>(U.trimEnd((String) item(), chars));
}
public Chain uncapitalize() {
return new Chain<>(U.uncapitalize((String) item()));
}
public Chain words() {
return new Chain<>(U.words((String) item()));
}
public Chain toJson() {
return new Chain<>(Json.toJson(value()));
}
public Chain fromJson() {
return new Chain<>(Json.fromJson((String) item()));
}
public Chain toXml() {
return new Chain<>(Xml.toXml(value()));
}
public Chain fromXml() {
return new Chain<>(Xml.fromXml((String) item()));
}
public Chain fetch() {
return new Chain<>(U.fetch((String) item()).text());
}
public Chain fetch(final String method, final String body) {
return new Chain<>(U.fetch((String) item(), method, body).text());
}
public Chain> createPermutationWithRepetition(final int permutationLength) {
return new Chain<>(U.createPermutationWithRepetition(value(), permutationLength));
}
public Chain xmlToJson() {
return new Chain<>(U.xmlToJson((String) item()));
}
public Chain jsonToXml() {
return new Chain<>(U.jsonToXml((String) item()));
}
}
public static Chain chain(final String item) {
return new U.Chain<>(item);
}
public static Chain chain(final List list) {
return new U.Chain<>(list);
}
public static Chain> chain(final Map map) {
return new U.Chain<>(map);
}
public static Chain chain(final Iterable iterable) {
return new U.Chain<>(newArrayList(iterable));
}
public static Chain chain(final Iterable iterable, int size) {
return new U.Chain<>(newArrayList(iterable, size));
}
@SuppressWarnings("unchecked")
public static