diff --git a/bundles/org.jkiss.utils/META-INF/MANIFEST.MF b/bundles/org.jkiss.utils/META-INF/MANIFEST.MF
deleted file mode 100644
index 50c633dd37fd6..0000000000000
--- a/bundles/org.jkiss.utils/META-INF/MANIFEST.MF
+++ /dev/null
@@ -1,17 +0,0 @@
-Manifest-Version: 1.0
-Bundle-ManifestVersion: 2
-Bundle-Name: DBeaver Utils
-Bundle-Vendor: DBeaver Corp
-Bundle-SymbolicName: org.jkiss.utils
-Bundle-Version: 2.1.207.qualifier
-Bundle-Release-Date: 20240205
-Bundle-RequiredExecutionEnvironment: JavaSE-17
-Require-Bundle: com.google.gson
-Export-Package: org.jkiss.code,
- org.jkiss.utils,
- org.jkiss.utils.csv,
- org.jkiss.utils.io,
- org.jkiss.utils.rest,
- org.jkiss.utils.time,
- org.jkiss.utils.xml
-Automatic-Module-Name: org.jkiss.utils
diff --git a/bundles/org.jkiss.utils/build.properties b/bundles/org.jkiss.utils/build.properties
deleted file mode 100644
index 3335c5d91cbc6..0000000000000
--- a/bundles/org.jkiss.utils/build.properties
+++ /dev/null
@@ -1,3 +0,0 @@
-source.. = src/
-output.. = target/classes/
-bin.includes = META-INF/,.
diff --git a/bundles/org.jkiss.utils/pom.xml b/bundles/org.jkiss.utils/pom.xml
deleted file mode 100644
index 94e81311b7249..0000000000000
--- a/bundles/org.jkiss.utils/pom.xml
+++ /dev/null
@@ -1,14 +0,0 @@
-
-
- 4.0.0
-
- org.jkiss.dbeaver
- bundles
- 1.0.0-SNAPSHOT
- ../
-
- org.jkiss.utils
- 2.1.207-SNAPSHOT
- eclipse-plugin
-
diff --git a/bundles/org.jkiss.utils/src/org/jkiss/code/NotNull.java b/bundles/org.jkiss.utils/src/org/jkiss/code/NotNull.java
deleted file mode 100644
index bff9bf18a575d..0000000000000
--- a/bundles/org.jkiss.utils/src/org/jkiss/code/NotNull.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * DBeaver - Universal Database Manager
- * Copyright (C) 2010-2024 DBeaver Corp and others
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.jkiss.code;
-
-/**
- * NotNull annotation
- */
-public @interface NotNull {
-
-}
diff --git a/bundles/org.jkiss.utils/src/org/jkiss/code/Nullable.java b/bundles/org.jkiss.utils/src/org/jkiss/code/Nullable.java
deleted file mode 100644
index 88a5ebf0de304..0000000000000
--- a/bundles/org.jkiss.utils/src/org/jkiss/code/Nullable.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * DBeaver - Universal Database Manager
- * Copyright (C) 2010-2024 DBeaver Corp and others
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.jkiss.code;
-
-/**
- * Nullable annotation
- */
-public @interface Nullable {
-
-}
diff --git a/bundles/org.jkiss.utils/src/org/jkiss/utils/AlphanumericComparator.java b/bundles/org.jkiss.utils/src/org/jkiss/utils/AlphanumericComparator.java
deleted file mode 100644
index e1c37a536846b..0000000000000
--- a/bundles/org.jkiss.utils/src/org/jkiss/utils/AlphanumericComparator.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * DBeaver - Universal Database Manager
- * Copyright (C) 2010-2024 DBeaver Corp and others
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.jkiss.utils;
-
-import org.jkiss.code.NotNull;
-
-import java.util.Comparator;
-
-/**
- * A comparator for comparing two strings lexicographically, treating them as sequences of alphanumeric characters.
- *
- * This comparator compares strings based on their alphanumeric content. It considers
- * the characters in the strings as a sequence of alphanumeric characters (letters and digits)
- * and compares them lexicographically. The comparison is case-insensitive.
- */
-public class AlphanumericComparator implements Comparator {
- private static final AlphanumericComparator INSTANCE = new AlphanumericComparator();
-
- private AlphanumericComparator() {
- // prevents instantiation
- }
-
- @NotNull
- public static AlphanumericComparator getInstance() {
- return INSTANCE;
- }
-
- @Override
- public int compare(CharSequence o1, CharSequence o2) {
- final int len1 = o1.length();
- final int len2 = o2.length();
-
- int i = 0;
- int j = 0;
-
- while (i < len1 && j < len2) {
- final char ch1 = Character.toUpperCase(o1.charAt(i));
- final char ch2 = Character.toUpperCase(o2.charAt(i));
-
- if (Character.isDigit(ch1) && Character.isDigit(ch2)) {
- int num1 = 0;
- int num2 = 0;
-
- while (i < len1 && Character.isDigit(o1.charAt(i))) {
- num1 = num1 * 10 + Character.digit(o1.charAt(i), 10);
- i += 1;
- }
-
- while (j < len2 && Character.isDigit(o2.charAt(j))) {
- num2 = num2 * 10 + Character.digit(o2.charAt(j), 10);
- j += 1;
- }
-
- if (num1 != num2) {
- return num1 - num2;
- }
- } else {
- if (ch1 != ch2) {
- return ch1 - ch2;
- }
-
- i += 1;
- j += 1;
- }
- }
-
- return len1 - len2;
- }
-}
diff --git a/bundles/org.jkiss.utils/src/org/jkiss/utils/ArgumentTokenizer.java b/bundles/org.jkiss.utils/src/org/jkiss/utils/ArgumentTokenizer.java
deleted file mode 100644
index 46e12bd0d03d7..0000000000000
--- a/bundles/org.jkiss.utils/src/org/jkiss/utils/ArgumentTokenizer.java
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
- * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software is Open Source Initiative approved Open Source Software.
- * Open Source Initative Approved is a trademark of the Open Source Initiative.
- *
- * This file is part of DrJava. Download the current version of this project
- * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
- */
-
-package org.jkiss.utils;
-
-import java.util.LinkedList;
-import java.util.List;
-
-/**
- * Utility class which can tokenize a String into a list of String arguments,
- * with behavior similar to parsing command line arguments to a program.
- * Quoted Strings are treated as single arguments, and escaped characters
- * are translated so that the tokenized arguments have the same meaning.
- * Since all methods are static, the class is declared abstract to prevent
- * instantiation.
- *
- * @version $Id$
- */
-public abstract class ArgumentTokenizer {
- private static final int NO_TOKEN_STATE = 0;
- private static final int NORMAL_TOKEN_STATE = 1;
- private static final int SINGLE_QUOTE_STATE = 2;
- private static final int DOUBLE_QUOTE_STATE = 3;
-
- /**
- * Tokenizes the given String into String tokens
- *
- * @param arguments A String containing one or more command-line style arguments to be tokenized.
- * @return A list of parsed and properly escaped arguments.
- */
- public static List tokenize(String arguments) {
- return tokenize(arguments, false);
- }
-
- /**
- * Tokenizes the given String into String tokens.
- *
- * @param arguments A String containing one or more command-line style arguments to be tokenized.
- * @param stringify whether or not to include escape special characters
- * @return A list of parsed and properly escaped arguments.
- */
- public static List tokenize(String arguments, boolean stringify) {
-
- LinkedList argList = new LinkedList();
- StringBuilder currArg = new StringBuilder();
- boolean escaped = false;
- int state = NO_TOKEN_STATE; // start in the NO_TOKEN_STATE
- int len = arguments.length();
-
- // Loop over each character in the string
- for (int i = 0; i < len; i++) {
- char c = arguments.charAt(i);
- if (escaped) {
- // Escaped state: just append the next character to the current arg.
- escaped = false;
- if (c != 'n' && c != 't' && c != '\\') {
- // This was just a regular slash
- currArg.append('\\');
- }
- currArg.append(c);
- } else {
- switch (state) {
- case SINGLE_QUOTE_STATE:
- if (c == '\'') {
- // Seen the close quote; continue this arg until whitespace is seen
- state = NORMAL_TOKEN_STATE;
- } else {
- currArg.append(c);
- }
- break;
- case DOUBLE_QUOTE_STATE:
- if (c == '"') {
- // Seen the close quote; continue this arg until whitespace is seen
- state = NORMAL_TOKEN_STATE;
- } else if (c == '\\') {
- // Look ahead, and only escape quotes or backslashes
- i++;
- char next = arguments.charAt(i);
- if (next == '"' || next == '\\') {
- currArg.append(next);
- } else {
- currArg.append(c);
- currArg.append(next);
- }
- } else {
- currArg.append(c);
- }
- break;
-// case NORMAL_TOKEN_STATE:
-// if (Character.isWhitespace(c)) {
-// // Whitespace ends the token; start a new one
-// argList.add(currArg.toString());
-// currArg = new StringBuffer();
-// state = NO_TOKEN_STATE;
-// }
-// else if (c == '\\') {
-// // Backslash in a normal token: escape the next character
-// escaped = true;
-// }
-// else if (c == '\'') {
-// state = SINGLE_QUOTE_STATE;
-// }
-// else if (c == '"') {
-// state = DOUBLE_QUOTE_STATE;
-// }
-// else {
-// currArg.append(c);
-// }
-// break;
- case NO_TOKEN_STATE:
- case NORMAL_TOKEN_STATE:
- switch (c) {
- case '\\':
- escaped = true;
- state = NORMAL_TOKEN_STATE;
- break;
- case '\'':
- state = SINGLE_QUOTE_STATE;
- break;
- case '"':
- state = DOUBLE_QUOTE_STATE;
- break;
- default:
- if (!Character.isWhitespace(c)) {
- currArg.append(c);
- state = NORMAL_TOKEN_STATE;
- } else if (state == NORMAL_TOKEN_STATE) {
- // Whitespace ends the token; start a new one
- argList.add(currArg.toString());
- currArg = new StringBuilder();
- state = NO_TOKEN_STATE;
- }
- }
- break;
- default:
- throw new IllegalStateException("ArgumentTokenizer state " + state + " is invalid!");
- }
- }
- }
-
- // If we're still escaped, put in the backslash
- if (escaped) {
- currArg.append('\\');
- argList.add(currArg.toString());
- }
- // Close the last argument if we haven't yet
- else if (state != NO_TOKEN_STATE) {
- argList.add(currArg.toString());
- }
- // Format each argument if we've been told to stringify them
- if (stringify) {
- for (int i = 0; i < argList.size(); i++) {
- argList.set(i, "\"" + _escapeQuotesAndBackslashes(argList.get(i)) + "\"");
- }
- }
- return argList;
- }
-
- /**
- * Inserts backslashes before any occurrences of a backslash or
- * quote in the given string. Also converts any special characters
- * appropriately.
- */
- protected static String _escapeQuotesAndBackslashes(String s) {
- final StringBuilder buf = new StringBuilder(s);
-
- // Walk backwards, looking for quotes or backslashes.
- // If we see any, insert an extra backslash into the buffer at
- // the same index. (By walking backwards, the index into the buffer
- // will remain correct as we change the buffer.)
- for (int i = s.length() - 1; i >= 0; i--) {
- char c = s.charAt(i);
- if ((c == '\\') || (c == '"')) {
- buf.insert(i, '\\');
- }
- // Replace any special characters with escaped versions
- else if (c == '\n') {
- buf.deleteCharAt(i);
- buf.insert(i, "\\n");
- } else if (c == '\t') {
- buf.deleteCharAt(i);
- buf.insert(i, "\\t");
- } else if (c == '\r') {
- buf.deleteCharAt(i);
- buf.insert(i, "\\r");
- } else if (c == '\b') {
- buf.deleteCharAt(i);
- buf.insert(i, "\\b");
- } else if (c == '\f') {
- buf.deleteCharAt(i);
- buf.insert(i, "\\f");
- }
- }
- return buf.toString();
- }
-}
diff --git a/bundles/org.jkiss.utils/src/org/jkiss/utils/ArrayUtils.java b/bundles/org.jkiss.utils/src/org/jkiss/utils/ArrayUtils.java
deleted file mode 100644
index deb37abc82b03..0000000000000
--- a/bundles/org.jkiss.utils/src/org/jkiss/utils/ArrayUtils.java
+++ /dev/null
@@ -1,311 +0,0 @@
-/*
- * DBeaver - Universal Database Manager
- * Copyright (C) 2010-2024 DBeaver Corp and others
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.jkiss.utils;
-
-import org.jkiss.code.NotNull;
-import org.jkiss.code.Nullable;
-
-import java.lang.reflect.Array;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.function.Predicate;
-
-/**
- * Common utils
- */
-public class ArrayUtils {
-
-
- public static boolean isEmpty(@Nullable Object[] arr)
- {
- return arr == null || arr.length == 0;
- }
-
- public static boolean isEmpty(@Nullable short[] array)
- {
- return array == null || array.length == 0;
- }
-
- public static boolean isArray(@Nullable Object value) {
- return value != null && value.getClass().isArray();
- }
-
- public static boolean contains(@Nullable short[] array, short value)
- {
- if (array == null)
- return false;
- for (short item : array) {
- if (item == value)
- return true;
- }
- return false;
- }
-
- public static boolean contains(@Nullable char[] array, char value)
- {
- if (array == null || array.length == 0)
- return false;
- for (char c : array) {
- if (c == value)
- return true;
- }
- return false;
- }
-
- public static boolean isEmpty(@Nullable int[] array)
- {
- return array == null || array.length == 0;
- }
-
- public static boolean contains(@Nullable int[] array, int value)
- {
- if (array == null)
- return false;
- for (int v : array) {
- if (v == value)
- return true;
- }
- return false;
- }
-
- public static boolean isEmpty(@Nullable long[] array)
- {
- return array == null || array.length == 0;
- }
-
- public static boolean contains(@Nullable long[] array, long value)
- {
- if (array == null)
- return false;
- for (long v : array) {
- if (v == value)
- return true;
- }
- return false;
- }
-
- public static boolean contains(OBJECT_TYPE[] array, OBJECT_TYPE value)
- {
- if (isEmpty(array))
- return false;
- for (OBJECT_TYPE object_type : array) {
- if (CommonUtils.equalObjects(value, object_type))
- return true;
- }
- return false;
- }
-
- public static boolean containsIgnoreCase(String[] array, String value)
- {
- if (isEmpty(array) || value == null)
- return false;
- for (String s : array) {
- if (value.equalsIgnoreCase(s))
- return true;
- }
- return false;
- }
-
- public static boolean containsRef(@NotNull OBJECT_TYPE[] array, @Nullable OBJECT_TYPE value)
- {
- final int length = array.length;
- for (OBJECT_TYPE object_type : array) {
- if (value == object_type)
- return true;
- }
- return false;
- }
-
- @SafeVarargs
- public static boolean containsAny(OBJECT_TYPE[] array, OBJECT_TYPE... values) {
- for (OBJECT_TYPE item : array) {
- for (OBJECT_TYPE value : values) {
- if (CommonUtils.equalObjects(item, value))
- return true;
- }
- }
- return false;
- }
-
- @SafeVarargs
- public static boolean containsAll(OBJECT_TYPE[] array, OBJECT_TYPE... values) {
- if (isEmpty(array)) {
- return false;
- }
- for (OBJECT_TYPE value : values) {
- if (!ArrayUtils.contains(array, value))
- return false;
- }
- return true;
- }
-
- @NotNull
- public static T[] concatArrays(@NotNull T[] first, @NotNull T[] second)
- {
- T[] result = Arrays.copyOf(first, first.length + second.length);
- System.arraycopy(second, 0, result, first.length, second.length);
- return result;
- }
-
- @NotNull
- public static List safeArray(@Nullable T[] array)
- {
- if (array == null) {
- return Collections.emptyList();
- } else {
- return Arrays.asList(array);
- }
- }
-
- /**
- * Returns index of the first found element satisfying a given predicate in the provided array
- */
- public static int indexOf(@NotNull T[] array, @NotNull Predicate condition) {
- for (int i = 0; i < array.length; i++) {
- if (condition.test(array[i])) {
- return i;
- }
- }
- return -1;
- }
-
- public static int indexOf(T[] array, T element) {
- for (int i = 0; i < array.length; i++) {
- if (CommonUtils.equalObjects(array[i], element)) {
- return i;
- }
- }
- return -1;
- }
-
- public static int indexOf(int[] array, int offset, int element) {
- for (int i = offset; i < array.length; i++) {
- if (array[i] == element) {
- return i;
- }
- }
- return -1;
- }
-
- public static int indexOf(byte[] array, int offset, byte element) {
- for (int i = offset; i < array.length; i++) {
- if (array[i] == element) {
- return i;
- }
- }
- return -1;
- }
-
- @SuppressWarnings("unchecked")
- public static T[] deleteArea(Class type, T[] elements, int from, int to) {
- int delCount = to - from + 1;
- T[] newArray = (T[]) Array.newInstance(type, elements.length - delCount);
- System.arraycopy(elements, 0, newArray, 0, from);
- if (to < elements.length - 1) {
- System.arraycopy(elements, to + 1, newArray, from, elements.length - from - delCount);
- }
-
- return newArray;
- }
-
- @SuppressWarnings("unchecked")
- public static T[] insertArea(Class type, Object[] elements, int pos, Object[] add) {
- T[] newArray = (T[]) Array.newInstance(type, elements.length + add.length);
- System.arraycopy(elements, 0, newArray, 0, pos);
- System.arraycopy(add, 0, newArray, pos, add.length);
- System.arraycopy(elements, pos, newArray, pos + add.length, elements.length - pos);
- return newArray;
- }
-
- @SuppressWarnings("unchecked")
- public static T[] add(Class type, T[] elements, T add) {
- T[] newArray = (T[]) Array.newInstance(type, elements.length + 1);
- System.arraycopy(elements, 0, newArray, 0, elements.length);
- newArray[elements.length] = add;
- return newArray;
- }
-
- @SuppressWarnings("unchecked")
- public static T[] remove(Class type, T[] elements, int index) {
- T[] newArray = (T[]) Array.newInstance(type, elements.length - 1);
- System.arraycopy(elements, 0, newArray, 0, index);
- if (index < elements.length - 1) {
- System.arraycopy(elements, index + 1, newArray, index, elements.length - index - 1);
- }
- return newArray;
- }
-
- public static T[] remove(Class type, T[] elements, T element) {
- for (int i = 0; i < elements.length; i++) {
- if (elements[i] == element) {
- return remove(type, elements, i);
- }
- }
- return elements;
- }
-
- public static int[] add(int[] elements, int add) {
- int[] newArray = new int[elements.length + 1];
- System.arraycopy(elements, 0, newArray, 0, elements.length);
- newArray[elements.length] = add;
- return newArray;
- }
-
- public static void main(String[] args) {
- String[] arr = new String[0];
-
- for (int i = 0; i < 100; i++) {
- arr = add(String.class, arr, String.valueOf(i));
- }
- System.out.println(Arrays.toString(arr));
- for (int i = 0; i < 100; i++) {
- arr = remove(String.class, arr, 0);
- }
- System.out.println(Arrays.toString(arr));
- }
-
- @SuppressWarnings("unchecked")
- public static T[] toArray(Class type, Collection extends T> list) {
- return list.toArray((T[]) Array.newInstance(type, list.size()));
- }
-
- @Nullable
- public static boolean[] unbox(@Nullable Boolean[] source) {
- if (source == null) {
- return null;
- }
- final boolean[] result = new boolean[source.length];
- for (int i = 0; i < source.length; i++) {
- result[i] = source[i];
- }
- return result;
- }
-
- public static void reverse(@Nullable Object[] array) {
- if (array == null || array.length <= 1) {
- return;
- }
- for (int i = 0, j = array.length - 1; j > i; ++i, j--) {
- final Object tmp = array[j];
- array[j] = array[i];
- array[i] = tmp;
- }
- }
-}
diff --git a/bundles/org.jkiss.utils/src/org/jkiss/utils/Base64.java b/bundles/org.jkiss.utils/src/org/jkiss/utils/Base64.java
deleted file mode 100644
index d936daa7ede26..0000000000000
--- a/bundles/org.jkiss.utils/src/org/jkiss/utils/Base64.java
+++ /dev/null
@@ -1,330 +0,0 @@
-/*
- * DBeaver - Universal Database Manager
- * Copyright (C) 2010-2024 DBeaver Corp and others
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.jkiss.utils;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Writer;
-
-public final class Base64 {
-
- private static final char[] S_BASE64CHAR = {
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
- 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
- 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
- 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
- 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
- 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', '+', '/'
- };
-
- private static final char S_BASE64PAD = '=';
-
- private static final byte[] S_DECODETABLE = new byte[128];
-
- static {
- for (int i = 0; i < S_DECODETABLE.length; i ++)
- S_DECODETABLE[i] = Byte.MAX_VALUE; // 127
- for (int i = 0; i < S_BASE64CHAR.length; i ++) // 0 to 63
- S_DECODETABLE[S_BASE64CHAR[i]] = (byte)i;
- }
-
- private static int decode0(char[] ibuf, byte[] obuf, int wp) {
- int outlen = 3;
- if (ibuf[3] == S_BASE64PAD) outlen = 2;
- if (ibuf[2] == S_BASE64PAD) outlen = 1;
- int b0 = S_DECODETABLE[ibuf[0]];
- int b1 = S_DECODETABLE[ibuf[1]];
- int b2 = S_DECODETABLE[ibuf[2]];
- int b3 = S_DECODETABLE[ibuf[3]];
- switch (outlen) {
- case 1:
- obuf[wp] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
- return 1;
- case 2:
- obuf[wp++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
- obuf[wp] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
- return 2;
- case 3:
- default:
- obuf[wp++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
- obuf[wp++] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
- obuf[wp] = (byte)(b2 << 6 & 0xc0 | b3 & 0x3f);
- return 3;
- }
- }
-
- /**
- *
- */
- public static byte[] decode(char[] data, int off, int len) {
- char[] ibuf = new char[4];
- int ibufcount = 0;
- byte[] obuf = new byte[len/4*3+3];
- int obufcount = 0;
- for (int i = off; i < off+len; i ++) {
- char ch = data[i];
- if (ch == S_BASE64PAD
- || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
- ibuf[ibufcount++] = ch;
- if (ibufcount == ibuf.length) {
- ibufcount = 0;
- obufcount += decode0(ibuf, obuf, obufcount);
- }
- }
- }
- if (obufcount == obuf.length)
- return obuf;
- byte[] ret = new byte[obufcount];
- System.arraycopy(obuf, 0, ret, 0, obufcount);
- return ret;
- }
-
- /**
- *
- */
- public static byte[] decode(String data) {
- char[] ibuf = new char[4];
- int ibufcount = 0;
- byte[] obuf = new byte[data.length()/4*3+3];
- int obufcount = 0;
- for (int i = 0; i < data.length(); i ++) {
- char ch = data.charAt(i);
- if (ch == S_BASE64PAD
- || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
- ibuf[ibufcount++] = ch;
- if (ibufcount == ibuf.length) {
- ibufcount = 0;
- obufcount += decode0(ibuf, obuf, obufcount);
- }
- }
- }
- if (obufcount == obuf.length)
- return obuf;
- byte[] ret = new byte[obufcount];
- System.arraycopy(obuf, 0, ret, 0, obufcount);
- return ret;
- }
-
- /**
- *
- */
- public static void decode(char[] data, int off, int len, OutputStream ostream) throws IOException {
- char[] ibuf = new char[4];
- int ibufcount = 0;
- byte[] obuf = new byte[3];
- for (int i = off; i < off+len; i ++) {
- char ch = data[i];
- if (ch == S_BASE64PAD
- || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
- ibuf[ibufcount++] = ch;
- if (ibufcount == ibuf.length) {
- ibufcount = 0;
- int obufcount = decode0(ibuf, obuf, 0);
- ostream.write(obuf, 0, obufcount);
- }
- }
- }
- }
-
- /**
- *
- */
- public static void decode(String data, OutputStream ostream) throws IOException {
- char[] ibuf = new char[4];
- int ibufcount = 0;
- byte[] obuf = new byte[3];
- for (int i = 0; i < data.length(); i ++) {
- char ch = data.charAt(i);
- if (ch == S_BASE64PAD
- || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
- ibuf[ibufcount++] = ch;
- if (ibufcount == ibuf.length) {
- ibufcount = 0;
- int obufcount = decode0(ibuf, obuf, 0);
- ostream.write(obuf, 0, obufcount);
- }
- }
- }
- }
-
- /**
- * Returns base64 representation of specified byte array.
- */
- public static String encode(byte[] data) {
- return encode(data, 0, data.length);
- }
-
- public static String splitLines(String bigString, int lineLength) {
- return bigString.replaceAll("(.{" + lineLength + "})", "$1\n");
- }
-
- /**
- * Returns base64 representation of specified byte array.
- */
- public static String encode(byte[] data, int off, int len)
- {
- if (len <= 0) return "";
- char[] out = new char[len/3*4+4];
- int rindex = off;
- int windex = 0;
- int rest = len-off;
- while (rest >= 3) {
- int i = ((data[rindex]&0xff)<<16)
- +((data[rindex+1]&0xff)<<8)
- +(data[rindex+2]&0xff);
- out[windex++] = S_BASE64CHAR[i>>18];
- out[windex++] = S_BASE64CHAR[(i>>12)&0x3f];
- out[windex++] = S_BASE64CHAR[(i>>6)&0x3f];
- out[windex++] = S_BASE64CHAR[i&0x3f];
- rindex += 3;
- rest -= 3;
- }
- if (rest == 1) {
- int i = data[rindex]&0xff;
- out[windex++] = S_BASE64CHAR[i>>2];
- out[windex++] = S_BASE64CHAR[(i<<4)&0x3f];
- out[windex++] = S_BASE64PAD;
- out[windex++] = S_BASE64PAD;
- } else if (rest == 2) {
- int i = ((data[rindex]&0xff)<<8)+(data[rindex+1]&0xff);
- out[windex++] = S_BASE64CHAR[i>>10];
- out[windex++] = S_BASE64CHAR[(i>>4)&0x3f];
- out[windex++] = S_BASE64CHAR[(i<<2)&0x3f];
- out[windex++] = S_BASE64PAD;
- }
- return new String(out, 0, windex);
- }
-
- /**
- * Outputs base64 representation of the specified byte array to a byte stream.
- */
- public static void encode(byte[] data, int off, int len, OutputStream ostream) throws IOException {
- if (len <= 0) return;
- byte[] out = new byte[4];
- int rindex = off;
- int rest = len-off;
- while (rest >= 3) {
- int i = ((data[rindex]&0xff)<<16)
- +((data[rindex+1]&0xff)<<8)
- +(data[rindex+2]&0xff);
- out[0] = (byte)S_BASE64CHAR[i>>18];
- out[1] = (byte)S_BASE64CHAR[(i>>12)&0x3f];
- out[2] = (byte)S_BASE64CHAR[(i>>6)&0x3f];
- out[3] = (byte)S_BASE64CHAR[i&0x3f];
- ostream.write(out, 0, 4);
- rindex += 3;
- rest -= 3;
- }
- if (rest == 1) {
- int i = data[rindex]&0xff;
- out[0] = (byte)S_BASE64CHAR[i>>2];
- out[1] = (byte)S_BASE64CHAR[(i<<4)&0x3f];
- out[2] = (byte)S_BASE64PAD;
- out[3] = (byte)S_BASE64PAD;
- ostream.write(out, 0, 4);
- } else if (rest == 2) {
- int i = ((data[rindex]&0xff)<<8)+(data[rindex+1]&0xff);
- out[0] = (byte)S_BASE64CHAR[i>>10];
- out[1] = (byte)S_BASE64CHAR[(i>>4)&0x3f];
- out[2] = (byte)S_BASE64CHAR[(i<<2)&0x3f];
- out[3] = (byte)S_BASE64PAD;
- ostream.write(out, 0, 4);
- }
- }
-
- /**
- * Outputs base64 representation of the specified byte array to a character stream.
- */
- public static void encode(byte[] data, int off, int len, Writer writer) throws IOException {
- if (len <= 0) return;
- char[] out = new char[4];
- int rindex = off;
- int rest = len-off;
- while (rest >= 3) {
- int i = ((data[rindex]&0xff)<<16)
- +((data[rindex+1]&0xff)<<8)
- +(data[rindex+2]&0xff);
- out[0] = S_BASE64CHAR[i>>18];
- out[1] = S_BASE64CHAR[(i>>12)&0x3f];
- out[2] = S_BASE64CHAR[(i>>6)&0x3f];
- out[3] = S_BASE64CHAR[i&0x3f];
- writer.write(out, 0, 4);
- rindex += 3;
- rest -= 3;
-/*
- if (output % 76 == 0)
- writer.write("\n");
-*/
- }
- if (rest == 1) {
- int i = data[rindex]&0xff;
- out[0] = S_BASE64CHAR[i>>2];
- out[1] = S_BASE64CHAR[(i<<4)&0x3f];
- out[2] = S_BASE64PAD;
- out[3] = S_BASE64PAD;
- writer.write(out, 0, 4);
- } else if (rest == 2) {
- int i = ((data[rindex]&0xff)<<8)+(data[rindex+1]&0xff);
- out[0] = S_BASE64CHAR[i>>10];
- out[1] = S_BASE64CHAR[(i>>4)&0x3f];
- out[2] = S_BASE64CHAR[(i<<2)&0x3f];
- out[3] = S_BASE64PAD;
- writer.write(out, 0, 4);
- }
- }
-
- /**
- * Outputs base64 representation of the specified input stream to a character stream.
- */
- public static void encode(InputStream stream, long len, Writer writer)
- throws IOException
- {
- if (len <= 0) return;
- char[] out = new char[4];
- long rest = len;
- while (rest >= 3) {
- int i = ((stream.read()&0xff)<<16)
- +((stream.read()&0xff)<<8)
- +(stream.read()&0xff);
- out[0] = S_BASE64CHAR[i>>18];
- out[1] = S_BASE64CHAR[(i>>12)&0x3f];
- out[2] = S_BASE64CHAR[(i>>6)&0x3f];
- out[3] = S_BASE64CHAR[i&0x3f];
- writer.write(out, 0, 4);
- rest -= 3;
- }
- if (rest == 1) {
- int i = stream.read()&0xff;
- out[0] = S_BASE64CHAR[i>>2];
- out[1] = S_BASE64CHAR[(i<<4)&0x3f];
- out[2] = S_BASE64PAD;
- out[3] = S_BASE64PAD;
- writer.write(out, 0, 4);
- } else if (rest == 2) {
- int i = ((stream.read()&0xff)<<8)+(stream.read()&0xff);
- out[0] = S_BASE64CHAR[i>>10];
- out[1] = S_BASE64CHAR[(i>>4)&0x3f];
- out[2] = S_BASE64CHAR[(i<<2)&0x3f];
- out[3] = S_BASE64PAD;
- writer.write(out, 0, 4);
- }
- }
-
-}
diff --git a/bundles/org.jkiss.utils/src/org/jkiss/utils/BeanUtils.java b/bundles/org.jkiss.utils/src/org/jkiss/utils/BeanUtils.java
deleted file mode 100644
index 5a686adbd543e..0000000000000
--- a/bundles/org.jkiss.utils/src/org/jkiss/utils/BeanUtils.java
+++ /dev/null
@@ -1,488 +0,0 @@
-/*
- * DBeaver - Universal Database Manager
- * Copyright (C) 2010-2024 DBeaver Corp and others
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.jkiss.utils;
-
-import org.jkiss.code.NotNull;
-import org.jkiss.code.Nullable;
-
-import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodType;
-import java.lang.reflect.*;
-import java.util.*;
-import java.util.function.Supplier;
-
-/**
- * BeanUtils
- */
-public class BeanUtils {
-
- public static boolean isGetterName(String name) {
- return name.startsWith("get") || name.startsWith("is") || name.startsWith("has");
- }
-
- public static String getPropertyNameFromGetter(String getterName) {
- if (getterName.startsWith("get")) {
- return
- Character.toLowerCase(getterName.charAt(3)) +
- getterName.substring(4);
- } else if (getterName.startsWith("is")) {
- return
- Character.toLowerCase(getterName.charAt(2)) +
- getterName.substring(3);
- } else if (getterName.startsWith("has")) {
- return
- Character.toLowerCase(getterName.charAt(3)) +
- getterName.substring(4);
- } else {
- // Unrecognized getter name
- return null;
- }
- }
-
- public static String getSetterName(String getterName) {
- if (getterName.startsWith("get")) {
- return "set" + getterName.substring(3);
- } else if (getterName.startsWith("is")) {
- return "set" + getterName.substring(2);
- } else if (getterName.startsWith("has")) {
- return "set" + getterName.substring(3);
- } else {
- // Unrecognized getter name
- return null;
- }
- }
-
- /**
- * Returns a set method matching the property name.
- */
- public static Method getSetMethod(Class> cl, String propertyName) {
- Method method = getSetMethod(cl, propertyName, false);
-
- if (method != null) {
- return method;
- }
-
- return getSetMethod(cl, propertyName, true);
- }
-
- /**
- * Returns a set method matching the property name.
- */
- public static Method getSetMethod(
- Class> cl,
- String propertyName,
- boolean ignoreCase) {
- String setName = "set" + propertyNameToMethodName(propertyName);
-
- return getSetMethod(
- cl.getMethods(),
- setName,
- ignoreCase);
- }
-
- /**
- * Returns a get method matching the property name.
- */
- public static Method getGetMethod(Class> cl, String propertyName) {
- Method method = getGetMethod(cl, propertyName, false);
-
- return method != null ?
- method :
- getGetMethod(cl, propertyName, true);
- }
-
- /**
- * Returns a get method matching the property name.
- */
- public static Method getGetMethod(
- Class> cl,
- String propertyName,
- boolean ignoreCase) {
- String methodName = propertyNameToMethodName(propertyName);
- return getGetMethod(
- cl.getMethods(),
- "get" + methodName,
- "is" + methodName,
- ignoreCase);
- }
-
- /**
- * Converts a user's property name to a bean method name.
- *
- * @param propertyName the user property name
- * @return the equivalent bean method name
- */
- public static String propertyNameToMethodName(String propertyName) {
- char ch = propertyName.charAt(0);
- if (Character.isLowerCase(ch))
- propertyName = Character.toUpperCase(ch) + propertyName.substring(1);
-
- return propertyName;
- }
-
- /**
- * Converts a user's property name to a bean method name.
- *
- * @param methodName the method name
- * @return the equivalent property name
- */
- public static String methodNameToPropertyName(String methodName) {
- if (methodName.startsWith("get"))
- methodName = methodName.substring(3);
- else if (methodName.startsWith("set"))
- methodName = methodName.substring(3);
- else if (methodName.startsWith("is"))
- methodName = methodName.substring(2);
-
- if (methodName.length() == 0)
- return null;
-
- char ch = methodName.charAt(0);
- if (Character.isUpperCase(ch) && (methodName.length() == 1 || !Character.isUpperCase(methodName.charAt(1)))) {
- methodName = Character.toLowerCase(ch) + methodName.substring(1);
- }
-
- return methodName;
- }
-
- public static boolean isArrayType(Type type) {
- return (type instanceof Class && ((Class>) type).isArray());
- }
-
- public static boolean isCollectionType(Type type) {
- if (type instanceof Class && Collection.class.isAssignableFrom((Class>) type)) {
-/*
- if (type instanceof ParameterizedType) {
- ParameterizedType pt = (ParameterizedType)type;
- if (pt.getActualTypeArguments().length == 1) {
- return true;
- }
- }
-*/
- return true;
- }
- return isArrayType(type);
- }
-
- public static Class> getCollectionType(Type type) {
- if (type instanceof ParameterizedType) {
- ParameterizedType pt = (ParameterizedType) type;
- if (pt.getActualTypeArguments().length == 1) {
- final Type argType = pt.getActualTypeArguments()[0];
- if (argType instanceof Class) {
- return (Class>) argType;
- } else if (argType instanceof WildcardType) {
- final Type[] upperBounds = ((WildcardType) argType).getUpperBounds();
- if (upperBounds.length > 0 && upperBounds[0] instanceof Class) {
- return (Class>) upperBounds[0];
- }
- final Type[] lowerBounds = ((WildcardType) argType).getLowerBounds();
- if (lowerBounds.length > 0 && lowerBounds[0] instanceof Class) {
- return (Class>) lowerBounds[0];
- }
- }
- }
- }
- return null;
- }
-
- public static Object readObjectProperty(Object object, String propName)
- throws IllegalAccessException, InvocationTargetException {
- if (propName.indexOf('.') == -1) {
- Method getter = getGetMethod(object.getClass(), propName);
- return getter == null ? null : getter.invoke(object);
- }
- // Parse property path
- StringTokenizer st = new StringTokenizer(propName, ".");
- Object value = object;
- while (value != null && st.hasMoreTokens()) {
- String pathItem = st.nextToken();
- Method getter = getGetMethod(value.getClass(), pathItem);
- if (getter == null) {
- return null;
- }
- value = getter.invoke(value);
- }
- return value;
- }
-
- /**
- * Finds the matching set method
- */
- private static Method getGetMethod(
- Method[] methods,
- String getName,
- String isName,
- boolean ignoreCase) {
- for (int i = 0; i < methods.length; i++) {
- Method method = methods[i];
-
- // The method must be public
- if (
- (!Modifier.isPublic(method.getModifiers())) ||
- (!Modifier.isPublic(method.getDeclaringClass().getModifiers())) ||
- (method.getParameterTypes().length != 0) ||
- (method.getReturnType().equals(void.class))) {
- continue;
- } else if (!ignoreCase && method.getName().equals(getName)) {
- // If it matches the get name, it's the right method
- return method;
- } else if (ignoreCase && method.getName().equalsIgnoreCase(getName)) {
- // If it matches the get name, it's the right method
- return method;
- } else if (!method.getReturnType().equals(boolean.class)) {
- // The is methods must return boolean
- continue;
- } else if (!ignoreCase && method.getName().equals(isName)) {
- // If it matches the is name, it must return boolean
- return method;
- } else if (ignoreCase && method.getName().equalsIgnoreCase(isName)) {
- // If it matches the is name, it must return boolean
- return method;
- }
- }
-
- return null;
- }
-
- /**
- * Finds the matching set method
- *
- * @param setName the method name
- */
- private static Method getSetMethod(
- Method[] methods,
- String setName,
- boolean ignoreCase) {
- for (int i = 0; i < methods.length; i++) {
- Method method = methods[i];
-
- // The method name must match
- if (
- !(ignoreCase ? method.getName().equalsIgnoreCase(setName) : method.getName().equals(setName)) ||
- !Modifier.isPublic(method.getModifiers()) ||
- !Modifier.isPublic(method.getDeclaringClass().getModifiers()) ||
- method.getParameterTypes().length != 1
- )
- continue;
-
- return method;
- }
-
- return null;
- }
-
- public static final Short DEFAULT_SHORT = (short) 0;
- public static final Integer DEFAULT_INTEGER = 0;
- public static final Long DEFAULT_LONG = 0L;
- public static final Float DEFAULT_FLOAT = (float) 0.0;
- public static final Double DEFAULT_DOUBLE = 0.0;
- public static final Byte DEFAULT_BYTE = (byte) 0;
- public static final Character DEFAULT_CHAR = (char) 0;
-
- public static boolean isBooleanType(Type paramClass) {
- return paramClass == Boolean.class || paramClass == Boolean.TYPE;
- }
-
- public static Object getDefaultPrimitiveValue(Class> paramClass) {
- if (paramClass == Boolean.TYPE) {
- return Boolean.FALSE;
- } else if (paramClass == Short.TYPE) {
- return DEFAULT_SHORT;
- } else if (paramClass == Integer.TYPE) {
- return DEFAULT_INTEGER;
- } else if (paramClass == Long.TYPE) {
- return DEFAULT_LONG;
- } else if (paramClass == Float.TYPE) {
- return DEFAULT_FLOAT;
- } else if (paramClass == Double.TYPE) {
- return DEFAULT_DOUBLE;
- } else if (paramClass == Byte.TYPE) {
- return DEFAULT_BYTE;
- } else if (paramClass == Character.TYPE) {
- return DEFAULT_CHAR;
- } else {
- throw new IllegalArgumentException("Class " + paramClass.getName() + " is not primitive type");
- }
- }
-
- public static boolean isNumericType(Class> paramClass) {
- return
- Number.class.isAssignableFrom(paramClass) ||
- paramClass == Short.TYPE ||
- paramClass == Integer.TYPE ||
- paramClass == Long.TYPE ||
- paramClass == Double.TYPE ||
- paramClass == Float.TYPE ||
- paramClass == Byte.TYPE;
- }
-
- public static Object invokeObjectMethod(Object object, String name, Class> paramTypes[], Object args[])
- throws Throwable {
- Method method = object.getClass().getMethod(name, paramTypes);
- method.setAccessible(true);
- try {
- return method.invoke(object, args);
- } catch (InvocationTargetException e) {
- throw e.getTargetException();
- }
- }
-
- public static Object invokeObjectMethod(Object object, String name)
- throws Throwable {
- Method method = object.getClass().getMethod(name);
- method.setAccessible(true);
- try {
- return method.invoke(object);
- } catch (InvocationTargetException e) {
- throw e.getTargetException();
- }
- }
-
- @Nullable
- public static Object invokeObjectDeclaredMethod(
- @NotNull Object object,
- @NotNull String methodName,
- @NotNull Class>[] paramTypes,
- @NotNull Object[] args) throws Throwable
- {
- for (Class> cls = object.getClass(); cls != null; cls = cls.getSuperclass()) {
- for (Method method : cls.getDeclaredMethods()) {
- if (method.getName().equals(methodName) && Arrays.equals(method.getParameterTypes(), paramTypes)) {
- method.setAccessible(true);
- try {
- return method.invoke(object, args);
- } catch (InvocationTargetException e) {
- throw e.getTargetException();
- }
- }
- }
- }
- throw new NoSuchMethodException("Cannot find declared method " + methodName + "(" + Arrays.toString(paramTypes) + ")");
- }
-
- public static Object invokeStaticMethod(Class> objectType, String name, Class> paramTypes[], Object args[])
- throws Throwable {
- Method method = objectType.getMethod(name, paramTypes);
- method.setAccessible(true);
- try {
- return method.invoke(null, args);
- } catch (InvocationTargetException e) {
- throw e.getTargetException();
- }
- }
-
- public static Class extends T> findAssignableType(Class>[] types, Class type) {
- for (Class> childType : types) {
- if (type.isAssignableFrom(childType)) {
- return (Class extends T>) childType;
- }
- }
- return null;
- }
-
- /**
- * Determines the distance of the given object to the given class in the
- * inheritance tree.
- *
- * @param object the root object
- * @param clazz the class to determine the distance to
- * @return the distance of the object to the class. If the object is not an
- * instance of the class {@code -1} will return.
- */
- public static int getInheritanceDistance(Object object, Class> clazz) {
- if (clazz.isInstance(object)) {
- int distance = 0;
- Class> compared = object.getClass();
- while (compared != clazz) {
- compared = compared.getSuperclass();
- distance++;
- if (compared == Object.class) {
- break;
- }
- }
- return distance;
- } else {
- return -1;
- }
- }
-
- @SuppressWarnings("unchecked")
- public static T getFieldValue(@NotNull Object object, @NotNull String name) throws Throwable {
- final Field field = object.getClass().getDeclaredField(name);
- if (!field.canAccess(object)) {
- field.setAccessible(true);
- }
- return (T) field.get(object);
- }
-
- @Nullable
- public static Object handleObjectMethod(@NotNull Object proxy, @NotNull Method method, Object[] args) {
- switch (method.getName()) {
- case "toString":
- return "Proxy";
- case "hashCode":
- return System.identityHashCode(proxy);
- case "equals":
- return proxy == args[0];
- default:
- return null;
- }
- }
-
- @NotNull
- public static List deepCopy(@NotNull List src) {
- final List dst = tryInstantiateOrDefault(src.getClass(), ArrayList::new);
- for (T element : src) {
- dst.add(deepCopy(element));
- }
- return dst;
- }
-
- @NotNull
- public static Map deepCopy(@NotNull Map src) {
- final Map dst = tryInstantiateOrDefault(src.getClass(), LinkedHashMap::new);
- for (Map.Entry entry : src.entrySet()) {
- dst.put(entry.getKey(), deepCopy(entry.getValue()));
- }
- return dst;
- }
-
- @SuppressWarnings("unchecked")
- private static T deepCopy(@Nullable T object) {
- if (object == null) {
- return null;
- } else if (object instanceof Map) {
- return (T) deepCopy((Map, ?>) object);
- } else if (object instanceof List) {
- return (T) deepCopy((List>) object);
- } else {
- return object;
- }
- }
-
- @SuppressWarnings("unchecked")
- @NotNull
- private static T tryInstantiateOrDefault(@NotNull Class> cls, @NotNull Supplier supplier) {
- try {
- return (T) MethodHandles.lookup().findConstructor(cls, MethodType.methodType(void.class)).invoke();
- } catch (Throwable ignored) {
- return supplier.get();
- }
- }
-}
diff --git a/bundles/org.jkiss.utils/src/org/jkiss/utils/ByteNumberFormat.java b/bundles/org.jkiss.utils/src/org/jkiss/utils/ByteNumberFormat.java
deleted file mode 100644
index f858287ac75a0..0000000000000
--- a/bundles/org.jkiss.utils/src/org/jkiss/utils/ByteNumberFormat.java
+++ /dev/null
@@ -1,180 +0,0 @@
-/*
- * DBeaver - Universal Database Manager
- * Copyright (C) 2010-2024 DBeaver Corp and others
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.jkiss.utils;
-
-import org.jkiss.code.NotNull;
-
-import java.text.DecimalFormat;
-import java.text.FieldPosition;
-import java.text.NumberFormat;
-import java.text.ParsePosition;
-
-/**
- * Bytes formatter
- */
-public class ByteNumberFormat extends NumberFormat {
- private static final long serialVersionUID = 1;
-
- public static final Unit[] UNITS = Unit.values();
-
- private static final DecimalFormat fpFormat = new DecimalFormat("#.#");
-
- private final BinaryPrefix binaryPrefix;
-
- /**
- * Creates a new formatter.
- */
- public ByteNumberFormat() {
- binaryPrefix = BinaryPrefix.JEDEC;
- }
-
- public ByteNumberFormat(@NotNull BinaryPrefix binaryPrefix) {
- this.binaryPrefix = binaryPrefix;
- }
-
- public static int computeIndex(double bytes) {
- int index = 0;
-
- for (int i = 0; i < UNITS.length; i++) {
- int result = (int)(bytes / 1024);
- if (result == 0) {
- break;
- } else {
- bytes /= 1024;
- if (bytes < 1) {
- break;
- }
- index++;
- }
- }
-
- return index;
- }
-
- /**
- * Returns a string representing the bytes.
- *
- * @param bytes the number of bytes.
- *
- * @return A string.
- */
- public String getBytes(double bytes) {
-
- int index = computeIndex(bytes);
- if (index >= UNITS.length) {
- index = UNITS.length - 1;
- }
-
- double intBytes = bytes;
- if (intBytes == 0) {
- return String.valueOf(0);
- }
-
- for (int i = 0; i < index; i++) {
- intBytes /= 1024;
- }
-
-
- String str;
- if ((long)intBytes >= 10) {
- str = String.valueOf((long)intBytes);
- } else {
- str = fpFormat.format(intBytes);
- }
- final Unit unit = UNITS[index];
- if (unit == Unit.BYTE) {
- return str;
- }
- return str + (binaryPrefix == BinaryPrefix.ISO ? unit.isoPrefix : unit.jedecPrefix);
- }
-
- /**
- * Formats a number into the specified string buffer.
- *
- * @param number the number to format.
- * @param toAppendTo the string buffer.
- * @param pos the field position (ignored here).
- *
- * @return The string buffer.
- */
- @Override
- public StringBuffer format(double number, StringBuffer toAppendTo,
- FieldPosition pos) {
- return toAppendTo.append(getBytes(number));
- }
-
- /**
- * Formats a number into the specified string buffer.
- *
- * @param number the number to format.
- * @param toAppendTo the string buffer.
- * @param pos the field position (ignored here).
- *
- * @return The string buffer.
- */
- @Override
- public StringBuffer format(long number, StringBuffer toAppendTo,
- FieldPosition pos) {
- return toAppendTo.append(getBytes(number));
- }
-
- /**
- * This method returns null for all inputs. This class cannot
- * be used for parsing.
- *
- * @param source the source string.
- * @param parsePosition the parse position.
- *
- * @return null.
- */
- @Override
- public Number parse(String source, ParsePosition parsePosition) {
- return null;
- }
-
- private enum Unit {
- BYTE("B", "B"),
- KILOBYTE("K", "KiB"),
- MEGABYTE("M", "MiB"),
- GIGABYTE("G", "GiB"),
- TERABYTE("T", "TiB"),
- PETABYTE("P", "PiB");
-
- private final String jedecPrefix;
- private final String isoPrefix;
-
- Unit(String jedecPrefix, String isoPrefix) {
- this.jedecPrefix = jedecPrefix;
- this.isoPrefix = isoPrefix;
- }
- }
-
- /**
- * A unit prefix for multiples of byte.
- */
- public enum BinaryPrefix {
- /**
- * JEDEC-compliant prefixes (K, M, G, T, etc.). It's easy to confuse such prefixes with SI prefixes, leading to ambiguity.
- */
- JEDEC,
-
- /**
- * ISO 80000-13 format. This format explicitly states the binary nature of prefixes right in their names (kibi, mibi, gibi, etc.)
- */
- ISO,
- }
-}
diff --git a/bundles/org.jkiss.utils/src/org/jkiss/utils/CommonUtils.java b/bundles/org.jkiss.utils/src/org/jkiss/utils/CommonUtils.java
deleted file mode 100644
index baad78ae23776..0000000000000
--- a/bundles/org.jkiss.utils/src/org/jkiss/utils/CommonUtils.java
+++ /dev/null
@@ -1,1066 +0,0 @@
-/*
- * DBeaver - Universal Database Manager
- * Copyright (C) 2010-2024 DBeaver Corp and others
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.jkiss.utils;
-
-import org.jkiss.code.NotNull;
-import org.jkiss.code.Nullable;
-
-import java.lang.reflect.InvocationTargetException;
-import java.util.*;
-import java.util.function.Function;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * Common utils
- */
-public class CommonUtils {
-
- public static final char PARAGRAPH_CHAR = (char) 182;
-
- public static boolean isJavaIdentifier(@NotNull CharSequence str) {
- if (str.length() == 0 || !Character.isJavaIdentifierStart(str.charAt(0))) {
- return false;
- }
- for (int i = 1; i < str.length(); i++) {
- if (!Character.isJavaIdentifierPart(str.charAt(i))) {
- return false;
- }
- }
- return true;
- }
-
- @NotNull
- public static String escapeJavaString(@NotNull String str) {
- StringBuilder res = new StringBuilder(str.length() + 5);
- for (int i = 0; i < str.length(); i++) {
- char c = str.charAt(i);
- switch (c) {
- case '"' -> res.append("\\\"");
- case '\n' -> res.append("\\n");
- case '\r' -> res.append("\\r");
- case '\t' -> res.append("\\t");
- default -> res.append(c);
- }
- }
- return res.toString();
- }
-
- @Nullable
- public static String escapeIdentifier(@Nullable String str) {
- if (str == null) {
- return null;
- }
- StringBuilder res = new StringBuilder(str.length());
- for (int i = 0; i < str.length(); i++) {
- char c = str.charAt(i);
- if (Character.isJavaIdentifierPart(c)) {
- res.append(c);
- } else {
- if (res.length() == 0 || res.charAt(res.length() - 1) != '_') {
- res.append('_');
- }
- }
- }
- return res.toString();
- }
-
- @NotNull
- public static String escapeFileName(@Nullable String str) {
- if (str == null) {
- return "";
- }
- StringBuilder res = new StringBuilder(str.length());
- for (int i = 0; i < str.length(); i++) {
- char c = str.charAt(i);
- if (Character.isISOControl(c) || c == '\\' || c == '/' || c == '<' || c == '>' || c == '|' || c == '"' || c == ':'
- || c == '*' || c == '?') {
- res.append('_');
- } else {
- res.append(c);
- }
- }
- return res.toString();
- }
-
- public static String makeDirectoryName(@NotNull String str) {
- if (!str.endsWith("/")) {
- str += "/";
- }
- return str;
- }
-
- @NotNull
- public static String removeTrailingSlash(@NotNull String str) {
- while (str.endsWith("/") || str.endsWith("\\")) {
- str = str.substring(0, str.length() - 1);
- }
- return str;
- }
-
- @NotNull
- public static String removeLeadingSlash(@NotNull String str) {
- while (str.startsWith("/") || str.startsWith("\\")) {
- str = str.substring(1);
- }
- return str;
- }
-
- public static String capitalizeWord(String str) {
- if (isEmpty(str) || Character.isUpperCase(str.charAt(0))) {
- return str;
- }
- return Character.toUpperCase(str.charAt(0)) + str.substring(1);
- }
-
-
- public static String toCamelCase(String str) {
- if (isEmpty(str)) {
- return str;
- }
-
- final StringBuilder ret = new StringBuilder(str.length());
-
- boolean isWordStart = true;
- for (int i = 0; i < str.length(); i++) {
- char ch = str.charAt(i);
- if (Character.isLetterOrDigit(ch)) {
- if (isWordStart) {
- ret.append(Character.toUpperCase(ch));
- isWordStart = false;
- } else {
- ret.append(Character.toLowerCase(ch));
- }
- } else {
- ret.append(ch);
- isWordStart = true;
- }
- }
-
- return ret.toString();
- }
-
- @NotNull
- public static T notNull(@Nullable T value, @NotNull T defaultValue) {
- return value != null ? value : defaultValue;
- }
-
- public static boolean isEmpty(@Nullable CharSequence value) {
- return value == null || value.length() == 0;
- }
-
- public static boolean isEmpty(@Nullable String value) {
- return value == null || value.length() == 0;
- }
-
- public static boolean isNotEmpty(@Nullable String value) {
- return !isEmpty(value);
- }
-
- public static boolean isEmpty(@Nullable Collection> value) {
- return value == null || value.isEmpty();
- }
-
- public static boolean isEmpty(@Nullable Map, ?> value) {
- return value == null || value.isEmpty();
- }
-
- @Nullable
- public static T getFirstOrNull(@NotNull Iterable iterable) {
- Iterator iterator = iterable.iterator();
- if (iterator.hasNext()) {
- return iterator.next();
- }
- return null;
- }
-
- @NotNull
- public static Collection safeCollection(@Nullable Collection theList) {
- if (theList == null) {
- theList = Collections.emptyList();
- }
- return theList;
- }
-
- @NotNull
- public static List singletonOrEmpty(@Nullable T object) {
- if (object == null) {
- return Collections.emptyList();
- }
- return Collections.singletonList(object);
- }
-
- @NotNull
- public static List safeList(@Nullable List theList) {
- if (theList == null) {
- theList = Collections.emptyList();
- }
- return theList;
- }
-
- @NotNull
- public static List copyList(@Nullable Collection theList) {
- if (theList == null) {
- return new ArrayList<>();
- } else {
- return new ArrayList<>(theList);
- }
- }
-
- /**
- * Swaps the element with its neighbor to the left in the specified list.
- * If the element is not present in the list or it is the leftmost element in the list,
- * the list remains unchanged.
- *
- * @param list list
- * @param element element
- * @param type of the list
- */
- public static void shiftLeft(@NotNull List super T> list, @NotNull T element) {
- int idx = list.indexOf(element);
- if (idx > 0) {
- Collections.swap(list, idx - 1, idx);
- }
- }
-
- /**
- * Swaps the element with its neighbor to the right in the specified list.
- * If the element is not present in the list or it is the rightmost element in the list,
- * the list remains unchanged.
- *
- * @param list list
- * @param element element
- * @param type of the list
- */
- public static void shiftRight(@NotNull List super T> list, @NotNull T element) {
- int idx = list.indexOf(element);
- if (idx != -1 && idx != list.size() - 1) {
- Collections.swap(list, idx, idx + 1);
- }
- }
-
- @NotNull
- public static String notEmpty(@Nullable String value) {
- return value == null ? "" : value;
- }
-
- @Nullable
- public static String nullIfEmpty(@Nullable String value) {
- return value == null || value.isEmpty() ? null : value;
- }
-
- public static boolean isTrue(Boolean value) {
- return value != null && value;
- }
-
- public static boolean getBoolean(String value) {
- return Boolean.parseBoolean(value);
- }
-
- public static boolean getBoolean(@Nullable String value, boolean defaultValue) {
- return isEmpty(value) ? defaultValue : Boolean.parseBoolean(value);
- }
-
- public static boolean getBoolean(@Nullable Object value, boolean defaultValue) {
- if (value == null) {
- return defaultValue;
- } else if (value instanceof Boolean b) {
- return b;
- } else {
- return getBoolean(value.toString(), defaultValue);
- }
- }
-
- @NotNull
- public static String getLineSeparator() {
- String lineSeparator = System.getProperty(StandardConstants.ENV_LINE_SEPARATOR);
- return lineSeparator == null ? "\n" : lineSeparator;
- }
-
- @NotNull
- public static Throwable getRootCause(@NotNull Throwable ex) {
- Throwable rootCause = ex;
- for (; ; ) {
- if (rootCause.getCause() != null) {
- rootCause = rootCause.getCause();
- } else if (rootCause instanceof InvocationTargetException ite && ite.getTargetException() != null) {
- rootCause = ite.getTargetException();
- } else {
- break;
- }
- }
- return rootCause;
- }
-
- public static boolean equalObjects(@Nullable Object o1, @Nullable Object o2) {
- if (o1 == o2) {
- return true;
- }
- if (o1 == null || o2 == null) {
- return false;
- }
-// if (o1.getClass() != o2.getClass()) {
-// return false;
-// }
- return o1.equals(o2);
- }
-
- public static boolean equalOrEmptyStrings(@Nullable String s1, @Nullable String s2) {
- return equalObjects(s1, s2) || (isEmpty(s1) && isEmpty(s2));
- }
-
- public static boolean equalsContents(@Nullable Collection> c1, @Nullable Collection> c2) {
- if (CommonUtils.isEmpty(c1) && CommonUtils.isEmpty(c2)) {
- return true;
- }
- if (c1 == null || c2 == null || c1.size() != c2.size()) {
- return false;
- }
- for (Object o : c1) {
- if (!c2.contains(o)) {
- return false;
- }
- }
- return true;
- }
-
- @NotNull
- public static String toString(@Nullable Object object) {
- if (object == null) {
- return "";
- } else if (object instanceof String s) {
- return s;
- } else {
- String strValue = object.toString();
- return strValue == null ? "" : strValue;
- }
- }
-
- public static String toString(@Nullable Object object, String def) {
- if (object == null) {
- return def;
- } else if (object instanceof String s) {
- return s;
- } else {
- return object.toString();
- }
- }
-
- public static boolean toBoolean(@Nullable Object object) {
- return object != null && getBoolean(object.toString());
- }
-
- public static int toInt(@Nullable Object object, int def) {
- if (object == null) {
- return def;
- } else if (object instanceof Number n) {
- return n.intValue();
- } else {
- try {
- return Integer.parseInt(toString(object));
- } catch (NumberFormatException e) {
- try {
- return (int)Double.parseDouble(toString(object));
- } catch (NumberFormatException e1) {
- e1.printStackTrace();
- return def;
- }
- }
- }
- }
-
- public static int toInt(@Nullable Object object) {
- return toInt(object, 0);
- }
-
- public static boolean isInt(@Nullable Object object) {
- if (object == null) {
- return false;
- } else if (object instanceof Number) {
- return true;
- } else {
- try {
- Integer.parseInt(toString(object));
- return true;
- } catch (NumberFormatException e) {
- return false;
- }
- }
- }
- public static long toLong(@Nullable Object object) {
- return toLong(object, 0);
- }
-
- public static long toLong(@Nullable Object object, long defValue) {
- if (object == null) {
- return defValue;
- } else if (object instanceof Number n) {
- return n.longValue();
- } else {
- try {
- return Long.parseLong(toString(object));
- } catch (NumberFormatException e) {
- try {
- return (int)Double.parseDouble(toString(object));
- } catch (NumberFormatException e1) {
- return defValue;
- }
- }
- }
- }
-
- public static boolean isLong(@Nullable Object object) {
- if (object == null) {
- return false;
- } else if (object instanceof Number) {
- return true;
- } else {
- try {
- Long.parseLong(toString(object));
- return true;
- } catch (NumberFormatException e) {
- return false;
- }
- }
- }
-
- public static boolean isNumber(@Nullable Object object) {
- if (object == null) {
- return false;
- } else if (object instanceof Number) {
- return true;
- } else {
- try {
- Double.parseDouble(toString(object));
- return true;
- } catch (NumberFormatException e) {
- return false;
- }
- }
- }
-
- public static double toDouble(@Nullable Object object) {
- if (object == null) {
- return 0.0;
- } else if (object instanceof Number) {
- return ((Number) object).doubleValue();
- } else {
- try {
- return Double.parseDouble(toString(object));
- } catch (NumberFormatException e) {
- return Double.NaN;
- }
- }
- }
-
- public static double toDouble(@Nullable Object object, double def) {
- if (object == null) {
- return def;
- } else if (object instanceof Number) {
- return ((Number) object).doubleValue();
- } else {
- try {
- return Double.parseDouble(toString(object));
- } catch (NumberFormatException e) {
- return def;
- }
- }
- }
-
- public static float toFloat(@Nullable Object object) {
- if (object == null) {
- return 0.0f;
- } else if (object instanceof Number) {
- return ((Number) object).floatValue();
- } else {
- try {
- return Float.parseFloat(toString(object));
- } catch (NumberFormatException e) {
- return Float.NaN;
- }
- }
- }
-
- public static float toFloat(@Nullable Object object, float def) {
- if (object == null) {
- return def;
- } else if (object instanceof Number) {
- return ((Number) object).floatValue();
- } else {
- try {
- return Float.parseFloat(toString(object));
- } catch (NumberFormatException e) {
- return def;
- }
- }
- }
-
- public static boolean isNaN(@Nullable Object value) {
- return (value instanceof Float && ((Float) value).isNaN())
- || (value instanceof Double && ((Double) value).isNaN());
- }
-
- public static boolean isInfinite(@Nullable Object value) {
- return (value instanceof Float && ((Float) value).isInfinite())
- || (value instanceof Double && ((Double) value).isInfinite());
- }
-
- @NotNull
- public static String toHexString(@Nullable byte[] bytes) {
- return bytes == null ? "" : toHexString(bytes, 0, bytes.length);
- }
-
- final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
-
- @NotNull
- public static String toHexString(@Nullable byte[] bytes, int offset, int length) {
- if (bytes == null || bytes.length == 0) {
- return "";
- }
- char[] hexChars = new char[length * 2];
- for (int i = 0; i < length; i++) {
- int v = bytes[offset + i] & 0xFF;
- hexChars[i * 2] = hexArray[v >>> 4];
- hexChars[i * 2 + 1] = hexArray[v & 0x0F];
- }
- return new String(hexChars);
- }
-
- public static byte[] parseHexString(String hex) {
- int strLength = hex.length();
- byte[] data = new byte[strLength / 2];
- for (int i = 0; i < strLength; i += 2) {
- data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i+1), 16));
- }
- return data;
- }
-
- public static String toBinaryString(long longValue, int bitCount) {
- String strValue = Long.toString(longValue, 2);
- if (strValue.length() < bitCount) {
- char[] headZeroes = new char[bitCount - strValue.length()];
- Arrays.fill(headZeroes, '0');
- strValue = String.valueOf(headZeroes) + strValue;
- }
- return strValue;
- }
-
- public static String[] splitWithDelimiter(String s, String delimiter) {
- if (s == null) {
- return null;
- }
- String delimiterReplacement = "DRDRDR"; //$NON-NLS-1$
- s = s.replace(delimiter, delimiterReplacement + delimiter);
- return s.split(delimiterReplacement);
- }
-
- @NotNull
- public static List splitString(@Nullable String str, char delimiter) {
- if (CommonUtils.isEmpty(str)) {
- return Collections.emptyList();
- } else {
- List result = new ArrayList<>();
- StringTokenizer st = new StringTokenizer(str, String.valueOf(delimiter));
- while (st.hasMoreTokens()) {
- result.add(st.nextToken());
- }
- return result;
- }
- }
-
- @NotNull
- public static String[] split(@Nullable String str, String delimiter) {
- if (CommonUtils.isEmpty(str)) {
- return new String[0];
- } else {
- return str.split(delimiter);
- }
- }
-
- @NotNull
- public static String makeString(@Nullable List tokens, char delimiter) {
- if (tokens == null) {
- return "";
- } else if (tokens.size() == 1) {
- return tokens.get(0);
- } else {
- StringBuilder buf = new StringBuilder();
- for (String token : tokens) {
- if (buf.length() > 0) {
- buf.append(delimiter);
- }
- buf.append(token);
- }
- return buf.toString();
- }
- }
-
- @Nullable
- public static String truncateString(@Nullable String str, int maxLength) {
- if (str != null && str.length() > maxLength) {
- return str.substring(0, maxLength);
- }
- return str;
- }
-
- public static String joinStrings(String divider, String ... array) {
- if (array == null) return "";
- StringBuilder str = new StringBuilder();
- for (int i = 0; i < array.length; i++) {
- if (i > 0) str.append(divider);
- str.append(array[i]);
- }
- return str.toString();
- }
-
- public static String joinStrings(String divider, Collection col) {
- if (col == null) return "";
- StringBuilder str = new StringBuilder();
- for (String item : col) {
- if (str.length() > 0) str.append(divider);
- str.append(item);
- }
- return str.toString();
- }
-
- public static boolean isEmptyTrimmed(@Nullable String str) {
- return str == null || str.length() == 0 || str.trim().length() == 0;
- }
-
- public static boolean isBitSet(int value, int mask) {
- return (value & mask) == mask;
- }
-
- public static boolean isBitSet(long value, long mask) {
- return (value & mask) == mask;
- }
-
- @Nullable
- public static > T valueOf(@NotNull Class type, @Nullable String name) {
- return valueOf(type, name, null, false);
- }
-
- @Nullable
- public static > T valueOf(@NotNull Class type, @Nullable String name, T defValue, boolean underscoreSpaces) {
- if (name == null) {
- return defValue;
- }
- name = name.trim();
- if (name.length() == 0) {
- return defValue;
- }
- if (underscoreSpaces) {
- name = name.replace(' ', '_');
- }
- try {
- return Enum.valueOf(type, name);
- } catch (Exception e) {
- e.printStackTrace();
- return defValue;
- }
- }
-
- public static > T valueOf(Class enumType, String str, T defValue) {
- if (isEmpty(str)) {
- return defValue;
- }
- try {
- return Enum.valueOf(enumType, str);
- } catch (Exception e) {
- e.printStackTrace();
- return defValue;
- }
- }
-
- @NotNull
- public static T getItem(@NotNull Collection collection, int index) {
- if (collection instanceof List) {
- return ((List) collection).get(index);
- } else {
- Iterator iter = collection.iterator();
- for (int i = 0; i < index; i++) {
- iter.next();
- }
- return iter.next();
- }
- }
-
- @NotNull
- public static > T fromOrdinal(Class enumClass, int ordinal) {
- T[] enumConstants = enumClass.getEnumConstants();
- for (T value : enumConstants) {
- if (value.ordinal() == ordinal) {
- return value;
- }
- }
- IllegalArgumentException error = new IllegalArgumentException("Invalid ordinal " + ordinal + " for type " + enumClass.getName());
- if (enumConstants.length == 0) {
- throw error;
- } else {
- error.printStackTrace(System.err);
- return enumConstants[0];
- }
- }
-
- @NotNull
- public static List filterCollection(@NotNull Collection> collection, @NotNull Class type) {
- List result = new ArrayList<>();
- for (Object item : collection) {
- if (type.isInstance(item)) {
- result.add(type.cast(item));
- }
- }
- return result;
- }
-
- @NotNull
- public static String escapeDisplayString(@NotNull final String delim) {
- StringBuilder str = new StringBuilder();
- for (int i = 0; i < delim.length(); i++) {
- char c = delim.charAt(i);
- if (c == '\n') {
- str.append("\\n");
- } else if (c == '\r') {
- str.append("\\r");
- } else if (c == '\t') {
- str.append("\\t");
- } else {
- str.append(c);
- }
- }
- return str.toString();
- }
-
- @NotNull
- public static String unescapeDisplayString(@NotNull final String delim) {
- return delim.replace("\\t", "\t").replace("\\n", "\n").replace("\\r", "\r");
- }
-
- public static int hashCode(@Nullable Object obj) {
- return obj == null ? 0 : obj.hashCode();
- }
-
- public static T getOption(Map options, String name, T defValue) {
- Object optionValue = options.get(name);
- if (optionValue == null) {
- return defValue;
- }
- return (T)optionValue;
- }
-
- public static boolean getOption(Map options, String name) {
- return getOption(options, name, false);
- }
-
- public static boolean getOption(Map options, String name, boolean defValue) {
- if (options == null) {
- return false;
- }
- Object optionValue = options.get(name);
- return getBoolean(optionValue, defValue);
- }
-
- public static Map makeStringMap(Map