// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.tools;
import static org.openstreetmap.josm.tools.I18n.marktr;
import static org.openstreetmap.josm.tools.I18n.tr;
import static org.openstreetmap.josm.tools.I18n.trn;
import java.awt.Color;
import java.awt.Font;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.AccessibleObject;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.AccessController;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivilegedAction;
import java.text.Bidi;
import java.text.MessageFormat;
import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Basic utils, that can be useful in different parts of the program.
*/
public final class Utils {
/** Pattern matching white spaces */
public static final Pattern WHITE_SPACES_PATTERN = Pattern.compile("\\s+");
private static final int MILLIS_OF_SECOND = 1000;
private static final int MILLIS_OF_MINUTE = 60000;
private static final int MILLIS_OF_HOUR = 3600000;
private static final int MILLIS_OF_DAY = 86400000;
/**
* A list of all characters allowed in URLs
*/
public static final String URL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=%";
private static final char[] DEFAULT_STRIP = {'\u200B', '\uFEFF'};
private static final String[] SIZE_UNITS = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
private Utils() {
// Hide default constructor for utils classes
}
/**
* Tests whether {@code predicate} applies to at least one element from {@code collection}.
*
* @param type of items
* @param collection the collection
* @param predicate the predicate
* @return {@code true} if {@code predicate} applies to at least one element from {@code collection}
* @deprecated use {@link Stream#anyMatch(java.util.function.Predicate)} instead.
*/
@Deprecated
public static boolean exists(Iterable extends T> collection, Predicate super T> predicate) {
for (T item : collection) {
if (predicate.evaluate(item)) {
return true;
}
}
return false;
}
/**
* Tests whether {@code predicate} applies to all elements from {@code collection}.
*
* @param type of items
* @param collection the collection
* @param predicate the predicate
* @return {@code true} if {@code predicate} applies to all elements from {@code collection}
* @deprecated use {@link Stream#allMatch(java.util.function.Predicate)} instead.
*/
@Deprecated
public static boolean forAll(Iterable extends T> collection, Predicate super T> predicate) {
return !exists(collection, Predicates.not(predicate));
}
/**
* Checks if an item that is an instance of clazz exists in the collection
* @param The collection type.
* @param collection The collection
* @param clazz The class to search for.
* @return true if that item exists in the collection.
*/
public static boolean exists(Iterable collection, Class extends T> clazz) {
return exists(collection, Predicates.isInstanceOf(clazz));
}
/**
* Finds the first item in the iterable for which the predicate matches.
* @param The iterable type.
* @param collection The iterable to search in.
* @param predicate The predicate to match
* @return the item or null if there was not match.
*/
public static T find(Iterable extends T> collection, Predicate super T> predicate) {
for (T item : collection) {
if (predicate.evaluate(item)) {
return item;
}
}
return null;
}
/**
* Finds the first item in the iterable which is of the given type.
* @param The iterable type.
* @param collection The iterable to search in.
* @param clazz The class to search for.
* @return the item or null if there was not match.
*/
@SuppressWarnings("unchecked")
public static T find(Iterable extends Object> collection, Class extends T> clazz) {
return (T) find(collection, Predicates.