Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ratis.util;
import org.apache.ratis.util.function.CheckedFunction;
import org.apache.ratis.util.function.CheckedRunnable;
import org.apache.ratis.util.function.CheckedSupplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* General Java utility methods.
*/
public interface JavaUtils {
Logger LOG = LoggerFactory.getLogger(JavaUtils.class);
CompletableFuture>[] EMPTY_COMPLETABLE_FUTURE_ARRAY = {};
ConcurrentMap, String> CLASS_SIMPLE_NAMES = new ConcurrentHashMap<>();
static String getClassSimpleName(Class> clazz) {
return CLASS_SIMPLE_NAMES.computeIfAbsent(clazz, Class::getSimpleName);
}
static String date() {
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS").format(new Date());
}
/**
* The same as {@link Class#cast(Object)} except that
* this method returns null (but not throw {@link ClassCastException})
* if the given object is not an instance of the given class.
*/
static T cast(Object obj, Class clazz) {
return clazz.isInstance(obj)? clazz.cast(obj): null;
}
static T cast(Object obj) {
@SuppressWarnings("unchecked")
final T t = (T)obj;
return t;
}
static StackTraceElement getCallerStackTraceElement() {
final StackTraceElement[] trace = Thread.currentThread().getStackTrace();
return trace[3];
}
static StackTraceElement getCurrentStackTraceElement() {
final StackTraceElement[] trace = Thread.currentThread().getStackTrace();
return trace[2];
}
static void runAsUnchecked(CheckedRunnable runnable) {
runAsUnchecked(runnable, RuntimeException::new);
}
static void runAsUnchecked(
CheckedRunnable runnable, Function converter) {
try {
runnable.run();
} catch(RuntimeException | Error e) {
throw e;
} catch(Throwable t) {
throw converter.apply(cast(t));
}
}
/**
* Invoke {@link Callable#call()} and, if there any,
* wrap the checked exception by {@link RuntimeException}.
*/
static T callAsUnchecked(Callable callable) {
return callAsUnchecked(callable::call, RuntimeException::new);
}
static