diff --git a/CHANGES.md b/CHANGES.md index 228a3b1b..7b9331db 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,10 @@ * Add `Multiset/LongMultiset.forEach(Consumer action)`. +* Add `IntObjPair/LongObjPair/DoubleObjPair`. + +* Refactoring `Synchronized`. + * Improvements and bug fix. diff --git a/README.md b/README.md index bd1f2fa7..57e30f23 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ A general programming library in Java/Android. It's easy to learn and simple to [Indexed](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/Indexed_view.html), [If](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/If_view.html), [Try](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/Try_view.html), +[Synchronzed](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/Synchronzed_view.html), [Retry](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/Retry_view.html), [Multiset](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/Multiset_view.html), [Multimap](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/Multimap_view.html), @@ -56,7 +57,8 @@ A general programming library in Java/Android. It's easy to learn and simple to [HttpClient](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/HttpClient_view.html), [OkHttpRequest](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/OkHttpRequest_view.html), [Profiler](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/Profiler_view.html), -[CodeGenerator](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/CodeGenerator_view.html)... +[CodeGenerator](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/CodeGenerator_view.html), +[f](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/f_view.html)... * Primitive Mutable: [MutableBoolean](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/MutableBoolean_view.html), diff --git a/docs/IOUtil.gif b/docs/IOUtil.gif index 09037ace..8e70601a 100644 Binary files a/docs/IOUtil.gif and b/docs/IOUtil.gif differ diff --git a/docs/Multimap.gif b/docs/Multimap.gif index 4aa41b4c..b3186eb8 100644 Binary files a/docs/Multimap.gif and b/docs/Multimap.gif differ diff --git a/docs/OkHttpRequest.gif b/docs/OkHttpRequest.gif index 607721c8..a391ccf2 100644 Binary files a/docs/OkHttpRequest.gif and b/docs/OkHttpRequest.gif differ diff --git a/docs/Primitives.gif b/docs/Primitives.gif index 2d6a4047..aadb4a84 100644 Binary files a/docs/Primitives.gif and b/docs/Primitives.gif differ diff --git a/docs/SQLBuilder.gif b/docs/SQLBuilder.gif index 85788dc8..f1397ad0 100644 Binary files a/docs/SQLBuilder.gif and b/docs/SQLBuilder.gif differ diff --git a/docs/StringUtil.gif b/docs/StringUtil.gif index b855c7ee..c2d99aa0 100644 Binary files a/docs/StringUtil.gif and b/docs/StringUtil.gif differ diff --git a/docs/Synchronized.gif b/docs/Synchronized.gif index 60786990..ed74710c 100644 Binary files a/docs/Synchronized.gif and b/docs/Synchronized.gif differ diff --git a/docs/f.gif b/docs/f.gif new file mode 100644 index 00000000..75fec419 Binary files /dev/null and b/docs/f.gif differ diff --git a/docs/f_view.html b/docs/f_view.html new file mode 100644 index 00000000..9b566354 --- /dev/null +++ b/docs/f_view.html @@ -0,0 +1,9 @@ + + + + Panoramic View of f + + + + + \ No newline at end of file diff --git a/lib/abacus-util-1.5.5.jar b/lib/abacus-util-1.5.5.jar index 95fb23eb..52674c9b 100644 Binary files a/lib/abacus-util-1.5.5.jar and b/lib/abacus-util-1.5.5.jar differ diff --git a/lib/abacus-util-jdk7-1.5.5.jar b/lib/abacus-util-jdk7-1.5.5.jar index da127ce7..54d63315 100644 Binary files a/lib/abacus-util-jdk7-1.5.5.jar and b/lib/abacus-util-jdk7-1.5.5.jar differ diff --git a/src/com/landawn/abacus/http/okhttp/OkHttpRequest.java b/src/com/landawn/abacus/http/okhttp/OkHttpRequest.java index 38b616c7..f230b014 100644 --- a/src/com/landawn/abacus/http/okhttp/OkHttpRequest.java +++ b/src/com/landawn/abacus/http/okhttp/OkHttpRequest.java @@ -16,7 +16,9 @@ import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Method; import java.net.URL; +import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; @@ -29,12 +31,14 @@ import com.landawn.abacus.logging.Logger; import com.landawn.abacus.logging.LoggerFactory; import com.landawn.abacus.type.Type; +import com.landawn.abacus.util.ClassUtil; import com.landawn.abacus.util.ContinuableFuture; import com.landawn.abacus.util.IOUtil; import com.landawn.abacus.util.N; import com.landawn.abacus.util.Try; import okhttp3.CacheControl; +import okhttp3.FormBody; import okhttp3.Headers; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; @@ -181,6 +185,41 @@ public OkHttpRequest removeHeader(String name) { return this; } + public OkHttpRequest body(final Map formBodyByMap) { + if (N.isNullOrEmpty(formBodyByMap)) { + this.body = Util.EMPTY_REQUEST; + return this; + } + + final FormBody.Builder builder = new FormBody.Builder(); + + for (Map.Entry entry : formBodyByMap.entrySet()) { + builder.add(N.stringOf(entry.getKey()), N.stringOf(entry.getValue())); + } + + this.body = builder.build(); + return this; + } + + public OkHttpRequest body(final Object formBodyByEntity) { + if (formBodyByEntity == null) { + this.body = Util.EMPTY_REQUEST; + return this; + } + + final Class cls = formBodyByEntity.getClass(); + N.checkArgument(N.isEntity(cls), "{} is not an entity class with getter/setter methods", cls); + + final FormBody.Builder builder = new FormBody.Builder(); + + for (Map.Entry entry : ClassUtil.getPropGetMethodList(cls).entrySet()) { + builder.add(entry.getKey(), N.stringOf(ClassUtil.getPropValue(formBodyByEntity, entry.getValue()))); + } + + this.body = builder.build(); + return this; + } + public OkHttpRequest body(RequestBody body) { this.body = body; return this; @@ -257,11 +296,11 @@ protected T execute(Class resultClass, String method) throws IOException } } - public ContinuableFuture asyncGet() throws IOException { + public ContinuableFuture asyncGet() { return asyncGet(DEFAULT_EXECUTOR); } - public ContinuableFuture asyncGet(final Executor executor) throws IOException { + public ContinuableFuture asyncGet(final Executor executor) { return ContinuableFuture.call(new Try.Callable() { @Override public Response call() throws IOException { @@ -271,11 +310,11 @@ public Response call() throws IOException { }, executor); } - public ContinuableFuture asyncGet(final Class resultClass) throws IOException { + public ContinuableFuture asyncGet(final Class resultClass) { return asyncGet(resultClass, DEFAULT_EXECUTOR); } - public ContinuableFuture asyncGet(final Class resultClass, final Executor executor) throws IOException { + public ContinuableFuture asyncGet(final Class resultClass, final Executor executor) { return ContinuableFuture.call(new Try.Callable() { @Override public T call() throws IOException { @@ -285,11 +324,11 @@ public T call() throws IOException { }, executor); } - public ContinuableFuture asyncPost() throws IOException { + public ContinuableFuture asyncPost() { return asyncPost(DEFAULT_EXECUTOR); } - public ContinuableFuture asyncPost(final Executor executor) throws IOException { + public ContinuableFuture asyncPost(final Executor executor) { return ContinuableFuture.call(new Try.Callable() { @Override public Response call() throws IOException { @@ -299,11 +338,11 @@ public Response call() throws IOException { }, executor); } - public ContinuableFuture asyncPost(final Class resultClass) throws IOException { + public ContinuableFuture asyncPost(final Class resultClass) { return asyncPost(resultClass, DEFAULT_EXECUTOR); } - public ContinuableFuture asyncPost(final Class resultClass, final Executor executor) throws IOException { + public ContinuableFuture asyncPost(final Class resultClass, final Executor executor) { return ContinuableFuture.call(new Try.Callable() { @Override public T call() throws IOException { @@ -313,11 +352,11 @@ public T call() throws IOException { }, executor); } - public ContinuableFuture asyncPut() throws IOException { + public ContinuableFuture asyncPut() { return asyncPut(DEFAULT_EXECUTOR); } - public ContinuableFuture asyncPut(final Executor executor) throws IOException { + public ContinuableFuture asyncPut(final Executor executor) { return ContinuableFuture.call(new Try.Callable() { @Override public Response call() throws IOException { @@ -327,11 +366,11 @@ public Response call() throws IOException { }, executor); } - public ContinuableFuture asyncPut(final Class resultClass) throws IOException { + public ContinuableFuture asyncPut(final Class resultClass) { return asyncPut(resultClass, DEFAULT_EXECUTOR); } - public ContinuableFuture asyncPut(final Class resultClass, final Executor executor) throws IOException { + public ContinuableFuture asyncPut(final Class resultClass, final Executor executor) { return ContinuableFuture.call(new Try.Callable() { @Override public T call() throws IOException { @@ -341,11 +380,11 @@ public T call() throws IOException { }, executor); } - public ContinuableFuture asyncDelete() throws IOException { + public ContinuableFuture asyncDelete() { return asyncDelete(DEFAULT_EXECUTOR); } - public ContinuableFuture asyncDelete(final Executor executor) throws IOException { + public ContinuableFuture asyncDelete(final Executor executor) { return ContinuableFuture.call(new Try.Callable() { @Override public Response call() throws IOException { @@ -355,11 +394,11 @@ public Response call() throws IOException { }, executor); } - public ContinuableFuture asyncDelete(final Class resultClass) throws IOException { + public ContinuableFuture asyncDelete(final Class resultClass) { return asyncDelete(resultClass, DEFAULT_EXECUTOR); } - public ContinuableFuture asyncDelete(final Class resultClass, final Executor executor) throws IOException { + public ContinuableFuture asyncDelete(final Class resultClass, final Executor executor) { return ContinuableFuture.call(new Try.Callable() { @Override public T call() throws IOException { @@ -369,11 +408,11 @@ public T call() throws IOException { }, executor); } - public ContinuableFuture asyncHead() throws IOException { + public ContinuableFuture asyncHead() { return asyncHead(DEFAULT_EXECUTOR); } - public ContinuableFuture asyncHead(final Executor executor) throws IOException { + public ContinuableFuture asyncHead(final Executor executor) { return ContinuableFuture.call(new Try.Callable() { @Override public Response call() throws IOException { @@ -383,11 +422,11 @@ public Response call() throws IOException { }, executor); } - public ContinuableFuture asyncPatch() throws IOException { + public ContinuableFuture asyncPatch() { return asyncPatch(DEFAULT_EXECUTOR); } - public ContinuableFuture asyncPatch(final Executor executor) throws IOException { + public ContinuableFuture asyncPatch(final Executor executor) { return ContinuableFuture.call(new Try.Callable() { @Override public Response call() throws IOException { diff --git a/src/com/landawn/abacus/util/Fn.java b/src/com/landawn/abacus/util/Fn.java index cf1b1638..355d5b1a 100644 --- a/src/com/landawn/abacus/util/Fn.java +++ b/src/com/landawn/abacus/util/Fn.java @@ -2435,19 +2435,19 @@ public R apply(A a, B b, C c) { /** * Synchronized {@code Predicate} * - * @param target to synchronized on + * @param mutex to synchronized on * @param predicate * @return */ @Beta - public static Predicate sp(final Object target, final Predicate predicate) { - N.checkArgNotNull(target, "target"); + public static Predicate sp(final Object mutex, final Predicate predicate) { + N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(predicate, "predicate"); return new Predicate() { @Override public boolean test(T t) { - synchronized (target) { + synchronized (mutex) { return predicate.test(t); } } @@ -2457,20 +2457,20 @@ public boolean test(T t) { /** * Synchronized {@code Predicate} * - * @param target to synchronized on + * @param mutex to synchronized on * @param a * @param biPredicate * @return */ @Beta - public static Predicate sp(final Object target, final A a, final BiPredicate biPredicate) { - N.checkArgNotNull(target, "target"); + public static Predicate sp(final Object mutex, final A a, final BiPredicate biPredicate) { + N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biPredicate, "biPredicate"); return new Predicate() { @Override public boolean test(T t) { - synchronized (target) { + synchronized (mutex) { return biPredicate.test(a, t); } } @@ -2480,21 +2480,21 @@ public boolean test(T t) { /** * Synchronized {@code Predicate} * - * @param target to synchronized on + * @param mutex to synchronized on * @param a * @param b * @param triPredicate * @return */ @Beta - public static Predicate sp(final Object target, final A a, final B b, final TriPredicate triPredicate) { - N.checkArgNotNull(target, "target"); + public static Predicate sp(final Object mutex, final A a, final B b, final TriPredicate triPredicate) { + N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(triPredicate, "triPredicate"); return new Predicate() { @Override public boolean test(T t) { - synchronized (target) { + synchronized (mutex) { return triPredicate.test(a, b, t); } } @@ -2504,19 +2504,19 @@ public boolean test(T t) { /** * Synchronized {@code BiPredicate} * - * @param target to synchronized on + * @param mutex to synchronized on * @param biPredicate * @return */ @Beta - public static BiPredicate sp(final Object target, final BiPredicate biPredicate) { - N.checkArgNotNull(target, "target"); + public static BiPredicate sp(final Object mutex, final BiPredicate biPredicate) { + N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biPredicate, "biPredicate"); return new BiPredicate() { @Override public boolean test(T t, U u) { - synchronized (target) { + synchronized (mutex) { return biPredicate.test(t, u); } } @@ -2526,19 +2526,19 @@ public boolean test(T t, U u) { /** * Synchronized {@code Consumer} * - * @param target to synchronized on + * @param mutex to synchronized on * @param consumer * @return */ @Beta - public static Consumer sc(final Object target, final Consumer consumer) { - N.checkArgNotNull(target, "target"); + public static Consumer sc(final Object mutex, final Consumer consumer) { + N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(consumer, "consumer"); return new Consumer() { @Override public void accept(T t) { - synchronized (target) { + synchronized (mutex) { consumer.accept(t); } } @@ -2548,20 +2548,20 @@ public void accept(T t) { /** * Synchronized {@code Consumer} * - * @param target to synchronized on + * @param mutex to synchronized on * @param a * @param biConsumer * @return */ @Beta - public static Consumer sc(final Object target, final A a, final BiConsumer biConsumer) { - N.checkArgNotNull(target, "target"); + public static Consumer sc(final Object mutex, final A a, final BiConsumer biConsumer) { + N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biConsumer, "biConsumer"); return new Consumer() { @Override public void accept(T t) { - synchronized (target) { + synchronized (mutex) { biConsumer.accept(a, t); } } @@ -2571,19 +2571,19 @@ public void accept(T t) { /** * Synchronized {@code BiConsumer} * - * @param target to synchronized on + * @param mutex to synchronized on * @param biConsumer * @return */ @Beta - public static BiConsumer sc(final Object target, final BiConsumer biConsumer) { - N.checkArgNotNull(target, "target"); + public static BiConsumer sc(final Object mutex, final BiConsumer biConsumer) { + N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biConsumer, "biConsumer"); return new BiConsumer() { @Override public void accept(T t, U u) { - synchronized (target) { + synchronized (mutex) { biConsumer.accept(t, u); } } @@ -2593,19 +2593,19 @@ public void accept(T t, U u) { /** * Synchronized {@code Function} * - * @param target to synchronized on + * @param mutex to synchronized on * @param function * @return */ @Beta - public static Function sf(final Object target, final Function function) { - N.checkArgNotNull(target, "target"); + public static Function sf(final Object mutex, final Function function) { + N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(function, "function"); return new Function() { @Override public R apply(T t) { - synchronized (target) { + synchronized (mutex) { return function.apply(t); } } @@ -2615,20 +2615,20 @@ public R apply(T t) { /** * Synchronized {@code Function} * - * @param target to synchronized on + * @param mutex to synchronized on * @param u * @param biFunction * @return */ @Beta - public static Function sf(final Object target, final A a, final BiFunction biFunction) { - N.checkArgNotNull(target, "target"); + public static Function sf(final Object mutex, final A a, final BiFunction biFunction) { + N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biFunction, "biFunction"); return new Function() { @Override public R apply(T t) { - synchronized (target) { + synchronized (mutex) { return biFunction.apply(a, t); } } @@ -2638,19 +2638,19 @@ public R apply(T t) { /** * Synchronized {@code BiFunction} * - * @param target to synchronized on + * @param mutex to synchronized on * @param biFunction * @return */ @Beta - public static BiFunction sf(final Object target, final BiFunction biFunction) { - N.checkArgNotNull(target, "target"); + public static BiFunction sf(final Object mutex, final BiFunction biFunction) { + N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biFunction, "biFunction"); return new BiFunction() { @Override public R apply(T t, U u) { - synchronized (target) { + synchronized (mutex) { return biFunction.apply(t, u); } } diff --git a/src/com/landawn/abacus/util/IOUtil.java b/src/com/landawn/abacus/util/IOUtil.java index 7691ca28..81a27274 100644 --- a/src/com/landawn/abacus/util/IOUtil.java +++ b/src/com/landawn/abacus/util/IOUtil.java @@ -938,6 +938,16 @@ public static LineIterator iterate(final Reader reader) throws UncheckedIOExcept return new LineIterator(reader); } + /** + * + * @param file + * @param buf + * @return + */ + public static int read(final File file, final byte[] buf) throws UncheckedIOException { + return read(file, buf, 0, buf.length); + } + /** * * @param file @@ -959,6 +969,16 @@ public static int read(final File file, final byte[] buf, final int off, final i } } + /** + * + * @param is + * @param buf + * @return + */ + public static int read(final InputStream is, final byte[] buf) throws IOException { + return read(is, buf, 0, buf.length); + } + /** * * @param is @@ -996,6 +1016,14 @@ public static int read(final InputStream is, final byte[] buf, final int off, fi return n; } + public static int read(final File file, final char[] buf) throws UncheckedIOException { + return read(file, buf, 0, buf.length); + } + + public static int read(final File file, final char[] buf, final Charset charset) throws UncheckedIOException { + return read(file, buf, 0, buf.length, charset); + } + /** * * @param file @@ -1005,10 +1033,14 @@ public static int read(final InputStream is, final byte[] buf, final int off, fi * @return */ public static int read(final File file, final char[] buf, final int off, final int len) throws UncheckedIOException { + return read(file, buf, off, len, Charsets.UTF_8); + } + + public static int read(final File file, final char[] buf, final int off, final int len, final Charset charset) throws UncheckedIOException { Reader reader = null; try { - reader = new FileReader(file); + reader = new InputStreamReader(new FileInputStream(file), charset == null ? Charsets.UTF_8 : charset); return read(reader, buf, off, len); } catch (IOException e) { throw new UncheckedIOException(e); @@ -1017,6 +1049,10 @@ public static int read(final File file, final char[] buf, final int off, final i } } + public static int read(final Reader reader, final char[] buf) throws IOException { + return read(reader, buf, 0, buf.length); + } + /** * * @param reader diff --git a/src/com/landawn/abacus/util/ImmutableEntry.java b/src/com/landawn/abacus/util/ImmutableEntry.java index 8211d9ed..063764fa 100644 --- a/src/com/landawn/abacus/util/ImmutableEntry.java +++ b/src/com/landawn/abacus/util/ImmutableEntry.java @@ -35,6 +35,14 @@ public ImmutableEntry(Map.Entry entry) { super(entry); } + public static ImmutableEntry of(K key, V value) { + return new ImmutableEntry<>(key, value); + } + + public static ImmutableEntry from(Map.Entry entry) { + return new ImmutableEntry(entry.getKey(), entry.getValue()); + } + /** * Always throw UnsupportedOperationException. * diff --git a/src/com/landawn/abacus/util/Indexed.java b/src/com/landawn/abacus/util/Indexed.java index a17db7e9..3fce80f3 100644 --- a/src/com/landawn/abacus/util/Indexed.java +++ b/src/com/landawn/abacus/util/Indexed.java @@ -76,12 +76,22 @@ public T value() { @Override public int hashCode() { - return (int) index + (value == null ? 0 : value.hashCode()) * 31; + return (int) (index * 31 + (value == null ? 0 : value.hashCode())); } @Override public boolean equals(Object obj) { - return obj instanceof Indexed && ((Indexed) obj).index == index && N.equals(((Indexed) obj).value, value); + if (this == obj) { + return true; + } + + if (obj instanceof Timed) { + final Indexed other = (Indexed) obj; + + return this.index == other.index && N.equals(this.value, other.value); + } + + return false; } @Override diff --git a/src/com/landawn/abacus/util/Multimap.java b/src/com/landawn/abacus/util/Multimap.java index dc8d3da1..549a8a01 100644 --- a/src/com/landawn/abacus/util/Multimap.java +++ b/src/com/landawn/abacus/util/Multimap.java @@ -1053,17 +1053,6 @@ public void forEachValue(final Try.Consumer } } - /** - * - * @param action - * @throws X - * @deprecated replaced by {@code flatForEachValue(com.landawn.abacus.util.Try.Consumer)} - */ - @Deprecated - public void flatForEach(Try.Consumer action) throws X { - flatForEachValue(action); - } - public void flatForEachValue(Try.Consumer action) throws X { N.checkArgNotNull(action); diff --git a/src/com/landawn/abacus/util/N.java b/src/com/landawn/abacus/util/N.java index 0d408a02..02819c1f 100644 --- a/src/com/landawn/abacus/util/N.java +++ b/src/com/landawn/abacus/util/N.java @@ -622,6 +622,26 @@ public static Type typeOf(final Class cls) { return (Type) type; } + /** + * + * @param targetClass + * @param str + * @return the default value of the specified targetClass if the specified string is null. + */ + @SuppressWarnings("unchecked") + public static T valueOf(final Class targetClass, final String str) { + return (str == null) ? defaultValueOf(targetClass) : (T) N.typeOf(targetClass).valueOf(str); + } + + @SuppressWarnings("unchecked") + public static T defaultValueOf(final Class cls) { + return (T) N.typeOf(cls).defaultValue(); + } + + public static T defaultIfNull(final T obj, final T defaultForNull) { + return obj == null ? defaultForNull : obj; + } + @SuppressWarnings("unchecked") // @SafeVarargs // public static List> typeOf(final Class... classes) { @@ -710,26 +730,6 @@ public static String stringOf(final Object obj) { return (obj == null) ? null : N.typeOf(obj.getClass()).stringOf(obj); } - /** - * - * @param targetClass - * @param str - * @return the default value of the specified targetClass if the specified string is null. - */ - @SuppressWarnings("unchecked") - public static T valueOf(final Class targetClass, final String str) { - return (str == null) ? defaultValueOf(targetClass) : (T) N.typeOf(targetClass).valueOf(str); - } - - @SuppressWarnings("unchecked") - public static T defaultValueOf(final Class cls) { - return (T) N.typeOf(cls).defaultValue(); - } - - public static T defaultIfNull(final T obj, final T defaultForNull) { - return obj == null ? defaultForNull : obj; - } - public static > List enumListOf(final Class enumClass) { List enumList = (List) enumListPool.get(enumClass); @@ -1714,6 +1714,8 @@ public static Object[] toArray(final Collection c, final int fromIndex, final } public static A[] toArray(final Collection c, final A[] a) { + N.checkArgNotNull(a); + if (N.isNullOrEmpty(c)) { return a; } @@ -1723,6 +1725,7 @@ public static A[] toArray(final Collection c, final A[] a) { public static A[] toArray(final Collection c, final int fromIndex, final int toIndex, final A[] a) { N.checkFromToIndex(fromIndex, toIndex, size(c)); + N.checkArgNotNull(a); if (N.isNullOrEmpty(c)) { return a; @@ -1749,7 +1752,48 @@ public static A[] toArray(final Collection c, final int from } } + public static A[] toArray(final Collection c, final IntFunction arraySupplier) { + N.checkArgNotNull(arraySupplier); + + if (N.isNullOrEmpty(c)) { + return arraySupplier.apply(0); + } + + return toArray(c, arraySupplier); + } + + public static A[] toArray(final Collection c, final int fromIndex, final int toIndex, final IntFunction arraySupplier) { + N.checkArgNotNull(arraySupplier); + N.checkFromToIndex(fromIndex, toIndex, size(c)); + + if (N.isNullOrEmpty(c)) { + return arraySupplier.apply(0); + } else if (fromIndex == 0 || toIndex == c.size()) { + return c.toArray(arraySupplier.apply(c.size())); + } else if (c instanceof List) { + return ((List) c).subList(fromIndex, toIndex).toArray(arraySupplier.apply(toIndex - fromIndex)); + } else { + final A[] res = arraySupplier.apply(toIndex - fromIndex); + final Iterator iter = c.iterator(); + int idx = 0; + + while (idx < fromIndex && iter.hasNext()) { + iter.next(); + idx++; + } + + while (idx < toIndex && iter.hasNext()) { + res[idx - fromIndex] = iter.next(); + idx++; + } + + return res; + } + } + public static A[] toArray(final Class targetClass, final Collection c) { + N.checkArgNotNull(targetClass); + if (N.isNullOrEmpty(c)) { return N.newArray(targetClass.getComponentType(), 0); } @@ -1758,6 +1802,7 @@ public static A[] toArray(final Class targetClass, final C } public static A[] toArray(final Class targetClass, final Collection c, final int fromIndex, final int toIndex) { + N.checkArgNotNull(targetClass); N.checkFromToIndex(fromIndex, toIndex, size(c)); final A[] res = N.newArray(targetClass.getComponentType(), toIndex - fromIndex); @@ -1902,19 +1947,19 @@ public static char[] toCharArray(final Collection c, final int fromIn return result; } - public static byte[] toByteArray(final Collection c) { + public static byte[] toByteArray(final Collection c) { return toByteArray(c, (byte) 0); } - public static byte[] toByteArray(final Collection c, final int fromIndex, final int toIndex) { + public static byte[] toByteArray(final Collection c, final int fromIndex, final int toIndex) { return toByteArray(c, fromIndex, toIndex, (byte) 0); } - public static byte[] toByteArray(final Collection c, final byte defaultForNull) { + public static byte[] toByteArray(final Collection c, final byte defaultForNull) { return toByteArray(c, 0, size(c), defaultForNull); } - public static byte[] toByteArray(final Collection c, final int fromIndex, final int toIndex, final byte defaultForNull) { + public static byte[] toByteArray(final Collection c, final int fromIndex, final int toIndex, final byte defaultForNull) { N.checkFromToIndex(fromIndex, toIndex, size(c)); if (fromIndex == toIndex) { @@ -1925,18 +1970,18 @@ public static byte[] toByteArray(final Collection c, final int fromIndex, byte[] result = new byte[len]; if (c instanceof List && c instanceof RandomAccess) { - final List list = (List) c; - Byte val = null; + final List list = (List) c; + Number val = null; for (int i = 0; i < len; i++) { if ((val = list.get(i + fromIndex)) == null) { result[i] = defaultForNull; } else { - result[i] = val; + result[i] = val.byteValue(); } } } else { - final Iterator iter = c.iterator(); + final Iterator iter = c.iterator(); if (fromIndex > 0) { int offset = 0; @@ -1946,13 +1991,13 @@ public static byte[] toByteArray(final Collection c, final int fromIndex, } } - Byte val = null; + Number val = null; for (int i = 0; i < len; i++) { if ((val = iter.next()) == null) { result[i] = defaultForNull; } else { - result[i] = val; + result[i] = val.byteValue(); } } } @@ -1960,19 +2005,19 @@ public static byte[] toByteArray(final Collection c, final int fromIndex, return result; } - public static short[] toShortArray(final Collection c) { + public static short[] toShortArray(final Collection c) { return toShortArray(c, (short) 0); } - public static short[] toShortArray(final Collection c, final int fromIndex, final int toIndex) { + public static short[] toShortArray(final Collection c, final int fromIndex, final int toIndex) { return toShortArray(c, fromIndex, toIndex, (short) 0); } - public static short[] toShortArray(final Collection c, final short defaultForNull) { + public static short[] toShortArray(final Collection c, final short defaultForNull) { return toShortArray(c, 0, size(c), defaultForNull); } - public static short[] toShortArray(final Collection c, final int fromIndex, final int toIndex, final short defaultForNull) { + public static short[] toShortArray(final Collection c, final int fromIndex, final int toIndex, final short defaultForNull) { N.checkFromToIndex(fromIndex, toIndex, size(c)); if (fromIndex == toIndex) { @@ -1983,18 +2028,18 @@ public static short[] toShortArray(final Collection c, final int fromInde short[] result = new short[len]; if (c instanceof List && c instanceof RandomAccess) { - final List list = (List) c; - Short val = null; + final List list = (List) c; + Number val = null; for (int i = 0; i < len; i++) { if ((val = list.get(i + fromIndex)) == null) { result[i] = defaultForNull; } else { - result[i] = val; + result[i] = val.shortValue(); } } } else { - final Iterator iter = c.iterator(); + final Iterator iter = c.iterator(); if (fromIndex > 0) { int offset = 0; @@ -2004,13 +2049,13 @@ public static short[] toShortArray(final Collection c, final int fromInde } } - Short val = null; + Number val = null; for (int i = 0; i < len; i++) { if ((val = iter.next()) == null) { result[i] = defaultForNull; } else { - result[i] = val; + result[i] = val.shortValue(); } } } @@ -2018,19 +2063,19 @@ public static short[] toShortArray(final Collection c, final int fromInde return result; } - public static int[] toIntArray(final Collection c) { + public static int[] toIntArray(final Collection c) { return toIntArray(c, 0); } - public static int[] toIntArray(final Collection c, final int fromIndex, final int toIndex) { + public static int[] toIntArray(final Collection c, final int fromIndex, final int toIndex) { return toIntArray(c, fromIndex, toIndex, 0); } - public static int[] toIntArray(final Collection c, final int defaultForNull) { + public static int[] toIntArray(final Collection c, final int defaultForNull) { return toIntArray(c, 0, size(c), defaultForNull); } - public static int[] toIntArray(final Collection c, final int fromIndex, final int toIndex, final int defaultForNull) { + public static int[] toIntArray(final Collection c, final int fromIndex, final int toIndex, final int defaultForNull) { N.checkFromToIndex(fromIndex, toIndex, size(c)); if (fromIndex == toIndex) { @@ -2041,18 +2086,18 @@ public static int[] toIntArray(final Collection c, final int fromIndex, int[] result = new int[len]; if (c instanceof List && c instanceof RandomAccess) { - final List list = (List) c; - Integer val = null; + final List list = (List) c; + Number val = null; for (int i = 0; i < len; i++) { if ((val = list.get(i + fromIndex)) == null) { result[i] = defaultForNull; } else { - result[i] = val; + result[i] = val.intValue(); } } } else { - final Iterator iter = c.iterator(); + final Iterator iter = c.iterator(); if (fromIndex > 0) { int offset = 0; @@ -2062,13 +2107,13 @@ public static int[] toIntArray(final Collection c, final int fromIndex, } } - Integer val = null; + Number val = null; for (int i = 0; i < len; i++) { if ((val = iter.next()) == null) { result[i] = defaultForNull; } else { - result[i] = val; + result[i] = val.intValue(); } } } @@ -2076,19 +2121,19 @@ public static int[] toIntArray(final Collection c, final int fromIndex, return result; } - public static long[] toLongArray(final Collection c) { + public static long[] toLongArray(final Collection c) { return toLongArray(c, 0); } - public static long[] toLongArray(final Collection c, final int fromIndex, final int toIndex) { + public static long[] toLongArray(final Collection c, final int fromIndex, final int toIndex) { return toLongArray(c, fromIndex, toIndex, 0); } - public static long[] toLongArray(final Collection c, final long defaultForNull) { + public static long[] toLongArray(final Collection c, final long defaultForNull) { return toLongArray(c, 0, size(c), defaultForNull); } - public static long[] toLongArray(final Collection c, final int fromIndex, final int toIndex, final long defaultForNull) { + public static long[] toLongArray(final Collection c, final int fromIndex, final int toIndex, final long defaultForNull) { N.checkFromToIndex(fromIndex, toIndex, size(c)); if (fromIndex == toIndex) { @@ -2099,18 +2144,18 @@ public static long[] toLongArray(final Collection c, final int fromIndex, long[] result = new long[len]; if (c instanceof List && c instanceof RandomAccess) { - final List list = (List) c; - Long val = null; + final List list = (List) c; + Number val = null; for (int i = 0; i < len; i++) { if ((val = list.get(i + fromIndex)) == null) { result[i] = defaultForNull; } else { - result[i] = val; + result[i] = val.longValue(); } } } else { - final Iterator iter = c.iterator(); + final Iterator iter = c.iterator(); if (fromIndex > 0) { int offset = 0; @@ -2120,13 +2165,13 @@ public static long[] toLongArray(final Collection c, final int fromIndex, } } - Long val = null; + Number val = null; for (int i = 0; i < len; i++) { if ((val = iter.next()) == null) { result[i] = defaultForNull; } else { - result[i] = val; + result[i] = val.longValue(); } } } @@ -2134,19 +2179,19 @@ public static long[] toLongArray(final Collection c, final int fromIndex, return result; } - public static float[] toFloatArray(final Collection c) { + public static float[] toFloatArray(final Collection c) { return toFloatArray(c, 0); } - public static float[] toFloatArray(final Collection c, final int fromIndex, final int toIndex) { + public static float[] toFloatArray(final Collection c, final int fromIndex, final int toIndex) { return toFloatArray(c, fromIndex, toIndex, 0); } - public static float[] toFloatArray(final Collection c, final float defaultForNull) { + public static float[] toFloatArray(final Collection c, final float defaultForNull) { return toFloatArray(c, 0, size(c), defaultForNull); } - public static float[] toFloatArray(final Collection c, final int fromIndex, final int toIndex, final float defaultForNull) { + public static float[] toFloatArray(final Collection c, final int fromIndex, final int toIndex, final float defaultForNull) { N.checkFromToIndex(fromIndex, toIndex, size(c)); if (fromIndex == toIndex) { @@ -2157,18 +2202,18 @@ public static float[] toFloatArray(final Collection c, final int fromInde float[] result = new float[len]; if (c instanceof List && c instanceof RandomAccess) { - final List list = (List) c; - Float val = null; + final List list = (List) c; + Number val = null; for (int i = 0; i < len; i++) { if ((val = list.get(i + fromIndex)) == null) { result[i] = defaultForNull; } else { - result[i] = val; + result[i] = val.floatValue(); } } } else { - final Iterator iter = c.iterator(); + final Iterator iter = c.iterator(); if (fromIndex > 0) { int offset = 0; @@ -2178,13 +2223,13 @@ public static float[] toFloatArray(final Collection c, final int fromInde } } - Float val = null; + Number val = null; for (int i = 0; i < len; i++) { if ((val = iter.next()) == null) { result[i] = defaultForNull; } else { - result[i] = val; + result[i] = val.floatValue(); } } } @@ -2192,19 +2237,19 @@ public static float[] toFloatArray(final Collection c, final int fromInde return result; } - public static double[] toDoubleArray(final Collection c) { + public static double[] toDoubleArray(final Collection c) { return toDoubleArray(c, 0); } - public static double[] toDoubleArray(final Collection c, final int fromIndex, final int toIndex) { + public static double[] toDoubleArray(final Collection c, final int fromIndex, final int toIndex) { return toDoubleArray(c, fromIndex, toIndex, 0); } - public static double[] toDoubleArray(final Collection c, final double defaultForNull) { + public static double[] toDoubleArray(final Collection c, final double defaultForNull) { return toDoubleArray(c, 0, size(c), defaultForNull); } - public static double[] toDoubleArray(final Collection c, final int fromIndex, final int toIndex, final double defaultForNull) { + public static double[] toDoubleArray(final Collection c, final int fromIndex, final int toIndex, final double defaultForNull) { N.checkFromToIndex(fromIndex, toIndex, size(c)); if (fromIndex == toIndex) { @@ -2215,18 +2260,18 @@ public static double[] toDoubleArray(final Collection c, final int fromI double[] result = new double[len]; if (c instanceof List && c instanceof RandomAccess) { - final List list = (List) c; - Double val = null; + final List list = (List) c; + Number val = null; for (int i = 0; i < len; i++) { if ((val = list.get(i + fromIndex)) == null) { result[i] = defaultForNull; } else { - result[i] = val; + result[i] = val.doubleValue(); } } } else { - final Iterator iter = c.iterator(); + final Iterator iter = c.iterator(); if (fromIndex > 0) { int offset = 0; @@ -2236,13 +2281,13 @@ public static double[] toDoubleArray(final Collection c, final int fromI } } - Double val = null; + Number val = null; for (int i = 0; i < len; i++) { if ((val = iter.next()) == null) { result[i] = defaultForNull; } else { - result[i] = val; + result[i] = val.doubleValue(); } } } @@ -4423,1212 +4468,784 @@ public static void eraseAll(final Object entity) { } } - public static int compare(final boolean a, final boolean b) { - return (a == b) ? 0 : (a ? 1 : -1); - } - - public static int compare(final byte a, final byte b) { - return (a < b) ? -1 : ((a == b) ? 0 : 1); - } - - public static int compare(final short a, final short b) { - return (a < b) ? -1 : ((a == b) ? 0 : 1); + /** + * Returns an empty {@code List} that is immutable. + * + * @return + * @see Collections#emptyList() + */ + public static List emptyList() { + return EMPTY_LIST; } - public static int compare(final int a, final int b) { - return (a < b) ? -1 : ((a == b) ? 0 : 1); + /** + * Returns an empty {@code Set} that is immutable. + * + * @return + * @see Collections#emptySet() + */ + public static Set emptySet() { + return EMPTY_SET; } - public static int compare(final long a, final long b) { - return (a < b) ? -1 : ((a == b) ? 0 : 1); + /** + * Returns an empty {@code SortedSet} that is immutable. + * + * @return + * @see Collections#emptySortedSet() + */ + public static SortedSet emptySortedSet() { + return EMPTY_SORTED_SET; } - public static int compare(final float a, final float b) { - return Float.compare(a, b); + /** + * Returns an empty {@code emptyNavigableSet} that is immutable. + * + * @return + * @see Collections#emptyNavigableSet() + */ + public static NavigableSet emptyNavigableSet() { + return EMPTY_NAVIGABLE_SET; } - public static int compare(final double a, final double b) { - return Double.compare(a, b); + /** + * Returns an empty {@code Map} that is immutable. + * + * @return + * @see Collections#emptyMap() + */ + public static Map emptyMap() { + return EMPTY_MAP; } - public static > int compare(final T a, final T b) { - return a == null ? (b == null ? 0 : -1) : (b == null ? 1 : a.compareTo(b)); + /** + * Returns an empty {@code SortedMap} that is immutable. + * + * @return + * @see Collections#emptySortedMap() + */ + public static SortedMap emptySortedMap() { + return EMPTY_SORTED_MAP; } /** - * Returns 0 if the arguments are identical and {@code c.compare(a, b)} - * otherwise. Consequently, if both arguments are {@code null} 0 is - * returned. - * - *

- * Note that if one of the arguments is {@code null}, a - * {@code NullPointerException} may or may not be thrown depending on what - * ordering policy, if any, the {@link Comparator Comparator} chooses to - * have for {@code null} values. - * - * @param - * the type of the objects being compared - * @param a - * an object - * @param b - * an object to be compared with {@code a} - * @param cmp - * the {@code Comparator} to compare the first two arguments - * @return 0 if the arguments are identical and {@code c.compare(a, b)} - * otherwise. - * @see Comparable - * @see Comparator + * Returns an empty {@code NavigableMap} that is immutable. + * + * @return + * @see Collections#emptyNavigableMap() */ - public static int compare(final T a, final T b, final Comparator cmp) { - return a == null ? (b == null ? 0 : -1) : (b == null ? 1 : (cmp == null ? NATURAL_ORDER : cmp).compare(a, b)); + public static NavigableMap emptyNavigableMap() { + return EMPTY_NAVIGABLE_MAP; } /** - * Continue to compare the pairs of values (a1, b1), (a2, b2) until they're not equal. - * 0 is returned if all of the pairs of values are equal. + * Returns an empty {@code Iterator} that is immutable. * - * @param a1 - * @param b1 - * @param a2 - * @param b2 * @return + * @see Collections#emptyIterator() */ - public static , T2 extends Comparable> int compare(T1 a1, T1 b1, T2 a2, T2 b2) { - int res = N.compare(a1, b1); - - return res == 0 ? N.compare(a2, b2) : res; + public static Iterator emptyIterator() { + return EMPTY_ITERATOR; } /** - * Continue to compare the pairs of values (a1, b1), (a2, b2), (a3, b3) until they're not equal. - * 0 is returned if all of the pairs of values are equal. + * Returns an empty {@code ListIterator} that is immutable. * - * @param a1 - * @param b1 - * @param a2 - * @param b2 - * @param a3 - * @param b3 * @return + * @see Collections#emptyListIterator() */ - public static , T2 extends Comparable, T3 extends Comparable> int compare(T1 a1, T1 b1, T2 a2, T2 b2, T3 a3, T3 b3) { - int res = 0; + public static ListIterator emptyListIterator() { + return EMPTY_LIST_ITERATOR; + } - if ((res = N.compare(a1, b1)) != 0) { - return res; - } else if ((res = N.compare(a2, b2)) != 0) { - return res; - } + public static boolean anyNull(final T a, final T b) { + return a == null || b == null; + } - return N.compare(a3, b3); + public static boolean anyNull(final T a, final T b, final T c) { + return a == null || b == null || c == null; } - /** - * Continue to compare the pairs of values (a1, b1), (a2, b2), (a3, b3), (a4, b4) until they're not equal. - * 0 is returned if all of the pairs of values are equal. - * - * @param a1 - * @param b1 - * @param a2 - * @param b2 - * @param a3 - * @param b3 - * @param a4 - * @param b4 - * @return - */ - public static , T2 extends Comparable, T3 extends Comparable, T4 extends Comparable> int compare(T1 a1, T1 b1, T2 a2, - T2 b2, T3 a3, T3 b3, T4 a4, T4 b4) { - int res = 0; + @SafeVarargs + public static boolean anyNull(final T... a) { + if (N.isNullOrEmpty(a)) { + return false; + } - if ((res = N.compare(a1, b1)) != 0) { - return res; - } else if ((res = N.compare(a2, b2)) != 0) { - return res; - } else if ((res = N.compare(a3, b3)) != 0) { - return res; + for (T e : a) { + if (e == null) { + return true; + } } - return N.compare(a4, b4); + return false; } - /** - * Continue to compare the pairs of values (a1, b1), (a2, b2), (a3, b3), (a4, b4), (a5, b5) until they're not equal. - * 0 is returned if all of the pairs of values are equal. - * - * @param a1 - * @param b1 - * @param a2 - * @param b2 - * @param a3 - * @param b3 - * @param a4 - * @param b4 - * @param a5 - * @param b5 - * @return - */ - public static , T2 extends Comparable, T3 extends Comparable, T4 extends Comparable, T5 extends Comparable> int compare( - T1 a1, T1 b1, T2 a2, T2 b2, T3 a3, T3 b3, T4 a4, T4 b4, T5 a5, T5 b5) { - int res = 0; - - if ((res = N.compare(a1, b1)) != 0) { - return res; - } else if ((res = N.compare(a2, b2)) != 0) { - return res; - } else if ((res = N.compare(a3, b3)) != 0) { - return res; - } else if ((res = N.compare(a4, b4)) != 0) { - return res; + public static boolean anyNull(final Collection c) { + if (N.isNullOrEmpty(c)) { + return false; } - return N.compare(a5, b5); - } - - /** - * Continue to compare the pairs of values (a1, b1), (a2, b2), (a3, b3), (a4, b4), (a5, b5), (a6, b6) until they're not equal. - * 0 is returned if all of the pairs of values are equal. - * - * @param a1 - * @param b1 - * @param a2 - * @param b2 - * @param a3 - * @param b3 - * @param a4 - * @param b4 - * @param a5 - * @param b5 - * @param a6 - * @param b6 - * @return - */ - public static , T2 extends Comparable, T3 extends Comparable, T4 extends Comparable, T5 extends Comparable, T6 extends Comparable> int compare( - T1 a1, T1 b1, T2 a2, T2 b2, T3 a3, T3 b3, T4 a4, T4 b4, T5 a5, T5 b5, T6 a6, T6 b6) { - int res = 0; - - if ((res = N.compare(a1, b1)) != 0) { - return res; - } else if ((res = N.compare(a2, b2)) != 0) { - return res; - } else if ((res = N.compare(a3, b3)) != 0) { - return res; - } else if ((res = N.compare(a4, b4)) != 0) { - return res; - } else if ((res = N.compare(a5, b5)) != 0) { - return res; + for (T e : c) { + if (e == null) { + return true; + } } - return N.compare(a6, b6); + return false; } - /** - * Continue to compare the pairs of values (a1, b1), (a2, b2), (a3, b3), (a4, b4), (a5, b5), (a6, b6), (a7, b7) until they're not equal. - * 0 is returned if all of the pairs of values are equal. - * - * @param a1 - * @param b1 - * @param a2 - * @param b2 - * @param a3 - * @param b3 - * @param a4 - * @param b4 - * @param a5 - * @param b5 - * @param a6 - * @param b6 - * @param a7 - * @param b7 - * @return - */ - public static , T2 extends Comparable, T3 extends Comparable, T4 extends Comparable, T5 extends Comparable, T6 extends Comparable, T7 extends Comparable> int compare( - T1 a1, T1 b1, T2 a2, T2 b2, T3 a3, T3 b3, T4 a4, T4 b4, T5 a5, T5 b5, T6 a6, T6 b6, T7 a7, T7 b7) { - int res = 0; - - if ((res = N.compare(a1, b1)) != 0) { - return res; - } else if ((res = N.compare(a2, b2)) != 0) { - return res; - } else if ((res = N.compare(a3, b3)) != 0) { - return res; - } else if ((res = N.compare(a4, b4)) != 0) { - return res; - } else if ((res = N.compare(a5, b5)) != 0) { - return res; - } else if ((res = N.compare(a6, b6)) != 0) { - return res; - } + public static boolean allNull(final T a, final T b) { + return a == null && b == null; + } - return N.compare(a7, b7); + public static boolean allNull(final T a, final T b, final T c) { + return a == null && b == null && c == null; } - public static int compare(final boolean[] a, final boolean[] b) { + @SafeVarargs + public static boolean allNull(final T... a) { if (N.isNullOrEmpty(a)) { - return N.isNullOrEmpty(b) ? 0 : -1; - } else if (N.isNullOrEmpty(b)) { - return 1; + return true; } - for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { - if (a[i] != b[i]) { - return a[i] ? 1 : -1; + for (T e : a) { + if (e != null) { + return false; } } - return a.length - b.length; + return true; } - public static int compare(final char[] a, final char[] b) { - if (N.isNullOrEmpty(a)) { - return N.isNullOrEmpty(b) ? 0 : -1; - } else if (N.isNullOrEmpty(b)) { - return 1; + public static boolean allNull(final Collection c) { + if (N.isNullOrEmpty(c)) { + return true; } - for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { - if (a[i] != b[i]) { - return a[i] > b[i] ? 1 : -1; + for (T e : c) { + if (e != null) { + return false; } } - return a.length - b.length; + return true; } - public static int compare(final byte[] a, final byte[] b) { - if (N.isNullOrEmpty(a)) { - return N.isNullOrEmpty(b) ? 0 : -1; - } else if (N.isNullOrEmpty(b)) { - return 1; + public static Nullable first(final Collection c) { + if (N.isNullOrEmpty(c)) { + return Nullable.empty(); } - for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { - if (a[i] != b[i]) { - return a[i] > b[i] ? 1 : -1; - } + if (c instanceof List && c instanceof RandomAccess) { + return Nullable.of(((List) c).get(0)); + } else { + return Nullable.of(c.iterator().next()); } - - return a.length - b.length; } - public static int compare(final short[] a, final short[] b) { - if (N.isNullOrEmpty(a)) { - return N.isNullOrEmpty(b) ? 0 : -1; - } else if (N.isNullOrEmpty(b)) { - return 1; + public static Nullable last(final Collection c) { + if (N.isNullOrEmpty(c)) { + return Nullable.empty(); } - for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { - if (a[i] != b[i]) { - return a[i] > b[i] ? 1 : -1; + if (c instanceof List) { + final List list = (List) c; + + if (c instanceof RandomAccess) { + return Nullable.of(list.get(c.size() - 1)); + } else { + return Nullable.of(list.listIterator(list.size()).previous()); } + } else if (c instanceof Deque) { + return Nullable.of(((Deque) c).descendingIterator().next()); + } else { + return Iterators.last(c.iterator()); } + } - return a.length - b.length; + public static Optional firstNonNull(final T a, final T b) { + return a != null ? Optional.of(a) : (b != null ? Optional.of(b) : Optional. empty()); } - public static int compare(final int[] a, final int[] b) { + public static Optional firstNonNull(final T a, final T b, final T c) { + return a != null ? Optional.of(a) : (b != null ? Optional.of(b) : (c != null ? Optional.of(c) : Optional. empty())); + } + + @SafeVarargs + public static Optional firstNonNull(final T... a) { if (N.isNullOrEmpty(a)) { - return N.isNullOrEmpty(b) ? 0 : -1; - } else if (N.isNullOrEmpty(b)) { - return 1; + return Optional.empty(); } - for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { - if (a[i] != b[i]) { - return a[i] > b[i] ? 1 : -1; + for (T e : a) { + if (e != null) { + return Optional.of(e); } } - return a.length - b.length; + return Optional.empty(); } - public static int compare(final long[] a, final long[] b) { - if (N.isNullOrEmpty(a)) { - return N.isNullOrEmpty(b) ? 0 : -1; - } else if (N.isNullOrEmpty(b)) { - return 1; + public static Optional firstNonNull(final Collection c) { + if (N.isNullOrEmpty(c)) { + return Optional.empty(); } - for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { - if (a[i] != b[i]) { - return a[i] > b[i] ? 1 : -1; + for (T e : c) { + if (e != null) { + return Optional.of(e); } } - return a.length - b.length; + return Optional.empty(); } - public static int compare(final float[] a, final float[] b) { + public static Optional lastNonNull(final T a, final T b) { + return b != null ? Optional.of(b) : (a != null ? Optional.of(a) : Optional. empty()); + } + + public static Optional lastNonNull(final T a, final T b, final T c) { + return c != null ? Optional.of(c) : (b != null ? Optional.of(b) : (a != null ? Optional.of(a) : Optional. empty())); + } + + @SafeVarargs + public static Optional lastNonNull(final T... a) { if (N.isNullOrEmpty(a)) { - return N.isNullOrEmpty(b) ? 0 : -1; - } else if (N.isNullOrEmpty(b)) { - return 1; + return Optional.empty(); } - int value = 0; - - for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { - if ((value = Float.compare(a[i], b[i])) != 0) { - return value; + for (int i = a.length - 1; i >= 0; i--) { + if (a[i] != null) { + return Optional.of(a[i]); } } - return a.length - b.length; + return Optional.empty(); } - public static int compare(final double[] a, final double[] b) { - if (N.isNullOrEmpty(a)) { - return N.isNullOrEmpty(b) ? 0 : -1; - } else if (N.isNullOrEmpty(b)) { - return 1; - } - - int value = 0; - - for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { - if ((value = Double.compare(a[i], b[i])) != 0) { - return value; - } + public static Optional lastNonNull(final Collection c) { + if (N.isNullOrEmpty(c)) { + return Optional.empty(); } - return a.length - b.length; - } - - public static > int compare(final T[] a, final T[] b) { - final Comparator cmp = NULL_MIN_COMPARATOR; - return compare(a, b, cmp); - } - - public static int compare(final T[] a, final T[] b, Comparator cmp) { - if (N.isNullOrEmpty(a)) { - return N.isNullOrEmpty(b) ? 0 : -1; - } else if (N.isNullOrEmpty(b)) { - return 1; - } + if (c instanceof List) { + final List list = (List) c; - cmp = cmp == null ? NULL_MIN_COMPARATOR : cmp; + if (c instanceof RandomAccess) { + for (int i = c.size() - 1; i >= 0; i--) { + if (list.get(i) != null) { + return Optional.of(list.get(i)); + } + } + } else { + final ListIterator iter = list.listIterator(list.size()); + T pre = null; - int value = 0; + while (iter.hasPrevious()) { + if ((pre = iter.previous()) != null) { + return Optional.of(pre); + } + } + } + } else if (c instanceof Deque) { + final Iterator iter = ((Deque) c).descendingIterator(); + T next = null; - for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { - if ((value = cmp.compare(a[i], b[i])) != 0) { - return value; + while (iter.hasNext()) { + if ((next = iter.next()) != null) { + return Optional.of(next); + } } - } + } else { + // @SuppressWarnings("unchecked") + // final T[] a = (T[]) c.toArray(); + // return lastNonNull(a); - return a.length - b.length; - } + Iterators.lastNonNull(c.iterator()); + } - public static > int compare(final Collection a, final Collection b) { - final Comparator cmp = NULL_MIN_COMPARATOR; - return compare(a, b, cmp); + return Optional.empty(); } - public static int compare(final Collection a, final Collection b, Comparator cmp) { - if (N.isNullOrEmpty(a)) { - return N.isNullOrEmpty(b) ? 0 : -1; - } else if (N.isNullOrEmpty(b)) { - return 1; + public static Optional> firstEntry(final Map map) { + if (map == null || map.isEmpty()) { + return Optional.empty(); } - cmp = cmp == null ? NULL_MIN_COMPARATOR : cmp; - - final Iterator iterA = a.iterator(); - final Iterator iterB = b.iterator(); - int value = 0; + return Optional.of(map.entrySet().iterator().next()); + } - for (int i = 0, minLen = min(a.size(), b.size()); i < minLen; i++) { - if ((value = cmp.compare(iterA.next(), iterB.next())) != 0) { - return value; - } + public static Optional> lastEntry(final Map map) { + if (map == null || map.isEmpty()) { + return Optional.empty(); } - return a.size() - b.size(); + return Iterators.lastNonNull(map.entrySet().iterator()); } - public static int compareIgnoreCase(final String a, final String b) { - return a == null ? (b == null ? 0 : -1) : (b == null ? 1 : a.compareToIgnoreCase(b)); + /** + * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. + * + * @param s + * @return + */ + public static int len(final CharSequence s) { + return s == null ? 0 : s.length(); } /** - * Returns an empty {@code List} that is immutable. + * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. * + * @param a * @return - * @see Collections#emptyList() */ - public static List emptyList() { - return EMPTY_LIST; + public static int len(final boolean[] a) { + return a == null ? 0 : a.length; } /** - * Returns an empty {@code Set} that is immutable. + * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. * + * @param a * @return - * @see Collections#emptySet() */ - public static Set emptySet() { - return EMPTY_SET; + public static int len(final char[] a) { + return a == null ? 0 : a.length; } /** - * Returns an empty {@code SortedSet} that is immutable. + * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. * + * @param a * @return - * @see Collections#emptySortedSet() */ - public static SortedSet emptySortedSet() { - return EMPTY_SORTED_SET; + public static int len(final byte[] a) { + return a == null ? 0 : a.length; } /** - * Returns an empty {@code emptyNavigableSet} that is immutable. + * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. * + * @param a * @return - * @see Collections#emptyNavigableSet() */ - public static NavigableSet emptyNavigableSet() { - return EMPTY_NAVIGABLE_SET; + public static int len(final short[] a) { + return a == null ? 0 : a.length; } /** - * Returns an empty {@code Map} that is immutable. + * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. * + * @param a * @return - * @see Collections#emptyMap() */ - public static Map emptyMap() { - return EMPTY_MAP; + public static int len(final int[] a) { + return a == null ? 0 : a.length; } /** - * Returns an empty {@code SortedMap} that is immutable. + * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. * + * @param a * @return - * @see Collections#emptySortedMap() */ - public static SortedMap emptySortedMap() { - return EMPTY_SORTED_MAP; + public static int len(final long[] a) { + return a == null ? 0 : a.length; } /** - * Returns an empty {@code NavigableMap} that is immutable. + * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. * + * @param a * @return - * @see Collections#emptyNavigableMap() */ - public static NavigableMap emptyNavigableMap() { - return EMPTY_NAVIGABLE_MAP; + public static int len(final float[] a) { + return a == null ? 0 : a.length; } /** - * Returns an empty {@code Iterator} that is immutable. + * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. * + * @param a * @return - * @see Collections#emptyIterator() */ - public static Iterator emptyIterator() { - return EMPTY_ITERATOR; + public static int len(final double[] a) { + return a == null ? 0 : a.length; } /** - * Returns an empty {@code ListIterator} that is immutable. + * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. * + * @param a * @return - * @see Collections#emptyListIterator() */ - public static ListIterator emptyListIterator() { - return EMPTY_LIST_ITERATOR; + public static int len(final Object[] a) { + return a == null ? 0 : a.length; } - public static boolean anyNull(final T a, final T b) { - return a == null || b == null; + /** + * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. + * + * @param c + * @return + */ + public static int size(final Collection c) { + return c == null ? 0 : c.size(); } - public static boolean anyNull(final T a, final T b, final T c) { - return a == null || b == null || c == null; + /** + * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. + * + * @param m + * @return + */ + public static int size(final Map m) { + return m == null ? 0 : m.size(); } - @SafeVarargs - public static boolean anyNull(final T... a) { - if (N.isNullOrEmpty(a)) { - return false; - } - - for (T e : a) { - if (e == null) { - return true; - } - } - - return false; + /** + * Returns an immutable empty list if the specified List is null, otherwise itself is returned. + * + * @param list + * @return + */ + public static List nullToEmpty(final List list) { + return list == null ? N. emptyList() : list; } - public static boolean anyNull(final Collection c) { - if (N.isNullOrEmpty(c)) { - return false; - } - - for (T e : c) { - if (e == null) { - return true; - } - } - - return false; + /** + * Returns an immutable empty set if the specified Set is null, otherwise itself is returned. + * + * @param set + * @return + */ + public static Set nullToEmpty(final Set set) { + return set == null ? N. emptySet() : set; } - public static boolean allNull(final T a, final T b) { - return a == null && b == null; + /** + * Returns an immutable empty SortedSet if the specified SortedSet is null, otherwise itself is returned. + * + * @param set + * @return + */ + public static SortedSet nullToEmpty(final SortedSet set) { + return set == null ? N. emptySortedSet() : set; } - public static boolean allNull(final T a, final T b, final T c) { - return a == null && b == null && c == null; + /** + * Returns an immutable empty NavigableSet if the specified NavigableSet is null, otherwise itself is returned. + * + * @param set + * @return + */ + public static NavigableSet nullToEmpty(final NavigableSet set) { + return set == null ? N. emptyNavigableSet() : set; } - @SafeVarargs - public static boolean allNull(final T... a) { - if (N.isNullOrEmpty(a)) { - return true; - } - - for (T e : a) { - if (e != null) { - return false; - } - } + /** + * Returns an immutable empty map if the specified Map is null, otherwise itself is returned. + * + * @param map + * @return + */ + public static Map nullToEmpty(final Map map) { + return map == null ? N. emptyMap() : map; + } - return true; + /** + * Returns an immutable empty SortedMap if the specified SortedMap is null, otherwise itself is returned. + * + * @param map + * @return + */ + public static SortedMap nullToEmpty(final SortedMap map) { + return map == null ? N. emptySortedMap() : map; } - public static boolean allNull(final Collection c) { - if (N.isNullOrEmpty(c)) { - return true; - } + /** + * Returns an immutable empty NavigableMap if the specified NavigableMap is null, otherwise itself is returned. + * + * @param map + * @return + */ + public static NavigableMap nullToEmpty(final NavigableMap map) { + return map == null ? N. emptyNavigableMap() : map; + } - for (T e : c) { - if (e != null) { - return false; - } - } + /** + * Returns an immutable empty Iterator if the specified Iterator is null, otherwise itself is returned. + * + * @param iter + * @return + */ + public static Iterator nullToEmpty(final Iterator iter) { + return iter == null ? N. emptyIterator() : iter; + } - return true; + /** + * Returns an immutable empty ListIterator if the specified ListIterator is null, otherwise itself is returned. + * + * @param iter + * @return + */ + public static ListIterator nullToEmpty(final ListIterator iter) { + return iter == null ? N. emptyListIterator() : iter; } - public static Nullable first(final Collection c) { - if (N.isNullOrEmpty(c)) { - return Nullable.empty(); - } + public static String nullToEmpty(final String str) { + return str == null ? EMPTY_STRING : str; + } - if (c instanceof List && c instanceof RandomAccess) { - return Nullable.of(((List) c).get(0)); - } else { - return Nullable.of(c.iterator().next()); - } + public static boolean[] nullToEmpty(final boolean[] a) { + return a == null ? EMPTY_BOOLEAN_ARRAY : a; } - public static Nullable last(final Collection c) { - if (N.isNullOrEmpty(c)) { - return Nullable.empty(); - } + public static char[] nullToEmpty(final char[] a) { + return a == null ? EMPTY_CHAR_ARRAY : a; + } - if (c instanceof List) { - final List list = (List) c; + public static byte[] nullToEmpty(final byte[] a) { + return a == null ? EMPTY_BYTE_ARRAY : a; + } - if (c instanceof RandomAccess) { - return Nullable.of(list.get(c.size() - 1)); - } else { - return Nullable.of(list.listIterator(list.size()).previous()); - } - } else if (c instanceof Deque) { - return Nullable.of(((Deque) c).descendingIterator().next()); - } else { - return Iterators.last(c.iterator()); - } + public static short[] nullToEmpty(final short[] a) { + return a == null ? EMPTY_SHORT_ARRAY : a; } - public static Optional firstNonNull(final T a, final T b) { - return a != null ? Optional.of(a) : (b != null ? Optional.of(b) : Optional. empty()); + public static int[] nullToEmpty(final int[] a) { + return a == null ? EMPTY_INT_ARRAY : a; } - public static Optional firstNonNull(final T a, final T b, final T c) { - return a != null ? Optional.of(a) : (b != null ? Optional.of(b) : (c != null ? Optional.of(c) : Optional. empty())); + public static long[] nullToEmpty(final long[] a) { + return a == null ? EMPTY_LONG_ARRAY : a; } - @SafeVarargs - public static Optional firstNonNull(final T... a) { - if (N.isNullOrEmpty(a)) { - return Optional.empty(); - } + public static float[] nullToEmpty(final float[] a) { + return a == null ? EMPTY_FLOAT_ARRAY : a; + } - for (T e : a) { - if (e != null) { - return Optional.of(e); - } - } + public static double[] nullToEmpty(final double[] a) { + return a == null ? EMPTY_DOUBLE_ARRAY : a; + } - return Optional.empty(); + public static String[] nullToEmpty(final String[] a) { + return a == null ? EMPTY_STRING_ARRAY : a; } - public static Optional firstNonNull(final Collection c) { - if (N.isNullOrEmpty(c)) { - return Optional.empty(); - } + /** + * + * @param a + * @return + * @deprecated replaced by {@link N#nullToEmpty(Class, Object[])} + */ + @Deprecated + public static Object[] nullToEmpty(final Object[] a) { + return a == null ? EMPTY_OBJECT_ARRAY : a; + } - for (T e : c) { - if (e != null) { - return Optional.of(e); - } - } + public static T[] nullToEmpty(final Class arrayType, final T[] a) { + return a == null ? (T[]) N.newArray(arrayType.getComponentType(), 0) : a; + } - return Optional.empty(); + public static boolean isNullOrEmpty(final CharSequence s) { + return (s == null) || (s.length() == 0); } - public static Optional lastNonNull(final T a, final T b) { - return b != null ? Optional.of(b) : (a != null ? Optional.of(a) : Optional. empty()); + public static boolean isNullOrEmpty(final boolean[] a) { + return (a == null) || (a.length == 0); } - public static Optional lastNonNull(final T a, final T b, final T c) { - return c != null ? Optional.of(c) : (b != null ? Optional.of(b) : (a != null ? Optional.of(a) : Optional. empty())); + public static boolean isNullOrEmpty(final char[] a) { + return (a == null) || (a.length == 0); } - @SafeVarargs - public static Optional lastNonNull(final T... a) { - if (N.isNullOrEmpty(a)) { - return Optional.empty(); - } + public static boolean isNullOrEmpty(final byte[] a) { + return (a == null) || (a.length == 0); + } - for (int i = a.length - 1; i >= 0; i--) { - if (a[i] != null) { - return Optional.of(a[i]); - } - } + public static boolean isNullOrEmpty(final short[] a) { + return (a == null) || (a.length == 0); + } - return Optional.empty(); + public static boolean isNullOrEmpty(final int[] a) { + return (a == null) || (a.length == 0); } - public static Optional lastNonNull(final Collection c) { - if (N.isNullOrEmpty(c)) { - return Optional.empty(); - } + public static boolean isNullOrEmpty(final long[] a) { + return (a == null) || (a.length == 0); + } - if (c instanceof List) { - final List list = (List) c; + public static boolean isNullOrEmpty(final float[] a) { + return (a == null) || (a.length == 0); + } - if (c instanceof RandomAccess) { - for (int i = c.size() - 1; i >= 0; i--) { - if (list.get(i) != null) { - return Optional.of(list.get(i)); - } - } - } else { - final ListIterator iter = list.listIterator(list.size()); - T pre = null; + public static boolean isNullOrEmpty(final double[] a) { + return (a == null) || (a.length == 0); + } - while (iter.hasPrevious()) { - if ((pre = iter.previous()) != null) { - return Optional.of(pre); - } - } - } - } else if (c instanceof Deque) { - final Iterator iter = ((Deque) c).descendingIterator(); - T next = null; + public static boolean isNullOrEmpty(final Object[] a) { + return (a == null) || (a.length == 0); + } - while (iter.hasNext()) { - if ((next = iter.next()) != null) { - return Optional.of(next); - } - } - } else { - // @SuppressWarnings("unchecked") - // final T[] a = (T[]) c.toArray(); - // return lastNonNull(a); + public static boolean isNullOrEmpty(final Collection c) { + return (c == null) || (c.isEmpty()); + } - Iterators.lastNonNull(c.iterator()); - } + public static boolean isNullOrEmpty(final Map m) { + return (m == null) || (m.isEmpty()); + } - return Optional.empty(); + @SuppressWarnings("rawtypes") + public static boolean isNullOrEmpty(final PrimitiveList list) { + return (list == null) || (list.isEmpty()); } - public static Optional> firstEntry(final Map map) { - if (map == null || map.isEmpty()) { - return Optional.empty(); - } + public static boolean isNullOrEmpty(final Multiset s) { + return (s == null) || (s.isEmpty()); + } - return Optional.of(map.entrySet().iterator().next()); + public static boolean isNullOrEmpty(final LongMultiset s) { + return (s == null) || (s.isEmpty()); } - public static Optional> lastEntry(final Map map) { - if (map == null || map.isEmpty()) { - return Optional.empty(); - } + public static boolean isNullOrEmpty(final Multimap m) { + return (m == null) || (m.isEmpty()); + } - return Iterators.lastNonNull(map.entrySet().iterator()); + public static boolean isNullOrEmpty(final DataSet rs) { + return (rs == null) || (rs.isEmpty()); } - /** - * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. - * - * @param s - * @return - */ - public static int len(final CharSequence s) { - return s == null ? 0 : s.length(); + public static boolean isNullOrEmpty(final EntityId entityId) { + return (entityId == null) || (entityId.isEmpty()); } - /** - * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. - * - * @param a - * @return - */ - public static int len(final boolean[] a) { - return a == null ? 0 : a.length; - } + // DON'T change 'OrEmptyOrBlank' to 'OrBlank' because of the occurring order in the auto-completed context menu. + public static boolean isNullOrEmptyOrBlank(final CharSequence s) { + if (N.isNullOrEmpty(s)) { + return true; + } - /** - * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. - * - * @param a - * @return - */ - public static int len(final char[] a) { - return a == null ? 0 : a.length; + for (int i = 0, len = s.length(); i < len; i++) { + if (Character.isWhitespace(s.charAt(i)) == false) { + return false; + } + } + + return true; } - /** - * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. - * - * @param a - * @return - */ - public static int len(final byte[] a) { - return a == null ? 0 : a.length; + public static boolean notNullOrEmpty(final CharSequence s) { + return (s != null) && (s.length() > 0); } - /** - * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. - * - * @param a - * @return - */ - public static int len(final short[] a) { - return a == null ? 0 : a.length; + public static boolean notNullOrEmpty(final boolean[] a) { + return (a != null) && (a.length > 0); } - /** - * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. - * - * @param a - * @return - */ - public static int len(final int[] a) { - return a == null ? 0 : a.length; + public static boolean notNullOrEmpty(final char[] a) { + return (a != null) && (a.length > 0); } - /** - * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. - * - * @param a - * @return - */ - public static int len(final long[] a) { - return a == null ? 0 : a.length; + public static boolean notNullOrEmpty(final byte[] a) { + return (a != null) && (a.length > 0); } - /** - * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. - * - * @param a - * @return - */ - public static int len(final float[] a) { - return a == null ? 0 : a.length; + public static boolean notNullOrEmpty(final short[] a) { + return (a != null) && (a.length > 0); } - /** - * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. - * - * @param a - * @return - */ - public static int len(final double[] a) { - return a == null ? 0 : a.length; + public static boolean notNullOrEmpty(final int[] a) { + return (a != null) && (a.length > 0); } - /** - * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. - * - * @param a - * @return - */ - public static int len(final Object[] a) { - return a == null ? 0 : a.length; + public static boolean notNullOrEmpty(final long[] a) { + return (a != null) && (a.length > 0); } - /** - * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. - * - * @param c - * @return - */ - public static int size(final Collection c) { - return c == null ? 0 : c.size(); + public static boolean notNullOrEmpty(final float[] a) { + return (a != null) && (a.length > 0); } - /** - * Returns the length/size of the specified {@code Array/Collection/Map/CharSequence}, or {@code 0} if it's empty or {@code null}. - * - * @param m - * @return - */ - public static int size(final Map m) { - return m == null ? 0 : m.size(); + public static boolean notNullOrEmpty(final double[] a) { + return (a != null) && (a.length > 0); } - /** - * Returns an immutable empty list if the specified List is null, otherwise itself is returned. - * - * @param list - * @return - */ - public static List nullToEmpty(final List list) { - return list == null ? N. emptyList() : list; + public static boolean notNullOrEmpty(final Object[] a) { + return (a != null) && (a.length > 0); } - /** - * Returns an immutable empty set if the specified Set is null, otherwise itself is returned. - * - * @param set - * @return - */ - public static Set nullToEmpty(final Set set) { - return set == null ? N. emptySet() : set; + public static boolean notNullOrEmpty(final Collection c) { + return (c != null) && (c.size() > 0); } - /** - * Returns an immutable empty SortedSet if the specified SortedSet is null, otherwise itself is returned. - * - * @param set - * @return - */ - public static SortedSet nullToEmpty(final SortedSet set) { - return set == null ? N. emptySortedSet() : set; + public static boolean notNullOrEmpty(final Map m) { + return (m != null) && (m.size() > 0); } - /** - * Returns an immutable empty NavigableSet if the specified NavigableSet is null, otherwise itself is returned. - * - * @param set - * @return - */ - public static NavigableSet nullToEmpty(final NavigableSet set) { - return set == null ? N. emptyNavigableSet() : set; + @SuppressWarnings("rawtypes") + public static boolean notNullOrEmpty(final PrimitiveList list) { + return (list != null) && (list.size() > 0); } - /** - * Returns an immutable empty map if the specified Map is null, otherwise itself is returned. - * - * @param map - * @return - */ - public static Map nullToEmpty(final Map map) { - return map == null ? N. emptyMap() : map; + public static boolean notNullOrEmpty(final Multiset s) { + return (s != null) && (s.size() > 0); } - /** - * Returns an immutable empty SortedMap if the specified SortedMap is null, otherwise itself is returned. - * - * @param map - * @return - */ - public static SortedMap nullToEmpty(final SortedMap map) { - return map == null ? N. emptySortedMap() : map; + public static boolean notNullOrEmpty(final LongMultiset s) { + return (s != null) && (s.size() > 0); } - /** - * Returns an immutable empty NavigableMap if the specified NavigableMap is null, otherwise itself is returned. - * - * @param map - * @return - */ - public static NavigableMap nullToEmpty(final NavigableMap map) { - return map == null ? N. emptyNavigableMap() : map; + public static boolean notNullOrEmpty(final Multimap m) { + return (m != null) && (m.size() > 0); } - /** - * Returns an immutable empty Iterator if the specified Iterator is null, otherwise itself is returned. - * - * @param iter - * @return - */ - public static Iterator nullToEmpty(final Iterator iter) { - return iter == null ? N. emptyIterator() : iter; + public static boolean notNullOrEmpty(final DataSet rs) { + return (rs != null) && (rs.size() > 0); } - /** - * Returns an immutable empty ListIterator if the specified ListIterator is null, otherwise itself is returned. - * - * @param iter - * @return - */ - public static ListIterator nullToEmpty(final ListIterator iter) { - return iter == null ? N. emptyListIterator() : iter; + public static boolean notNullOrEmpty(final EntityId entityId) { + return (entityId != null) && (!entityId.isEmpty()); } - public static String nullToEmpty(final String str) { - return str == null ? EMPTY_STRING : str; + // DON'T change 'OrEmptyOrBlank' to 'OrBlank' because of the occurring order in the auto-completed context menu. + public static boolean notNullOrEmptyOrBlank(final CharSequence s) { + return !N.isNullOrEmptyOrBlank(s); } - public static boolean[] nullToEmpty(final boolean[] a) { - return a == null ? EMPTY_BOOLEAN_ARRAY : a; + private static boolean isNullErrorMsg(final String msg) { + // shortest message: "it is null" + return msg.length() > 9 && msg.indexOf(WD._SPACE) > 0; } - public static char[] nullToEmpty(final char[] a) { - return a == null ? EMPTY_CHAR_ARRAY : a; - } - - public static byte[] nullToEmpty(final byte[] a) { - return a == null ? EMPTY_BYTE_ARRAY : a; - } - - public static short[] nullToEmpty(final short[] a) { - return a == null ? EMPTY_SHORT_ARRAY : a; - } - - public static int[] nullToEmpty(final int[] a) { - return a == null ? EMPTY_INT_ARRAY : a; - } - - public static long[] nullToEmpty(final long[] a) { - return a == null ? EMPTY_LONG_ARRAY : a; - } - - public static float[] nullToEmpty(final float[] a) { - return a == null ? EMPTY_FLOAT_ARRAY : a; - } - - public static double[] nullToEmpty(final double[] a) { - return a == null ? EMPTY_DOUBLE_ARRAY : a; - } - - public static String[] nullToEmpty(final String[] a) { - return a == null ? EMPTY_STRING_ARRAY : a; - } - - /** - * - * @param a - * @return - * @deprecated replaced by {@link N#nullToEmpty(Class, Object[])} - */ - @Deprecated - public static Object[] nullToEmpty(final Object[] a) { - return a == null ? EMPTY_OBJECT_ARRAY : a; - } - - public static T[] nullToEmpty(final Class arrayType, final T[] a) { - return a == null ? (T[]) N.newArray(arrayType.getComponentType(), 0) : a; - } - - public static boolean isNullOrEmpty(final CharSequence s) { - return (s == null) || (s.length() == 0); - } - - public static boolean isNullOrEmpty(final boolean[] a) { - return (a == null) || (a.length == 0); - } - - public static boolean isNullOrEmpty(final char[] a) { - return (a == null) || (a.length == 0); - } - - public static boolean isNullOrEmpty(final byte[] a) { - return (a == null) || (a.length == 0); - } - - public static boolean isNullOrEmpty(final short[] a) { - return (a == null) || (a.length == 0); - } - - public static boolean isNullOrEmpty(final int[] a) { - return (a == null) || (a.length == 0); - } - - public static boolean isNullOrEmpty(final long[] a) { - return (a == null) || (a.length == 0); - } - - public static boolean isNullOrEmpty(final float[] a) { - return (a == null) || (a.length == 0); - } - - public static boolean isNullOrEmpty(final double[] a) { - return (a == null) || (a.length == 0); - } - - public static boolean isNullOrEmpty(final Object[] a) { - return (a == null) || (a.length == 0); - } - - public static boolean isNullOrEmpty(final Collection c) { - return (c == null) || (c.isEmpty()); - } - - public static boolean isNullOrEmpty(final Map m) { - return (m == null) || (m.isEmpty()); - } - - @SuppressWarnings("rawtypes") - public static boolean isNullOrEmpty(final PrimitiveList list) { - return (list == null) || (list.isEmpty()); - } - - public static boolean isNullOrEmpty(final Multiset s) { - return (s == null) || (s.isEmpty()); - } - - public static boolean isNullOrEmpty(final LongMultiset s) { - return (s == null) || (s.isEmpty()); - } - - public static boolean isNullOrEmpty(final Multimap m) { - return (m == null) || (m.isEmpty()); - } - - public static boolean isNullOrEmpty(final DataSet rs) { - return (rs == null) || (rs.isEmpty()); - } - - public static boolean isNullOrEmpty(final EntityId entityId) { - return (entityId == null) || (entityId.isEmpty()); - } - - // DON'T change 'OrEmptyOrBlank' to 'OrBlank' because of the occurring order in the auto-completed context menu. - public static boolean isNullOrEmptyOrBlank(final CharSequence s) { - if (N.isNullOrEmpty(s)) { - return true; - } - - for (int i = 0, len = s.length(); i < len; i++) { - if (Character.isWhitespace(s.charAt(i)) == false) { - return false; - } - } - - return true; - } - - public static boolean notNullOrEmpty(final CharSequence s) { - return (s != null) && (s.length() > 0); - } - - public static boolean notNullOrEmpty(final boolean[] a) { - return (a != null) && (a.length > 0); - } - - public static boolean notNullOrEmpty(final char[] a) { - return (a != null) && (a.length > 0); - } - - public static boolean notNullOrEmpty(final byte[] a) { - return (a != null) && (a.length > 0); - } - - public static boolean notNullOrEmpty(final short[] a) { - return (a != null) && (a.length > 0); - } - - public static boolean notNullOrEmpty(final int[] a) { - return (a != null) && (a.length > 0); - } - - public static boolean notNullOrEmpty(final long[] a) { - return (a != null) && (a.length > 0); - } - - public static boolean notNullOrEmpty(final float[] a) { - return (a != null) && (a.length > 0); - } - - public static boolean notNullOrEmpty(final double[] a) { - return (a != null) && (a.length > 0); - } - - public static boolean notNullOrEmpty(final Object[] a) { - return (a != null) && (a.length > 0); - } - - public static boolean notNullOrEmpty(final Collection c) { - return (c != null) && (c.size() > 0); - } - - public static boolean notNullOrEmpty(final Map m) { - return (m != null) && (m.size() > 0); - } - - @SuppressWarnings("rawtypes") - public static boolean notNullOrEmpty(final PrimitiveList list) { - return (list != null) && (list.size() > 0); - } - - public static boolean notNullOrEmpty(final Multiset s) { - return (s != null) && (s.size() > 0); - } - - public static boolean notNullOrEmpty(final LongMultiset s) { - return (s != null) && (s.size() > 0); - } - - public static boolean notNullOrEmpty(final Multimap m) { - return (m != null) && (m.size() > 0); - } - - public static boolean notNullOrEmpty(final DataSet rs) { - return (rs != null) && (rs.size() > 0); - } - - public static boolean notNullOrEmpty(final EntityId entityId) { - return (entityId != null) && (!entityId.isEmpty()); - } - - // DON'T change 'OrEmptyOrBlank' to 'OrBlank' because of the occurring order in the auto-completed context menu. - public static boolean notNullOrEmptyOrBlank(final CharSequence s) { - return !N.isNullOrEmptyOrBlank(s); - } - - private static boolean isNullErrorMsg(final String msg) { - // shortest message: "it is null" - return msg.length() > 9 && msg.indexOf(WD._SPACE) > 0; - } - - /** - *
- * Copied from JDK 9 through: StreamSupport at: https://github.com/streamsupport/streamsupport. - *
- * - * Checks if the {@code index} is within the bounds of the range from - * {@code 0} (inclusive) to {@code length} (exclusive). - * - *

The {@code index} is defined to be out-of-bounds if any of the - * following inequalities is true: - *

    - *
  • {@code index < 0}
  • - *
  • {@code index >= length}
  • - *
  • {@code length < 0}, which is implied from the former inequalities
  • - *
- * - * @param index the index - * @param length the upper-bound (exclusive) of the range - * @return {@code index} if it is within bounds of the range - * @throws IndexOutOfBoundsException if the {@code index} is out-of-bounds - * @since 9 - */ - public static void checkIndex(final int index, final int length) { - if (index < 0 || index >= length) { - throw new IndexOutOfBoundsException(String.format("Index %d out-of-bounds for length %d", index, length)); - } + /** + *
+ * Copied from JDK 9 through: StreamSupport at: https://github.com/streamsupport/streamsupport. + *
+ * + * Checks if the {@code index} is within the bounds of the range from + * {@code 0} (inclusive) to {@code length} (exclusive). + * + *

The {@code index} is defined to be out-of-bounds if any of the + * following inequalities is true: + *

    + *
  • {@code index < 0}
  • + *
  • {@code index >= length}
  • + *
  • {@code length < 0}, which is implied from the former inequalities
  • + *
+ * + * @param index the index + * @param length the upper-bound (exclusive) of the range + * @return {@code index} if it is within bounds of the range + * @throws IndexOutOfBoundsException if the {@code index} is out-of-bounds + * @since 9 + */ + public static void checkIndex(final int index, final int length) { + if (index < 0 || index >= length) { + throw new IndexOutOfBoundsException(String.format("Index %d out-of-bounds for length %d", index, length)); + } } /** @@ -6157,552 +5774,1190 @@ public static double[] checkArgNotNullOrEmpty(final double[] arg, final String a } } - return arg; + return arg; + } + + /** + * Checks if the specified {@code arg} is {@code null} or empty, and throws {@code IllegalArgumentException} if it is. + * + * @param arg + * @param argNameOrErrorMsg + * @throws IllegalArgumentException if the specified {@code arg} is {@code null} or empty. + */ + public static T[] checkArgNotNullOrEmpty(final T[] arg, final String argNameOrErrorMsg) { + if (N.isNullOrEmpty(arg)) { + if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { + throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be null or empty"); + } else { + throw new IllegalArgumentException(argNameOrErrorMsg); + } + } + + return arg; + } + + /** + * Checks if the specified {@code arg} is {@code null} or empty, and throws {@code IllegalArgumentException} if it is. + * + * @param arg + * @param argNameOrErrorMsg + * @throws IllegalArgumentException if the specified {@code arg} is {@code null} or empty. + */ + public static > T checkArgNotNullOrEmpty(final T arg, final String argNameOrErrorMsg) { + if (N.isNullOrEmpty(arg)) { + if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { + throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be null or empty"); + } else { + throw new IllegalArgumentException(argNameOrErrorMsg); + } + } + + return arg; + } + + /** + * Checks if the specified {@code arg} is {@code null} or empty, and throws {@code IllegalArgumentException} if it is. + * + * @param arg + * @param argNameOrErrorMsg + * @throws IllegalArgumentException if the specified {@code arg} is {@code null} or empty. + */ + public static > T checkArgNotNullOrEmpty(final T arg, final String argNameOrErrorMsg) { + if (N.isNullOrEmpty(arg)) { + if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { + throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be null or empty"); + } else { + throw new IllegalArgumentException(argNameOrErrorMsg); + } + } + + return arg; + } + + /** + * Check if the specified parameter is null or empty or blank + * + * @param arg + * @param msg name of parameter or error message + * @return the input parameter + * @throws IllegalArgumentException if the specified parameter is null or empty. + */ + // DON'T change 'OrEmptyOrBlank' to 'OrBlank' because of the occurring order in the auto-completed context menu. + public static T checkArgNotNullOrEmptyOrBlank(final T arg, final String msg) { + if (N.isNullOrEmptyOrBlank(arg)) { + if (isNullErrorMsg(msg)) { + throw new IllegalArgumentException(msg); + } else { + throw new IllegalArgumentException("'" + msg + "' can not be null or empty or blank"); + } + } + + return arg; + } + + /** + * Checks if the specified {@code arg} is not negative, and throws {@code IllegalArgumentException} if it is. + * + * @param arg + * @param argNameOrErrorMsg + * @throws IllegalArgumentException if the specified {@code arg} is negative. + */ + public static int checkArgNotNegative(final int arg, final String argNameOrErrorMsg) { + if (arg < 0) { + if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { + throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be negative: " + arg); + } else { + throw new IllegalArgumentException(argNameOrErrorMsg); + } + } + + return arg; + } + + /** + * Checks if the specified {@code arg} is not negative, and throws {@code IllegalArgumentException} if it is. + * + * @param arg + * @param argNameOrErrorMsg + * @throws IllegalArgumentException if the specified {@code arg} is negative. + */ + public static long checkArgNotNegative(final long arg, final String argNameOrErrorMsg) { + if (arg < 0) { + if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { + throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be negative: " + arg); + } else { + throw new IllegalArgumentException(argNameOrErrorMsg); + } + } + + return arg; + } + + /** + * Checks if the specified {@code arg} is not negative, and throws {@code IllegalArgumentException} if it is. + * + * @param arg + * @param argNameOrErrorMsg + * @throws IllegalArgumentException if the specified {@code arg} is negative. + */ + public static double checkArgNotNegative(final double arg, final String argNameOrErrorMsg) { + if (arg < 0) { + if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { + throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be negative: " + arg); + } else { + throw new IllegalArgumentException(argNameOrErrorMsg); + } + } + + return arg; + } + + /** + * Checks if the specified {@code arg} is positive, and throws {@code IllegalArgumentException} if it is not. + * + * @param arg + * @param argNameOrErrorMsg + * @throws IllegalArgumentException if the specified {@code arg} is negative. + */ + public static int checkArgPositive(final int arg, final String argNameOrErrorMsg) { + if (arg <= 0) { + if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { + throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be zero or negative: " + arg); + } else { + throw new IllegalArgumentException(argNameOrErrorMsg); + } + } + + return arg; + } + + /** + * Checks if the specified {@code arg} is positive, and throws {@code IllegalArgumentException} if it is not. + * + * @param arg + * @param argNameOrErrorMsg + * @throws IllegalArgumentException if the specified {@code arg} is negative. + */ + public static long checkArgPositive(final long arg, final String argNameOrErrorMsg) { + if (arg <= 0) { + if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { + throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be zero or negative: " + arg); + } else { + throw new IllegalArgumentException(argNameOrErrorMsg); + } + } + + return arg; + } + + /** + * Checks if the specified {@code arg} is positive, and throws {@code IllegalArgumentException} if it is not. + * + * @param arg + * @param argNameOrErrorMsg + * @throws IllegalArgumentException if the specified {@code arg} is negative. + */ + public static double checkArgPositive(final double arg, final String argNameOrErrorMsg) { + if (arg <= 0) { + if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { + throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be zero or negative: " + arg); + } else { + throw new IllegalArgumentException(argNameOrErrorMsg); + } + } + + return arg; + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + * @param expression a boolean expression + * @throws IllegalStateException if {@code expression} is false + */ + public static void checkState(boolean expression) { + if (!expression) { + throw new IllegalStateException(); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + * @param expression a boolean expression + * @param errorMessage the exception message to use if the check fails; will be converted to a + * string using {@link String#valueOf(Object)} + * @throws IllegalStateException if {@code expression} is false + */ + public static void checkState(boolean expression, String errorMessage) { + if (!expression) { + throw new IllegalStateException(errorMessage); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + *

See {@link #checkState(boolean, String, Object...)} for details. + */ + public static void checkState(boolean b, String errorMessageTemplate, int p) { + if (!b) { + throw new IllegalStateException(format(errorMessageTemplate, p)); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + *

See {@link #checkState(boolean, String, Object...)} for details. + */ + public static void checkState(boolean b, String errorMessageTemplate, int p1, int p2) { + if (!b) { + throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + *

See {@link #checkState(boolean, String, Object...)} for details. + */ + public static void checkState(boolean b, String errorMessageTemplate, int p1, int p2, int p3) { + if (!b) { + throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3)); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + *

See {@link #checkState(boolean, String, Object...)} for details. + */ + public static void checkState(boolean b, String errorMessageTemplate, double p) { + if (!b) { + throw new IllegalStateException(format(errorMessageTemplate, p)); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + *

See {@link #checkState(boolean, String, Object...)} for details. + */ + public static void checkState(boolean b, String errorMessageTemplate, double p1, double p2) { + if (!b) { + throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + *

See {@link #checkState(boolean, String, Object...)} for details. + */ + public static void checkState(boolean b, String errorMessageTemplate, double p1, double p2, double p3) { + if (!b) { + throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3)); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + *

See {@link #checkState(boolean, String, Object...)} for details. + */ + public static void checkState(boolean b, String errorMessageTemplate, Object p) { + if (!b) { + throw new IllegalStateException(format(errorMessageTemplate, p)); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + *

See {@link #checkState(boolean, String, Object...)} for details. + */ + public static void checkState(boolean b, String errorMessageTemplate, Object p1, Object p2) { + if (!b) { + throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + *

See {@link #checkState(boolean, String, Object...)} for details. + */ + public static void checkState(boolean b, String errorMessageTemplate, Object p1, Object p2, Object p3) { + if (!b) { + throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3)); + } + } + + public static void checkState(boolean b, Try.Supplier errorMessageSupplier) throws E { + if (!b) { + throw new IllegalStateException(errorMessageSupplier.get()); + } + } + + static String format(String template, Object arg) { + template = String.valueOf(template); // null -> "null" + + // start substituting the arguments into the '%s' placeholders + final StringBuilder sb = Objectory.createStringBuilder(template.length() + 16); + + String placeholder = "{}"; + int placeholderStart = template.indexOf(placeholder); + + if (placeholderStart < 0) { + placeholder = "%s"; + placeholderStart = template.indexOf(placeholder); + } + + if (placeholderStart >= 0) { + sb.append(template, 0, placeholderStart); + sb.append(N.toString(arg)); + sb.append(template, placeholderStart + 2, template.length()); + } else { + sb.append(" ["); + sb.append(N.toString(arg)); + sb.append(']'); + } + + final String result = sb.toString(); + + Objectory.recycle(sb); + + return result; + } + + static String format(String template, Object arg1, Object arg2) { + template = String.valueOf(template); // null -> "null" + + // start substituting the arguments into the '%s' placeholders + final StringBuilder sb = Objectory.createStringBuilder(template.length() + 32); + + String placeholder = "{}"; + int placeholderStart = template.indexOf(placeholder); + + if (placeholderStart < 0) { + placeholder = "%s"; + placeholderStart = template.indexOf(placeholder); + } + + int templateStart = 0; + int cnt = 0; + + if (placeholderStart >= 0) { + cnt++; + sb.append(template, templateStart, placeholderStart); + sb.append(N.toString(arg1)); + templateStart = placeholderStart + 2; + placeholderStart = template.indexOf(placeholder, templateStart); + + if (placeholderStart >= 0) { + cnt++; + sb.append(template, templateStart, placeholderStart); + sb.append(N.toString(arg2)); + templateStart = placeholderStart + 2; + } + + sb.append(template, templateStart, template.length()); + } + + if (cnt == 0) { + sb.append(" ["); + sb.append(N.toString(arg1)); + sb.append(", "); + sb.append(N.toString(arg2)); + sb.append(']'); + } else if (cnt == 1) { + sb.append(" ["); + sb.append(N.toString(arg2)); + sb.append(']'); + } + + final String result = sb.toString(); + + Objectory.recycle(sb); + + return result; + } + + static String format(String template, Object arg1, Object arg2, Object arg3) { + template = String.valueOf(template); // null -> "null" + + // start substituting the arguments into the '%s' placeholders + final StringBuilder sb = Objectory.createStringBuilder(template.length() + 48); + + String placeholder = "{}"; + int placeholderStart = template.indexOf(placeholder); + + if (placeholderStart < 0) { + placeholder = "%s"; + placeholderStart = template.indexOf(placeholder); + } + + int templateStart = 0; + int cnt = 0; + + if (placeholderStart >= 0) { + cnt++; + sb.append(template, templateStart, placeholderStart); + sb.append(N.toString(arg1)); + templateStart = placeholderStart + 2; + placeholderStart = template.indexOf(placeholder, templateStart); + + if (placeholderStart >= 0) { + cnt++; + sb.append(template, templateStart, placeholderStart); + sb.append(N.toString(arg2)); + templateStart = placeholderStart + 2; + placeholderStart = template.indexOf(placeholder, templateStart); + + if (placeholderStart >= 0) { + cnt++; + sb.append(template, templateStart, placeholderStart); + sb.append(N.toString(arg3)); + templateStart = placeholderStart + 2; + } + } + + sb.append(template, templateStart, template.length()); + } + + if (cnt == 0) { + sb.append(" ["); + sb.append(N.toString(arg1)); + sb.append(", "); + sb.append(N.toString(arg2)); + sb.append(", "); + sb.append(N.toString(arg3)); + sb.append(']'); + } else if (cnt == 1) { + sb.append(" ["); + sb.append(N.toString(arg2)); + sb.append(", "); + sb.append(N.toString(arg3)); + sb.append(']'); + } else if (cnt == 2) { + sb.append(" ["); + sb.append(N.toString(arg3)); + sb.append(']'); + } + + final String result = sb.toString(); + + Objectory.recycle(sb); + + return result; + } + + /** + * Substitutes each {@code %s} in {@code template} with an argument. These are matched by + * position: the first {@code %s} gets {@code args[0]}, etc. If there are more arguments than + * placeholders, the unmatched arguments will be appended to the end of the formatted message in + * square braces. + * + * @param template a non-null string containing 0 or more {@code %s} placeholders. + * @param args the arguments to be substituted into the message template. Arguments are converted + * to strings using {@link String#valueOf(Object)}. Arguments can be null. + */ + // Note that this is somewhat-improperly used from Verify.java as well. + static String format(String template, Object... args) { + template = String.valueOf(template); // null -> "null" + + if (N.isNullOrEmpty(args)) { + return template; + } + + // start substituting the arguments into the '%s' placeholders + final StringBuilder sb = Objectory.createStringBuilder(template.length() + 16 * args.length); + int templateStart = 0; + int i = 0; + + String placeholder = "{}"; + int placeholderStart = template.indexOf(placeholder); + + if (placeholderStart < 0) { + placeholder = "%s"; + placeholderStart = template.indexOf(placeholder); + } + + while (placeholderStart >= 0 && i < args.length) { + sb.append(template, templateStart, placeholderStart); + sb.append(N.toString(args[i++])); + templateStart = placeholderStart + 2; + placeholderStart = template.indexOf(placeholder, templateStart); + } + + sb.append(template, templateStart, template.length()); + + // if we run out of placeholders, append the extra args in square braces + if (i < args.length) { + sb.append(" ["); + sb.append(N.toString(args[i++])); + while (i < args.length) { + sb.append(", "); + sb.append(N.toString(args[i++])); + } + sb.append(']'); + } + + final String result = sb.toString(); + + Objectory.recycle(sb); + + return result; + } + + public static int compare(final boolean a, final boolean b) { + return (a == b) ? 0 : (a ? 1 : -1); + } + + public static int compare(final byte a, final byte b) { + return (a < b) ? -1 : ((a == b) ? 0 : 1); + } + + public static int compare(final short a, final short b) { + return (a < b) ? -1 : ((a == b) ? 0 : 1); + } + + public static int compare(final int a, final int b) { + return (a < b) ? -1 : ((a == b) ? 0 : 1); + } + + public static int compare(final long a, final long b) { + return (a < b) ? -1 : ((a == b) ? 0 : 1); + } + + public static int compare(final float a, final float b) { + return Float.compare(a, b); + } + + public static int compare(final double a, final double b) { + return Double.compare(a, b); + } + + public static > int compare(final T a, final T b) { + return a == null ? (b == null ? 0 : -1) : (b == null ? 1 : a.compareTo(b)); + } + + /** + * Returns 0 if the arguments are identical and {@code c.compare(a, b)} + * otherwise. Consequently, if both arguments are {@code null} 0 is + * returned. + * + *

+ * Note that if one of the arguments is {@code null}, a + * {@code NullPointerException} may or may not be thrown depending on what + * ordering policy, if any, the {@link Comparator Comparator} chooses to + * have for {@code null} values. + * + * @param + * the type of the objects being compared + * @param a + * an object + * @param b + * an object to be compared with {@code a} + * @param cmp + * the {@code Comparator} to compare the first two arguments + * @return 0 if the arguments are identical and {@code c.compare(a, b)} + * otherwise. + * @see Comparable + * @see Comparator + */ + public static int compare(final T a, final T b, final Comparator cmp) { + return a == null ? (b == null ? 0 : -1) : (b == null ? 1 : (cmp == null ? NATURAL_ORDER : cmp).compare(a, b)); + } + + /** + * Continue to compare the pairs of values (a1, b1), (a2, b2) until they're not equal. + * 0 is returned if all of the pairs of values are equal. + * + * @param a1 + * @param b1 + * @param a2 + * @param b2 + * @return + */ + public static , T2 extends Comparable> int compare(T1 a1, T1 b1, T2 a2, T2 b2) { + int res = N.compare(a1, b1); + + return res == 0 ? N.compare(a2, b2) : res; + } + + /** + * Continue to compare the pairs of values (a1, b1), (a2, b2), (a3, b3) until they're not equal. + * 0 is returned if all of the pairs of values are equal. + * + * @param a1 + * @param b1 + * @param a2 + * @param b2 + * @param a3 + * @param b3 + * @return + */ + public static , T2 extends Comparable, T3 extends Comparable> int compare(T1 a1, T1 b1, T2 a2, T2 b2, T3 a3, T3 b3) { + int res = 0; + + if ((res = N.compare(a1, b1)) != 0) { + return res; + } else if ((res = N.compare(a2, b2)) != 0) { + return res; + } + + return N.compare(a3, b3); + } + + /** + * Continue to compare the pairs of values (a1, b1), (a2, b2), (a3, b3), (a4, b4) until they're not equal. + * 0 is returned if all of the pairs of values are equal. + * + * @param a1 + * @param b1 + * @param a2 + * @param b2 + * @param a3 + * @param b3 + * @param a4 + * @param b4 + * @return + */ + public static , T2 extends Comparable, T3 extends Comparable, T4 extends Comparable> int compare(T1 a1, T1 b1, T2 a2, + T2 b2, T3 a3, T3 b3, T4 a4, T4 b4) { + int res = 0; + + if ((res = N.compare(a1, b1)) != 0) { + return res; + } else if ((res = N.compare(a2, b2)) != 0) { + return res; + } else if ((res = N.compare(a3, b3)) != 0) { + return res; + } + + return N.compare(a4, b4); } /** - * Checks if the specified {@code arg} is {@code null} or empty, and throws {@code IllegalArgumentException} if it is. + * Continue to compare the pairs of values (a1, b1), (a2, b2), (a3, b3), (a4, b4), (a5, b5) until they're not equal. + * 0 is returned if all of the pairs of values are equal. * - * @param arg - * @param argNameOrErrorMsg - * @throws IllegalArgumentException if the specified {@code arg} is {@code null} or empty. + * @param a1 + * @param b1 + * @param a2 + * @param b2 + * @param a3 + * @param b3 + * @param a4 + * @param b4 + * @param a5 + * @param b5 + * @return */ - public static T[] checkArgNotNullOrEmpty(final T[] arg, final String argNameOrErrorMsg) { - if (N.isNullOrEmpty(arg)) { - if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { - throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be null or empty"); - } else { - throw new IllegalArgumentException(argNameOrErrorMsg); - } + public static , T2 extends Comparable, T3 extends Comparable, T4 extends Comparable, T5 extends Comparable> int compare( + T1 a1, T1 b1, T2 a2, T2 b2, T3 a3, T3 b3, T4 a4, T4 b4, T5 a5, T5 b5) { + int res = 0; + + if ((res = N.compare(a1, b1)) != 0) { + return res; + } else if ((res = N.compare(a2, b2)) != 0) { + return res; + } else if ((res = N.compare(a3, b3)) != 0) { + return res; + } else if ((res = N.compare(a4, b4)) != 0) { + return res; } - return arg; + return N.compare(a5, b5); } /** - * Checks if the specified {@code arg} is {@code null} or empty, and throws {@code IllegalArgumentException} if it is. + * Continue to compare the pairs of values (a1, b1), (a2, b2), (a3, b3), (a4, b4), (a5, b5), (a6, b6) until they're not equal. + * 0 is returned if all of the pairs of values are equal. * - * @param arg - * @param argNameOrErrorMsg - * @throws IllegalArgumentException if the specified {@code arg} is {@code null} or empty. + * @param a1 + * @param b1 + * @param a2 + * @param b2 + * @param a3 + * @param b3 + * @param a4 + * @param b4 + * @param a5 + * @param b5 + * @param a6 + * @param b6 + * @return */ - public static > T checkArgNotNullOrEmpty(final T arg, final String argNameOrErrorMsg) { - if (N.isNullOrEmpty(arg)) { - if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { - throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be null or empty"); - } else { - throw new IllegalArgumentException(argNameOrErrorMsg); - } + public static , T2 extends Comparable, T3 extends Comparable, T4 extends Comparable, T5 extends Comparable, T6 extends Comparable> int compare( + T1 a1, T1 b1, T2 a2, T2 b2, T3 a3, T3 b3, T4 a4, T4 b4, T5 a5, T5 b5, T6 a6, T6 b6) { + int res = 0; + + if ((res = N.compare(a1, b1)) != 0) { + return res; + } else if ((res = N.compare(a2, b2)) != 0) { + return res; + } else if ((res = N.compare(a3, b3)) != 0) { + return res; + } else if ((res = N.compare(a4, b4)) != 0) { + return res; + } else if ((res = N.compare(a5, b5)) != 0) { + return res; } - return arg; + return N.compare(a6, b6); } /** - * Checks if the specified {@code arg} is {@code null} or empty, and throws {@code IllegalArgumentException} if it is. + * Continue to compare the pairs of values (a1, b1), (a2, b2), (a3, b3), (a4, b4), (a5, b5), (a6, b6), (a7, b7) until they're not equal. + * 0 is returned if all of the pairs of values are equal. * - * @param arg - * @param argNameOrErrorMsg - * @throws IllegalArgumentException if the specified {@code arg} is {@code null} or empty. + * @param a1 + * @param b1 + * @param a2 + * @param b2 + * @param a3 + * @param b3 + * @param a4 + * @param b4 + * @param a5 + * @param b5 + * @param a6 + * @param b6 + * @param a7 + * @param b7 + * @return */ - public static > T checkArgNotNullOrEmpty(final T arg, final String argNameOrErrorMsg) { - if (N.isNullOrEmpty(arg)) { - if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { - throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be null or empty"); - } else { - throw new IllegalArgumentException(argNameOrErrorMsg); - } + public static , T2 extends Comparable, T3 extends Comparable, T4 extends Comparable, T5 extends Comparable, T6 extends Comparable, T7 extends Comparable> int compare( + T1 a1, T1 b1, T2 a2, T2 b2, T3 a3, T3 b3, T4 a4, T4 b4, T5 a5, T5 b5, T6 a6, T6 b6, T7 a7, T7 b7) { + int res = 0; + + if ((res = N.compare(a1, b1)) != 0) { + return res; + } else if ((res = N.compare(a2, b2)) != 0) { + return res; + } else if ((res = N.compare(a3, b3)) != 0) { + return res; + } else if ((res = N.compare(a4, b4)) != 0) { + return res; + } else if ((res = N.compare(a5, b5)) != 0) { + return res; + } else if ((res = N.compare(a6, b6)) != 0) { + return res; } - return arg; + return N.compare(a7, b7); } - /** - * Check if the specified parameter is null or empty or blank - * - * @param arg - * @param msg name of parameter or error message - * @return the input parameter - * @throws IllegalArgumentException if the specified parameter is null or empty. - */ - // DON'T change 'OrEmptyOrBlank' to 'OrBlank' because of the occurring order in the auto-completed context menu. - public static T checkArgNotNullOrEmptyOrBlank(final T arg, final String msg) { - if (N.isNullOrEmptyOrBlank(arg)) { - if (isNullErrorMsg(msg)) { - throw new IllegalArgumentException(msg); - } else { - throw new IllegalArgumentException("'" + msg + "' can not be null or empty or blank"); + public static int compare(final boolean[] a, final boolean[] b) { + if (N.isNullOrEmpty(a)) { + return N.isNullOrEmpty(b) ? 0 : -1; + } else if (N.isNullOrEmpty(b)) { + return 1; + } + + for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { + if (a[i] != b[i]) { + return a[i] ? 1 : -1; } } - return arg; + return a.length - b.length; } - /** - * Checks if the specified {@code arg} is not negative, and throws {@code IllegalArgumentException} if it is. - * - * @param arg - * @param argNameOrErrorMsg - * @throws IllegalArgumentException if the specified {@code arg} is negative. - */ - public static int checkArgNotNegative(final int arg, final String argNameOrErrorMsg) { - if (arg < 0) { - if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { - throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be negative: " + arg); - } else { - throw new IllegalArgumentException(argNameOrErrorMsg); + public static int compare(final boolean[] a, final int fromIndexA, final boolean[] b, final int fromIndexB, final int len) { + N.checkArgNotNegative(len, "len"); + N.checkFromIndexSize(fromIndexA, len, len(a)); + N.checkFromIndexSize(fromIndexB, len, len(b)); + + if ((fromIndexA == fromIndexB && a == b) || len == 0) { + return 0; + } + + for (int i = fromIndexA, j = fromIndexB, k = 0; k < len; i++, j++, k++) { + if (a[i] != b[j]) { + return a[i] ? 1 : -1; } } - return arg; + return 0; } - /** - * Checks if the specified {@code arg} is not negative, and throws {@code IllegalArgumentException} if it is. - * - * @param arg - * @param argNameOrErrorMsg - * @throws IllegalArgumentException if the specified {@code arg} is negative. - */ - public static long checkArgNotNegative(final long arg, final String argNameOrErrorMsg) { - if (arg < 0) { - if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { - throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be negative: " + arg); - } else { - throw new IllegalArgumentException(argNameOrErrorMsg); + public static int compare(final char[] a, final char[] b) { + if (N.isNullOrEmpty(a)) { + return N.isNullOrEmpty(b) ? 0 : -1; + } else if (N.isNullOrEmpty(b)) { + return 1; + } + + for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { + if (a[i] != b[i]) { + return a[i] > b[i] ? 1 : -1; } } - return arg; + return a.length - b.length; } - /** - * Checks if the specified {@code arg} is not negative, and throws {@code IllegalArgumentException} if it is. - * - * @param arg - * @param argNameOrErrorMsg - * @throws IllegalArgumentException if the specified {@code arg} is negative. - */ - public static double checkArgNotNegative(final double arg, final String argNameOrErrorMsg) { - if (arg < 0) { - if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { - throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be negative: " + arg); - } else { - throw new IllegalArgumentException(argNameOrErrorMsg); + public static int compare(final char[] a, final int fromIndexA, final char[] b, final int fromIndexB, final int len) { + N.checkArgNotNegative(len, "len"); + N.checkFromIndexSize(fromIndexA, len, len(a)); + N.checkFromIndexSize(fromIndexB, len, len(b)); + + if ((fromIndexA == fromIndexB && a == b) || len == 0) { + return 0; + } + + for (int i = fromIndexA, j = fromIndexB, k = 0; k < len; i++, j++, k++) { + if (a[i] != b[j]) { + return a[i] > b[i] ? 1 : -1; } } - return arg; + return 0; } - /** - * Checks if the specified {@code arg} is positive, and throws {@code IllegalArgumentException} if it is not. - * - * @param arg - * @param argNameOrErrorMsg - * @throws IllegalArgumentException if the specified {@code arg} is negative. - */ - public static int checkArgPositive(final int arg, final String argNameOrErrorMsg) { - if (arg <= 0) { - if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { - throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be zero or negative: " + arg); - } else { - throw new IllegalArgumentException(argNameOrErrorMsg); + public static int compare(final byte[] a, final byte[] b) { + if (N.isNullOrEmpty(a)) { + return N.isNullOrEmpty(b) ? 0 : -1; + } else if (N.isNullOrEmpty(b)) { + return 1; + } + + for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { + if (a[i] != b[i]) { + return a[i] > b[i] ? 1 : -1; } } - return arg; + return a.length - b.length; } - /** - * Checks if the specified {@code arg} is positive, and throws {@code IllegalArgumentException} if it is not. - * - * @param arg - * @param argNameOrErrorMsg - * @throws IllegalArgumentException if the specified {@code arg} is negative. - */ - public static long checkArgPositive(final long arg, final String argNameOrErrorMsg) { - if (arg <= 0) { - if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { - throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be zero or negative: " + arg); - } else { - throw new IllegalArgumentException(argNameOrErrorMsg); + public static int compare(final byte[] a, final int fromIndexA, final byte[] b, final int fromIndexB, final int len) { + N.checkArgNotNegative(len, "len"); + N.checkFromIndexSize(fromIndexA, len, len(a)); + N.checkFromIndexSize(fromIndexB, len, len(b)); + + if ((fromIndexA == fromIndexB && a == b) || len == 0) { + return 0; + } + + for (int i = fromIndexA, j = fromIndexB, k = 0; k < len; i++, j++, k++) { + if (a[i] != b[j]) { + return a[i] > b[i] ? 1 : -1; } } - return arg; + return 0; } - /** - * Checks if the specified {@code arg} is positive, and throws {@code IllegalArgumentException} if it is not. - * - * @param arg - * @param argNameOrErrorMsg - * @throws IllegalArgumentException if the specified {@code arg} is negative. - */ - public static double checkArgPositive(final double arg, final String argNameOrErrorMsg) { - if (arg <= 0) { - if (argNameOrErrorMsg.indexOf(' ') == N.INDEX_NOT_FOUND) { - throw new IllegalArgumentException("'" + argNameOrErrorMsg + "' can not be zero or negative: " + arg); - } else { - throw new IllegalArgumentException(argNameOrErrorMsg); + public static int compare(final short[] a, final short[] b) { + if (N.isNullOrEmpty(a)) { + return N.isNullOrEmpty(b) ? 0 : -1; + } else if (N.isNullOrEmpty(b)) { + return 1; + } + + for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { + if (a[i] != b[i]) { + return a[i] > b[i] ? 1 : -1; } } - return arg; + return a.length - b.length; } - /** - * Ensures the truth of an expression involving the state of the calling instance, but not - * involving any parameters to the calling method. - * - * @param expression a boolean expression - * @throws IllegalStateException if {@code expression} is false - */ - public static void checkState(boolean expression) { - if (!expression) { - throw new IllegalStateException(); + public static int compare(final short[] a, final int fromIndexA, final short[] b, final int fromIndexB, final int len) { + N.checkArgNotNegative(len, "len"); + N.checkFromIndexSize(fromIndexA, len, len(a)); + N.checkFromIndexSize(fromIndexB, len, len(b)); + + if ((fromIndexA == fromIndexB && a == b) || len == 0) { + return 0; } - } - /** - * Ensures the truth of an expression involving the state of the calling instance, but not - * involving any parameters to the calling method. - * - * @param expression a boolean expression - * @param errorMessage the exception message to use if the check fails; will be converted to a - * string using {@link String#valueOf(Object)} - * @throws IllegalStateException if {@code expression} is false - */ - public static void checkState(boolean expression, String errorMessage) { - if (!expression) { - throw new IllegalStateException(errorMessage); + for (int i = fromIndexA, j = fromIndexB, k = 0; k < len; i++, j++, k++) { + if (a[i] != b[j]) { + return a[i] > b[i] ? 1 : -1; + } } + + return 0; } - /** - * Ensures the truth of an expression involving the state of the calling instance, but not - * involving any parameters to the calling method. - * - *

See {@link #checkState(boolean, String, Object...)} for details. - */ - public static void checkState(boolean b, String errorMessageTemplate, int p) { - if (!b) { - throw new IllegalStateException(format(errorMessageTemplate, p)); + public static int compare(final int[] a, final int[] b) { + if (N.isNullOrEmpty(a)) { + return N.isNullOrEmpty(b) ? 0 : -1; + } else if (N.isNullOrEmpty(b)) { + return 1; } - } - /** - * Ensures the truth of an expression involving the state of the calling instance, but not - * involving any parameters to the calling method. - * - *

See {@link #checkState(boolean, String, Object...)} for details. - */ - public static void checkState(boolean b, String errorMessageTemplate, int p1, int p2) { - if (!b) { - throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); + for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { + if (a[i] != b[i]) { + return a[i] > b[i] ? 1 : -1; + } } + + return a.length - b.length; } - /** - * Ensures the truth of an expression involving the state of the calling instance, but not - * involving any parameters to the calling method. - * - *

See {@link #checkState(boolean, String, Object...)} for details. - */ - public static void checkState(boolean b, String errorMessageTemplate, int p1, int p2, int p3) { - if (!b) { - throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3)); + public static int compare(final int[] a, final int fromIndexA, final int[] b, final int fromIndexB, final int len) { + N.checkArgNotNegative(len, "len"); + N.checkFromIndexSize(fromIndexA, len, len(a)); + N.checkFromIndexSize(fromIndexB, len, len(b)); + + if ((fromIndexA == fromIndexB && a == b) || len == 0) { + return 0; } - } - /** - * Ensures the truth of an expression involving the state of the calling instance, but not - * involving any parameters to the calling method. - * - *

See {@link #checkState(boolean, String, Object...)} for details. - */ - public static void checkState(boolean b, String errorMessageTemplate, double p) { - if (!b) { - throw new IllegalStateException(format(errorMessageTemplate, p)); + for (int i = fromIndexA, j = fromIndexB, k = 0; k < len; i++, j++, k++) { + if (a[i] != b[j]) { + return a[i] > b[i] ? 1 : -1; + } } + + return 0; } - /** - * Ensures the truth of an expression involving the state of the calling instance, but not - * involving any parameters to the calling method. - * - *

See {@link #checkState(boolean, String, Object...)} for details. - */ - public static void checkState(boolean b, String errorMessageTemplate, double p1, double p2) { - if (!b) { - throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); + public static int compare(final long[] a, final long[] b) { + if (N.isNullOrEmpty(a)) { + return N.isNullOrEmpty(b) ? 0 : -1; + } else if (N.isNullOrEmpty(b)) { + return 1; } - } - /** - * Ensures the truth of an expression involving the state of the calling instance, but not - * involving any parameters to the calling method. - * - *

See {@link #checkState(boolean, String, Object...)} for details. - */ - public static void checkState(boolean b, String errorMessageTemplate, double p1, double p2, double p3) { - if (!b) { - throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3)); + for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { + if (a[i] != b[i]) { + return a[i] > b[i] ? 1 : -1; + } } + + return a.length - b.length; } - /** - * Ensures the truth of an expression involving the state of the calling instance, but not - * involving any parameters to the calling method. - * - *

See {@link #checkState(boolean, String, Object...)} for details. - */ - public static void checkState(boolean b, String errorMessageTemplate, Object p) { - if (!b) { - throw new IllegalStateException(format(errorMessageTemplate, p)); + public static int compare(final long[] a, final int fromIndexA, final long[] b, final int fromIndexB, final int len) { + N.checkArgNotNegative(len, "len"); + N.checkFromIndexSize(fromIndexA, len, len(a)); + N.checkFromIndexSize(fromIndexB, len, len(b)); + + if ((fromIndexA == fromIndexB && a == b) || len == 0) { + return 0; } - } - /** - * Ensures the truth of an expression involving the state of the calling instance, but not - * involving any parameters to the calling method. - * - *

See {@link #checkState(boolean, String, Object...)} for details. - */ - public static void checkState(boolean b, String errorMessageTemplate, Object p1, Object p2) { - if (!b) { - throw new IllegalStateException(format(errorMessageTemplate, p1, p2)); + for (int i = fromIndexA, j = fromIndexB, k = 0; k < len; i++, j++, k++) { + if (a[i] != b[j]) { + return a[i] > b[i] ? 1 : -1; + } } + + return 0; } - /** - * Ensures the truth of an expression involving the state of the calling instance, but not - * involving any parameters to the calling method. - * - *

See {@link #checkState(boolean, String, Object...)} for details. - */ - public static void checkState(boolean b, String errorMessageTemplate, Object p1, Object p2, Object p3) { - if (!b) { - throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3)); + public static int compare(final float[] a, final float[] b) { + if (N.isNullOrEmpty(a)) { + return N.isNullOrEmpty(b) ? 0 : -1; + } else if (N.isNullOrEmpty(b)) { + return 1; } - } - public static void checkState(boolean b, Try.Supplier errorMessageSupplier) throws E { - if (!b) { - throw new IllegalStateException(errorMessageSupplier.get()); + int value = 0; + + for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { + if ((value = Float.compare(a[i], b[i])) != 0) { + return value; + } } + + return a.length - b.length; } - static String format(String template, Object arg) { - template = String.valueOf(template); // null -> "null" + public static int compare(final float[] a, final int fromIndexA, final float[] b, final int fromIndexB, final int len) { + N.checkArgNotNegative(len, "len"); + N.checkFromIndexSize(fromIndexA, len, len(a)); + N.checkFromIndexSize(fromIndexB, len, len(b)); - // start substituting the arguments into the '%s' placeholders - final StringBuilder sb = Objectory.createStringBuilder(template.length() + 16); + if ((fromIndexA == fromIndexB && a == b) || len == 0) { + return 0; + } - String placeholder = "{}"; - int placeholderStart = template.indexOf(placeholder); + int value = 0; - if (placeholderStart < 0) { - placeholder = "%s"; - placeholderStart = template.indexOf(placeholder); + for (int i = fromIndexA, j = fromIndexB, k = 0; k < len; i++, j++, k++) { + if ((value = Float.compare(a[i], b[j])) != 0) { + return value; + } } - if (placeholderStart >= 0) { - sb.append(template, 0, placeholderStart); - sb.append(N.toString(arg)); - sb.append(template, placeholderStart + 2, template.length()); - } else { - sb.append(" ["); - sb.append(N.toString(arg)); - sb.append(']'); + return 0; + } + + public static int compare(final double[] a, final double[] b) { + if (N.isNullOrEmpty(a)) { + return N.isNullOrEmpty(b) ? 0 : -1; + } else if (N.isNullOrEmpty(b)) { + return 1; } - final String result = sb.toString(); + int value = 0; - Objectory.recycle(sb); + for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { + if ((value = Double.compare(a[i], b[i])) != 0) { + return value; + } + } - return result; + return a.length - b.length; } - static String format(String template, Object arg1, Object arg2) { - template = String.valueOf(template); // null -> "null" + public static int compare(final double[] a, final int fromIndexA, final double[] b, final int fromIndexB, final int len) { + N.checkArgNotNegative(len, "len"); + N.checkFromIndexSize(fromIndexA, len, len(a)); + N.checkFromIndexSize(fromIndexB, len, len(b)); - // start substituting the arguments into the '%s' placeholders - final StringBuilder sb = Objectory.createStringBuilder(template.length() + 32); + if ((fromIndexA == fromIndexB && a == b) || len == 0) { + return 0; + } - String placeholder = "{}"; - int placeholderStart = template.indexOf(placeholder); + int value = 0; - if (placeholderStart < 0) { - placeholder = "%s"; - placeholderStart = template.indexOf(placeholder); + for (int i = fromIndexA, j = fromIndexB, k = 0; k < len; i++, j++, k++) { + if ((value = Double.compare(a[i], b[j])) != 0) { + return value; + } } - int templateStart = 0; - int cnt = 0; + return 0; + } - if (placeholderStart >= 0) { - cnt++; - sb.append(template, templateStart, placeholderStart); - sb.append(N.toString(arg1)); - templateStart = placeholderStart + 2; - placeholderStart = template.indexOf(placeholder, templateStart); + public static > int compare(final T[] a, final T[] b) { + final Comparator cmp = NATURAL_ORDER; - if (placeholderStart >= 0) { - cnt++; - sb.append(template, templateStart, placeholderStart); - sb.append(N.toString(arg2)); - templateStart = placeholderStart + 2; - } + return compare(a, b, cmp); + } - sb.append(template, templateStart, template.length()); - } + public static > int compare(final T[] a, final int fromIndexA, final T[] b, final int fromIndexB, final int len) { + final Comparator cmp = NATURAL_ORDER; - if (cnt == 0) { - sb.append(" ["); - sb.append(N.toString(arg1)); - sb.append(", "); - sb.append(N.toString(arg2)); - sb.append(']'); - } else if (cnt == 1) { - sb.append(" ["); - sb.append(N.toString(arg2)); - sb.append(']'); + return compare(a, fromIndexA, b, fromIndexB, len, cmp); + } + + public static int compare(final T[] a, final T[] b, Comparator cmp) { + if (N.isNullOrEmpty(a)) { + return N.isNullOrEmpty(b) ? 0 : -1; + } else if (N.isNullOrEmpty(b)) { + return 1; } - final String result = sb.toString(); + cmp = cmp == null ? NATURAL_ORDER : cmp; - Objectory.recycle(sb); + int value = 0; - return result; + for (int i = 0, minLen = min(a.length, b.length); i < minLen; i++) { + if ((value = cmp.compare(a[i], b[i])) != 0) { + return value; + } + } + + return a.length - b.length; } - static String format(String template, Object arg1, Object arg2, Object arg3) { - template = String.valueOf(template); // null -> "null" + public static int compare(final T[] a, final int fromIndexA, final T[] b, final int fromIndexB, final int len, Comparator cmp) { + N.checkArgNotNegative(len, "len"); + N.checkFromIndexSize(fromIndexA, len, len(a)); + N.checkFromIndexSize(fromIndexB, len, len(b)); - // start substituting the arguments into the '%s' placeholders - final StringBuilder sb = Objectory.createStringBuilder(template.length() + 48); + if ((fromIndexA == fromIndexB && a == b) || len == 0) { + return 0; + } - String placeholder = "{}"; - int placeholderStart = template.indexOf(placeholder); + cmp = cmp == null ? NATURAL_ORDER : cmp; - if (placeholderStart < 0) { - placeholder = "%s"; - placeholderStart = template.indexOf(placeholder); - } + int value = 0; - int templateStart = 0; - int cnt = 0; + for (int i = fromIndexA, j = fromIndexB, k = 0; k < len; i++, j++, k++) { + if ((value = cmp.compare(a[i], b[j])) != 0) { + return value; + } + } - if (placeholderStart >= 0) { - cnt++; - sb.append(template, templateStart, placeholderStart); - sb.append(N.toString(arg1)); - templateStart = placeholderStart + 2; - placeholderStart = template.indexOf(placeholder, templateStart); + return 0; + } - if (placeholderStart >= 0) { - cnt++; - sb.append(template, templateStart, placeholderStart); - sb.append(N.toString(arg2)); - templateStart = placeholderStart + 2; - placeholderStart = template.indexOf(placeholder, templateStart); + public static > int compare(final Collection a, final Collection b) { + final Comparator cmp = NATURAL_ORDER; - if (placeholderStart >= 0) { - cnt++; - sb.append(template, templateStart, placeholderStart); - sb.append(N.toString(arg3)); - templateStart = placeholderStart + 2; - } - } + return compare(a, b, cmp); + } - sb.append(template, templateStart, template.length()); + public static int compare(final Collection a, final Collection b, Comparator cmp) { + if (N.isNullOrEmpty(a)) { + return N.isNullOrEmpty(b) ? 0 : -1; + } else if (N.isNullOrEmpty(b)) { + return 1; } - if (cnt == 0) { - sb.append(" ["); - sb.append(N.toString(arg1)); - sb.append(", "); - sb.append(N.toString(arg2)); - sb.append(", "); - sb.append(N.toString(arg3)); - sb.append(']'); - } else if (cnt == 1) { - sb.append(" ["); - sb.append(N.toString(arg2)); - sb.append(", "); - sb.append(N.toString(arg3)); - sb.append(']'); - } else if (cnt == 2) { - sb.append(" ["); - sb.append(N.toString(arg3)); - sb.append(']'); - } + cmp = cmp == null ? NATURAL_ORDER : cmp; - final String result = sb.toString(); + final Iterator iterA = a.iterator(); + final Iterator iterB = b.iterator(); + int value = 0; - Objectory.recycle(sb); + for (int i = 0, minLen = min(a.size(), b.size()); i < minLen; i++) { + if ((value = cmp.compare(iterA.next(), iterB.next())) != 0) { + return value; + } + } - return result; + return a.size() - b.size(); } - /** - * Substitutes each {@code %s} in {@code template} with an argument. These are matched by - * position: the first {@code %s} gets {@code args[0]}, etc. If there are more arguments than - * placeholders, the unmatched arguments will be appended to the end of the formatted message in - * square braces. - * - * @param template a non-null string containing 0 or more {@code %s} placeholders. - * @param args the arguments to be substituted into the message template. Arguments are converted - * to strings using {@link String#valueOf(Object)}. Arguments can be null. - */ - // Note that this is somewhat-improperly used from Verify.java as well. - static String format(String template, Object... args) { - template = String.valueOf(template); // null -> "null" + public static int compare(final Collection a, int fromIndexA, final Collection b, int fromIndexB, final int len, Comparator cmp) { + N.checkArgNotNegative(len, "len"); + N.checkFromIndexSize(fromIndexA, len, size(a)); + N.checkFromIndexSize(fromIndexB, len, size(b)); - if (N.isNullOrEmpty(args)) { - return template; + if ((fromIndexA == fromIndexB && a == b) || len == 0) { + return 0; } - // start substituting the arguments into the '%s' placeholders - final StringBuilder sb = Objectory.createStringBuilder(template.length() + 16 * args.length); - int templateStart = 0; - int i = 0; - - String placeholder = "{}"; - int placeholderStart = template.indexOf(placeholder); + cmp = cmp == null ? NATURAL_ORDER : cmp; + final Iterator iterA = a.iterator(); + final Iterator iterB = b.iterator(); - if (placeholderStart < 0) { - placeholder = "%s"; - placeholderStart = template.indexOf(placeholder); + while (fromIndexA-- > 0) { + iterA.next(); } - while (placeholderStart >= 0 && i < args.length) { - sb.append(template, templateStart, placeholderStart); - sb.append(N.toString(args[i++])); - templateStart = placeholderStart + 2; - placeholderStart = template.indexOf(placeholder, templateStart); + while (fromIndexB-- > 0) { + iterB.next(); } - sb.append(template, templateStart, template.length()); + int value = 0; - // if we run out of placeholders, append the extra args in square braces - if (i < args.length) { - sb.append(" ["); - sb.append(N.toString(args[i++])); - while (i < args.length) { - sb.append(", "); - sb.append(N.toString(args[i++])); + for (int i = 0; i < len; i++) { + if ((value = cmp.compare(iterA.next(), iterB.next())) != 0) { + return value; } - sb.append(']'); } - final String result = sb.toString(); - - Objectory.recycle(sb); + return 0; + } - return result; + public static int compareIgnoreCase(final String a, final String b) { + return a == null ? (b == null ? 0 : -1) : (b == null ? 1 : a.compareToIgnoreCase(b)); } /** @@ -6883,10 +7138,7 @@ public static boolean equals(final boolean[] a, final boolean[] b) { * @return */ public static boolean equals(final boolean[] a, final int fromIndexA, final boolean[] b, final int fromIndexB, final int len) { - if (len < 0) { - throw new IllegalArgumentException("'len' can not be negative"); - } - + N.checkArgNotNegative(len, "len"); N.checkFromIndexSize(fromIndexA, len, len(a)); N.checkFromIndexSize(fromIndexB, len, len(b)); @@ -6924,10 +7176,7 @@ public static boolean equals(final char[] a, final char[] b) { * @return */ public static boolean equals(final char[] a, final int fromIndexA, final char[] b, final int fromIndexB, final int len) { - if (len < 0) { - throw new IllegalArgumentException("'len' can not be negative"); - } - + N.checkArgNotNegative(len, "len"); N.checkFromIndexSize(fromIndexA, len, len(a)); N.checkFromIndexSize(fromIndexB, len, len(b)); @@ -6965,10 +7214,7 @@ public static boolean equals(final byte[] a, final byte[] b) { * @return */ public static boolean equals(final byte[] a, final int fromIndexA, final byte[] b, final int fromIndexB, final int len) { - if (len < 0) { - throw new IllegalArgumentException("'len' can not be negative"); - } - + N.checkArgNotNegative(len, "len"); N.checkFromIndexSize(fromIndexA, len, len(a)); N.checkFromIndexSize(fromIndexB, len, len(b)); @@ -7006,10 +7252,7 @@ public static boolean equals(final short[] a, final short[] b) { * @return */ public static boolean equals(final short[] a, final int fromIndexA, final short[] b, final int fromIndexB, final int len) { - if (len < 0) { - throw new IllegalArgumentException("'len' can not be negative"); - } - + N.checkArgNotNegative(len, "len"); N.checkFromIndexSize(fromIndexA, len, len(a)); N.checkFromIndexSize(fromIndexB, len, len(b)); @@ -7047,10 +7290,7 @@ public static boolean equals(final int[] a, final int[] b) { * @return */ public static boolean equals(final int[] a, final int fromIndexA, final int[] b, final int fromIndexB, final int len) { - if (len < 0) { - throw new IllegalArgumentException("'len' can not be negative"); - } - + N.checkArgNotNegative(len, "len"); N.checkFromIndexSize(fromIndexA, len, len(a)); N.checkFromIndexSize(fromIndexB, len, len(b)); @@ -7088,10 +7328,7 @@ public static boolean equals(final long[] a, final long[] b) { * @return */ public static boolean equals(final long[] a, final int fromIndexA, final long[] b, final int fromIndexB, final int len) { - if (len < 0) { - throw new IllegalArgumentException("'len' can not be negative"); - } - + N.checkArgNotNegative(len, "len"); N.checkFromIndexSize(fromIndexA, len, len(a)); N.checkFromIndexSize(fromIndexB, len, len(b)); @@ -7129,10 +7366,7 @@ public static boolean equals(final float[] a, final float[] b) { * @return */ public static boolean equals(final float[] a, final int fromIndexA, final float[] b, final int fromIndexB, final int len) { - if (len < 0) { - throw new IllegalArgumentException("'len' can not be negative"); - } - + N.checkArgNotNegative(len, "len"); N.checkFromIndexSize(fromIndexA, len, len(a)); N.checkFromIndexSize(fromIndexB, len, len(b)); @@ -7170,10 +7404,7 @@ public static boolean equals(final double[] a, final double[] b) { * @return */ public static boolean equals(final double[] a, final int fromIndexA, final double[] b, final int fromIndexB, final int len) { - if (len < 0) { - throw new IllegalArgumentException("'len' can not be negative"); - } - + N.checkArgNotNegative(len, "len"); N.checkFromIndexSize(fromIndexA, len, len(a)); N.checkFromIndexSize(fromIndexB, len, len(b)); @@ -7211,10 +7442,7 @@ public static boolean equals(final Object[] a, final Object[] b) { * @return */ public static boolean equals(final Object[] a, final int fromIndexA, final Object[] b, final int fromIndexB, final int len) { - if (len < 0) { - throw new IllegalArgumentException("'len' can not be negative"); - } - + N.checkArgNotNegative(len, "len"); N.checkFromIndexSize(fromIndexA, len, len(a)); N.checkFromIndexSize(fromIndexB, len, len(b)); @@ -7244,10 +7472,7 @@ public static boolean deepEquals(final Object[] a, final Object[] b) { } public static boolean deepEquals(final Object[] a, final int fromIndexA, final Object[] b, final int fromIndexB, final int len) { - if (len < 0) { - throw new IllegalArgumentException("'len' can not be negative"); - } - + N.checkArgNotNegative(len, "len"); N.checkFromIndexSize(fromIndexA, len, len(a)); N.checkFromIndexSize(fromIndexB, len, len(b)); @@ -7280,10 +7505,7 @@ public static boolean equalsIgnoreCase(final String[] a, final String[] b) { * @return */ public static boolean equalsIgnoreCase(final String[] a, final int fromIndexA, final String[] b, final int fromIndexB, final int len) { - if (len < 0) { - throw new IllegalArgumentException("'len' can not be negative"); - } - + N.checkArgNotNegative(len, "len"); N.checkFromIndexSize(fromIndexA, len, len(a)); N.checkFromIndexSize(fromIndexB, len, len(b)); @@ -9375,18 +9597,22 @@ public static void swap(final Pair pair) { * * @param pair * @param predicate + * @return * @throws NullPointerExceptoin if the specified {@code pair} or {@code predicate} is {@code null}. * @throws E */ - public static void swapIf(final Pair pair, Try.Predicate, E> predicate) throws E { + public static boolean swapIf(final Pair pair, Try.Predicate, E> predicate) throws E { if (predicate.test(pair)) { pair.set(pair.right, pair.left); + return true; } + + return false; } /** * - * @param triple + * @param triple * @throws NullPointerExceptoin if the specified {@code pair} is {@code null}. */ public static void swap(final Triple triple) { @@ -9399,15 +9625,19 @@ public static void swap(final Triple triple) { * * @param triple * @param predicate + * @return * @throws NullPointerExceptoin if the specified {@code triple} or {@code predicate} is {@code null}. * @throws E */ - public static void swapIf(final Triple triple, Try.Predicate, E> predicate) throws E { + public static boolean swapIf(final Triple triple, Try.Predicate, E> predicate) throws E { if (predicate.test(triple)) { final T left = triple.left; triple.setLeft(triple.right); triple.setRight(left); + return true; } + + return false; } public static void fill(final boolean[] a, final boolean val) { @@ -9604,19 +9834,19 @@ public static T fill(Class entityClass) { * Fill the properties of the entity with random values. * * @param entityClass entity class with getter/setter methods - * @param len + * @param count * @return * @deprecated */ @Deprecated - public static List fill(Class entityClass, int len) { + public static List fill(Class entityClass, int count) { if (N.isEntity(entityClass) == false) { throw new IllegalArgumentException(entityClass.getCanonicalName() + " is not a valid entity class with property getter/setter method"); } - final List resultList = new ArrayList<>(len); + final List resultList = new ArrayList<>(count); - for (int i = 0; i < len; i++) { + for (int i = 0; i < count; i++) { final T entity = N.newInstance(entityClass); fill(entity); resultList.add(entity); @@ -26225,7 +26455,7 @@ public static double median(final double a, final double b, final double c) { * @see #median(int...) */ public static > T median(final T a, final T b, final T c) { - return (T) median(a, b, c, NULL_MIN_COMPARATOR); + return (T) median(a, b, c, NATURAL_ORDER); } /** @@ -26237,7 +26467,9 @@ public static > T median(final T a, final T b, f * @return the median of the values * @see #median(int...) */ - public static T median(final T a, final T b, final T c, final Comparator cmp) { + public static T median(final T a, final T b, final T c, Comparator cmp) { + cmp = cmp == null ? NATURAL_ORDER : cmp; + int ab = cmp.compare(a, b); int ac = cmp.compare(a, c); int bc = 0; diff --git a/src/com/landawn/abacus/util/Primitives.java b/src/com/landawn/abacus/util/Primitives.java index e2335ea2..08fcabc1 100644 --- a/src/com/landawn/abacus/util/Primitives.java +++ b/src/com/landawn/abacus/util/Primitives.java @@ -523,6 +523,118 @@ public static Double[][] box(double[][] a) { return result; } + public static Boolean[][][] box(boolean[][][] a) { + if (a == null) { + return null; + } + + final Boolean[][][] result = new Boolean[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = box(a[i]); + } + + return result; + } + + public static Character[][][] box(char[][][] a) { + if (a == null) { + return null; + } + + final Character[][][] result = new Character[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = box(a[i]); + } + + return result; + } + + public static Byte[][][] box(byte[][][] a) { + if (a == null) { + return null; + } + + final Byte[][][] result = new Byte[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = box(a[i]); + } + + return result; + } + + public static Short[][][] box(short[][][] a) { + if (a == null) { + return null; + } + + final Short[][][] result = new Short[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = box(a[i]); + } + + return result; + } + + public static Integer[][][] box(int[][][] a) { + if (a == null) { + return null; + } + + final Integer[][][] result = new Integer[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = box(a[i]); + } + + return result; + } + + public static Long[][][] box(long[][][] a) { + if (a == null) { + return null; + } + + final Long[][][] result = new Long[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = box(a[i]); + } + + return result; + } + + public static Float[][][] box(float[][][] a) { + if (a == null) { + return null; + } + + final Float[][][] result = new Float[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = box(a[i]); + } + + return result; + } + + public static Double[][][] box(double[][][] a) { + if (a == null) { + return null; + } + + final Double[][][] result = new Double[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = box(a[i]); + } + + return result; + } + static T box(final Object a) { if (a == null) { return null; @@ -1249,6 +1361,150 @@ public static double[][] unbox(Double[][] a, double valueForNull) { return result; } + public static boolean[][][] unbox(Boolean[][][] a) { + return unbox(a, false); + } + + public static boolean[][][] unbox(Boolean[][][] a, boolean valueForNull) { + if (a == null) { + return null; + } + + final boolean[][][] result = new boolean[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = unbox(a[i], valueForNull); + } + + return result; + } + + public static char[][][] unbox(Character[][][] a) { + return unbox(a, (char) 0); + } + + public static char[][][] unbox(Character[][][] a, char valueForNull) { + if (a == null) { + return null; + } + + final char[][][] result = new char[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = unbox(a[i], valueForNull); + } + + return result; + } + + public static byte[][][] unbox(Byte[][][] a) { + return unbox(a, (byte) 0); + } + + public static byte[][][] unbox(Byte[][][] a, byte valueForNull) { + if (a == null) { + return null; + } + + final byte[][][] result = new byte[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = unbox(a[i], valueForNull); + } + + return result; + } + + public static short[][][] unbox(Short[][][] a) { + return unbox(a, (short) 0); + } + + public static short[][][] unbox(Short[][][] a, short valueForNull) { + if (a == null) { + return null; + } + + final short[][][] result = new short[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = unbox(a[i], valueForNull); + } + + return result; + } + + public static int[][][] unbox(Integer[][][] a) { + return unbox(a, 0); + } + + public static int[][][] unbox(Integer[][][] a, int valueForNull) { + if (a == null) { + return null; + } + + final int[][][] result = new int[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = unbox(a[i], valueForNull); + } + + return result; + } + + public static long[][][] unbox(Long[][][] a) { + return unbox(a, 0); + } + + public static long[][][] unbox(Long[][][] a, long valueForNull) { + if (a == null) { + return null; + } + + final long[][][] result = new long[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = unbox(a[i], valueForNull); + } + + return result; + } + + public static float[][][] unbox(Float[][][] a) { + return unbox(a, 0); + } + + public static float[][][] unbox(Float[][][] a, float valueForNull) { + if (a == null) { + return null; + } + + final float[][][] result = new float[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = unbox(a[i], valueForNull); + } + + return result; + } + + public static double[][][] unbox(Double[][][] a) { + return unbox(a, 0); + } + + public static double[][][] unbox(Double[][][] a, double valueForNull) { + if (a == null) { + return null; + } + + final double[][][] result = new double[a.length][][]; + + for (int i = 0, len = a.length; i < len; i++) { + result[i] = unbox(a[i], valueForNull); + } + + return result; + } + public static boolean unboxOrDefault(Boolean b) { if (b == null) { return false; diff --git a/src/com/landawn/abacus/util/Synchronized.java b/src/com/landawn/abacus/util/Synchronized.java index 0c0d6940..0d99f689 100644 --- a/src/com/landawn/abacus/util/Synchronized.java +++ b/src/com/landawn/abacus/util/Synchronized.java @@ -14,235 +14,184 @@ package com.landawn.abacus.util; -import com.landawn.abacus.util.function.Callable; -import com.landawn.abacus.util.function.Consumer; -import com.landawn.abacus.util.function.Function; -import com.landawn.abacus.util.function.Predicate; -import com.landawn.abacus.util.function.Runnable; - /** * * @since 0.8 * - * @author Haiyang Li - * @deprecated replaced by {@code Fn#sp(Object, Predicate), Fn#sc(Object, Consumer), Fn#sf(Object, Function)} + * @author Haiyang Li */ -@Deprecated public final class Synchronized { - private final T target; + private final T mutex; + + Synchronized(final T mutex) { + N.checkArgNotNull(mutex); - Synchronized(final T target) { - this.target = target; + this.mutex = mutex; } - public static Synchronized on(final T target) { - N.checkArgNotNull(target); + public static Synchronized on(final T mutex) { + N.checkArgNotNull(mutex); - return new Synchronized<>(target); + return new Synchronized<>(mutex); } /** * - * @param target to locked on. + * @param mutex to locked on. * @param cmd * @return */ - public static Runnable run(final T target, final Try.Runnable cmd) { - N.checkArgNotNull(target); + public static void run(final T mutex, final Try.Runnable cmd) throws E { + N.checkArgNotNull(mutex); N.checkArgNotNull(cmd); - return new Runnable() { - @Override - public void run() { - synchronized (target) { - cmd.run(); - } - } - }; + synchronized (mutex) { + cmd.run(); + } } /** * - * @param target to locked on. + * @param mutex to locked on. * @param cmd * @return */ - public static Callable call(final T target, final Try.Callable cmd) { - N.checkArgNotNull(target); + public static R call(final T mutex, final Try.Callable cmd) throws E { + N.checkArgNotNull(mutex); N.checkArgNotNull(cmd); - return new Callable() { - @Override - public R call() { - synchronized (target) { - return cmd.call(); - } - } - }; + synchronized (mutex) { + return cmd.call(); + } } /** * - * @param target to locked on. + * @param mutex to locked on. * @param predicate * @return */ - public static Predicate test(final T target, final Try.Predicate predicate) { - N.checkArgNotNull(target); + public static boolean test(final T mutex, final Try.Predicate predicate) throws E { + N.checkArgNotNull(mutex); N.checkArgNotNull(predicate); - return new Predicate() { - @Override - public boolean test(T t) { - synchronized (target) { - return predicate.test(t); - } - } - }; + synchronized (mutex) { + return predicate.test(mutex); + } } /** * - * @param target to locked on. + * @param mutex to locked on. * @param predicate * @return */ - public static Predicate test(final T target, final Try.BiPredicate predicate) { - N.checkArgNotNull(target); + public static boolean test(final T mutex, final U u, final Try.BiPredicate predicate) throws E { + N.checkArgNotNull(mutex); N.checkArgNotNull(predicate); - return new Predicate() { - @Override - public boolean test(U t) { - synchronized (target) { - return predicate.test(target, t); - } - } - }; + synchronized (mutex) { + return predicate.test(mutex, u); + } } /** * - * @param target to locked on. + * @param mutex to locked on. * @param consumer * @return */ - public static Consumer accept(final T target, final Try.Consumer consumer) { - N.checkArgNotNull(target); + public static void accept(final T mutex, final Try.Consumer consumer) throws E { + N.checkArgNotNull(mutex); N.checkArgNotNull(consumer); - return new Consumer() { - @Override - public void accept(U t) { - synchronized (target) { - consumer.accept(t); - } - } - }; + synchronized (mutex) { + consumer.accept(mutex); + } } /** * - * @param target to locked on. + * @param mutex to locked on. * @param consumer * @return */ - public static Consumer accept(final T target, final Try.BiConsumer consumer) { - N.checkArgNotNull(target); + public static void accept(final T mutex, final U u, final Try.BiConsumer consumer) throws E { + N.checkArgNotNull(mutex); N.checkArgNotNull(consumer); - return new Consumer() { - @Override - public void accept(U t) { - synchronized (target) { - consumer.accept(target, t); - } - } - }; + synchronized (mutex) { + consumer.accept(mutex, u); + } } /** * - * @param target to locked on. + * @param mutex to locked on. * @param funciton * @return */ - public static Function apply(final T target, final Try.Function funciton) { - N.checkArgNotNull(target); + public static R apply(final T mutex, final Try.Function funciton) throws E { + N.checkArgNotNull(mutex); N.checkArgNotNull(funciton); - return new Function() { - @Override - public R apply(U t) { - synchronized (target) { - return funciton.apply(t); - } - } - }; + synchronized (mutex) { + return funciton.apply(mutex); + } } /** * - * @param target to locked on. + * @param mutex to locked on. * @param funciton * @return */ - public static Function apply(final T target, final Try.BiFunction funciton) { - N.checkArgNotNull(target); + public static R apply(final T mutex, final U u, final Try.BiFunction funciton) throws E { + N.checkArgNotNull(mutex); N.checkArgNotNull(funciton); - return new Function() { - @Override - public R apply(U t) { - synchronized (target) { - return funciton.apply(target, t); - } - } - }; + synchronized (mutex) { + return funciton.apply(mutex, u); + } } public void run(final Try.Runnable cmd) throws E { - N.checkArgNotNull(target); N.checkArgNotNull(cmd); - synchronized (target) { + synchronized (mutex) { cmd.run(); } } public R call(final Try.Callable cmd) throws E { - N.checkArgNotNull(target); N.checkArgNotNull(cmd); - synchronized (target) { + synchronized (mutex) { return cmd.call(); } } public boolean test(final Try.Predicate predicate) throws E { - N.checkArgNotNull(target); N.checkArgNotNull(predicate); - synchronized (target) { - return predicate.test(target); + synchronized (mutex) { + return predicate.test(mutex); } } public void accept(final Try.Consumer consumer) throws E { - N.checkArgNotNull(target); N.checkArgNotNull(consumer); - synchronized (target) { - consumer.accept(target); + synchronized (mutex) { + consumer.accept(mutex); } } public R apply(final Try.Function function) throws E { - N.checkArgNotNull(target); N.checkArgNotNull(function); - synchronized (target) { - return function.apply(target); + synchronized (mutex) { + return function.apply(mutex); } } } diff --git a/src/com/landawn/abacus/util/Timed.java b/src/com/landawn/abacus/util/Timed.java index a28843b3..d2fd8850 100644 --- a/src/com/landawn/abacus/util/Timed.java +++ b/src/com/landawn/abacus/util/Timed.java @@ -21,8 +21,8 @@ * @author Haiyang Li */ public class Timed { - private final T value; private final long timeInMillis; + private final T value; private Timed(T value, long timeInMillis) { this.value = value; @@ -33,11 +33,40 @@ public static Timed of(T value, long timeInMillis) { return new Timed<>(value, timeInMillis); } + /** + * + * @return time in milliseconds. + */ + public long time() { + return timeInMillis; + } + public T value() { return value; } - public long time() { - return timeInMillis; + @Override + public int hashCode() { + return (int) (timeInMillis * 31 + (value == null ? 0 : value.hashCode())); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof Timed) { + final Timed other = (Timed) obj; + + return this.timeInMillis == other.timeInMillis && N.equals(this.value, other.value); + } + + return false; + } + + @Override + public String toString() { + return timeInMillis + ": " + N.toString(value); } } diff --git a/src/com/landawn/abacus/util/f.java b/src/com/landawn/abacus/util/f.java index a3f365ee..0fc0dc90 100644 --- a/src/com/landawn/abacus/util/f.java +++ b/src/com/landawn/abacus/util/f.java @@ -1853,108 +1853,6 @@ public static boolean[][][] zip(final boolean[][][] a, fin return result; } - public static Boolean[] box(final boolean[] a) { - return Primitives.box(a); - } - - public static Boolean[][] box(final boolean[][] a) { - if (a == null) { - return null; - } - - final int len = N.len(a); - final Boolean[][] result = new Boolean[len][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static Boolean[][][] box(final boolean[][][] a) { - if (a == null) { - return null; - } - - final int len = N.len(a); - final Boolean[][][] result = new Boolean[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static boolean[] unbox(final Boolean[] a) { - return Primitives.unbox(a); - } - - public static boolean[] unbox(final Boolean[] a, final boolean valueForNul) { - return Primitives.unbox(a, valueForNul); - } - - public static boolean[][] unbox(final Boolean[][] a) { - if (a == null) { - return null; - } - - final int len = N.len(a); - final boolean[][] result = new boolean[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static boolean[][] unbox(final Boolean[][] a, final boolean valueForNul) { - if (a == null) { - return null; - } - - final int len = N.len(a); - final boolean[][] result = new boolean[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - - public static boolean[][][] unbox(final Boolean[][][] a) { - if (a == null) { - return null; - } - - final int len = N.len(a); - final boolean[][][] result = new boolean[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static boolean[][][] unbox(final Boolean[][][] a, final boolean valueForNul) { - if (a == null) { - return null; - } - - final int len = N.len(a); - final boolean[][][] result = new boolean[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - public static void println(final boolean[] a) { if (a == null) { N.println("null"); @@ -2575,108 +2473,6 @@ public static char[][][] zip(final char[][][] a, final cha return result; } - public static Character[] box(final char[] a) { - return Primitives.box(a); - } - - public static Character[][] box(final char[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Character[][] result = new Character[len][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static Character[][][] box(final char[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Character[][][] result = new Character[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static char[] unbox(final Character[] a) { - return Primitives.unbox(a); - } - - public static char[] unbox(final Character[] a, final char valueForNul) { - return Primitives.unbox(a, valueForNul); - } - - public static char[][] unbox(final Character[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final char[][] result = new char[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static char[][] unbox(final Character[][] a, final char valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final char[][] result = new char[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - - public static char[][][] unbox(final Character[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final char[][][] result = new char[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static char[][][] unbox(final Character[][][] a, final char valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final char[][][] result = new char[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - public static void println(final char[] a) { if (a == null) { N.println("null"); @@ -4550,108 +4346,6 @@ public static byte[][][] zip(final byte[][][] a, final byt return result; } - public static Byte[] box(final byte[] a) { - return Primitives.box(a); - } - - public static Byte[][] box(final byte[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Byte[][] result = new Byte[len][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static Byte[][][] box(final byte[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Byte[][][] result = new Byte[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static byte[] unbox(final Byte[] a) { - return Primitives.unbox(a); - } - - public static byte[] unbox(final Byte[] a, final byte valueForNul) { - return Primitives.unbox(a, valueForNul); - } - - public static byte[][] unbox(final Byte[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final byte[][] result = new byte[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static byte[][] unbox(final Byte[][] a, final byte valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final byte[][] result = new byte[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - - public static byte[][][] unbox(final Byte[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final byte[][][] result = new byte[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static byte[][][] unbox(final Byte[][][] a, final byte valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final byte[][][] result = new byte[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - public static void println(final byte[] a) { if (a == null) { N.println("null"); @@ -6530,108 +6224,6 @@ public static short[][][] zip(final short[][][] a, final s return result; } - public static Short[] box(final short[] a) { - return Primitives.box(a); - } - - public static Short[][] box(final short[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Short[][] result = new Short[len][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static Short[][][] box(final short[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Short[][][] result = new Short[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static short[] unbox(final Short[] a) { - return Primitives.unbox(a); - } - - public static short[] unbox(final Short[] a, final short valueForNul) { - return Primitives.unbox(a, valueForNul); - } - - public static short[][] unbox(final Short[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final short[][] result = new short[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static short[][] unbox(final Short[][] a, final short valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final short[][] result = new short[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - - public static short[][][] unbox(final Short[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final short[][][] result = new short[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static short[][][] unbox(final Short[][][] a, final short valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final short[][][] result = new short[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - public static void println(final short[] a) { if (a == null) { N.println("null"); @@ -8488,114 +8080,12 @@ public static int[][][] zip(final int[][][] a, final int[] final int[][][] result = new int[N.max(lenA, lenB, lenC)][][]; - for (int i = 0, min = N.min(lenA, lenB, lenC); i < min; i++) { - result[i] = zip(a[i], b[i], c[i], valueForNoneA, valueForNoneB, valueForNoneC, zipFunction); - } - - for (int i = N.min(lenA, lenB, lenC), len = result.length; i < len; i++) { - result[i] = zip(i < lenA ? a[i] : null, i < lenB ? b[i] : null, i < lenC ? c[i] : null, valueForNoneA, valueForNoneB, valueForNoneC, zipFunction); - } - - return result; - } - - public static Integer[] box(final int[] a) { - return Primitives.box(a); - } - - public static Integer[][] box(final int[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Integer[][] result = new Integer[len][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static Integer[][][] box(final int[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Integer[][][] result = new Integer[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static int[] unbox(final Integer[] a) { - return Primitives.unbox(a); - } - - public static int[] unbox(final Integer[] a, final int valueForNul) { - return Primitives.unbox(a, valueForNul); - } - - public static int[][] unbox(final Integer[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final int[][] result = new int[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static int[][] unbox(final Integer[][] a, final int valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final int[][] result = new int[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - - public static int[][][] unbox(final Integer[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final int[][][] result = new int[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static int[][][] unbox(final Integer[][][] a, final int valueForNul) { - if (a == null) { - return null; + for (int i = 0, min = N.min(lenA, lenB, lenC); i < min; i++) { + result[i] = zip(a[i], b[i], c[i], valueForNoneA, valueForNoneB, valueForNoneC, zipFunction); } - final int len = a.length; - final int[][][] result = new int[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); + for (int i = N.min(lenA, lenB, lenC), len = result.length; i < len; i++) { + result[i] = zip(i < lenA ? a[i] : null, i < lenB ? b[i] : null, i < lenC ? c[i] : null, valueForNoneA, valueForNoneB, valueForNoneC, zipFunction); } return result; @@ -10474,108 +9964,6 @@ public static long[][][] zip(final long[][][] a, final lon return result; } - public static Long[] box(final long[] a) { - return Primitives.box(a); - } - - public static Long[][] box(final long[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Long[][] result = new Long[len][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static Long[][][] box(final long[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Long[][][] result = new Long[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static long[] unbox(final Long[] a) { - return Primitives.unbox(a); - } - - public static long[] unbox(final Long[] a, final long valueForNul) { - return Primitives.unbox(a, valueForNul); - } - - public static long[][] unbox(final Long[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final long[][] result = new long[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static long[][] unbox(final Long[][] a, final long valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final long[][] result = new long[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - - public static long[][][] unbox(final Long[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final long[][][] result = new long[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static long[][][] unbox(final Long[][][] a, final long valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final long[][][] result = new long[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - public static void println(final long[] a) { if (a == null) { N.println("null"); @@ -12454,108 +11842,6 @@ public static float[][][] zip(final float[][][] a, final f return result; } - public static Float[] box(final float[] a) { - return Primitives.box(a); - } - - public static Float[][] box(final float[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Float[][] result = new Float[len][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static Float[][][] box(final float[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Float[][][] result = new Float[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static float[] unbox(final Float[] a) { - return Primitives.unbox(a); - } - - public static float[] unbox(final Float[] a, final float valueForNul) { - return Primitives.unbox(a, valueForNul); - } - - public static float[][] unbox(final Float[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final float[][] result = new float[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static float[][] unbox(final Float[][] a, final float valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final float[][] result = new float[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - - public static float[][][] unbox(final Float[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final float[][][] result = new float[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static float[][][] unbox(final Float[][][] a, final float valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final float[][][] result = new float[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - public static void println(final float[] a) { if (a == null) { N.println("null"); @@ -14437,108 +13723,6 @@ public static double[][][] zip(final double[][][] a, final return result; } - public static Double[] box(final double[] a) { - return Primitives.box(a); - } - - public static Double[][] box(final double[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Double[][] result = new Double[len][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static Double[][][] box(final double[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final Double[][][] result = new Double[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = box(a[i]); - } - - return result; - } - - public static double[] unbox(final Double[] a) { - return Primitives.unbox(a); - } - - public static double[] unbox(final Double[] a, final double valueForNul) { - return Primitives.unbox(a, valueForNul); - } - - public static double[][] unbox(final Double[][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final double[][] result = new double[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static double[][] unbox(final Double[][] a, final double valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final double[][] result = new double[len][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - - public static double[][][] unbox(final Double[][][] a) { - if (a == null) { - return null; - } - - final int len = a.length; - final double[][][] result = new double[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i]); - } - - return result; - } - - public static double[][][] unbox(final Double[][][] a, final double valueForNul) { - if (a == null) { - return null; - } - - final int len = a.length; - final double[][][] result = new double[len][][]; - - for (int i = 0; i < len; i++) { - result[i] = unbox(a[i], valueForNul); - } - - return result; - } - public static void println(final double[] a) { if (a == null) { N.println("null");