下面列出了org.apache.commons.lang3.exception.ExceptionUtils#wrapAndThrow ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void acquire() {
lock.lock();
try {
acquireCnt++;
while (!acquirable()) {
try {
doWait();
} catch (InterruptedException e) {
ExceptionUtils.wrapAndThrow(e);
}
}
onAcquired();
} finally {
acquireCnt--;
lock.unlock();
}
}
protected void onException(Throwable e) {
if (!this.exception.isInstance(e)) {
ExceptionUtils.wrapAndThrow(e);
}
if (onException != null) {
onException.accept(e);
}
}
public static void uncheckDo(RunnableWithError runnable) {
try {
runnable.run();
} catch (Throwable e) {
ExceptionUtils.wrapAndThrow(e);
}
}
public static <T, E extends Throwable> T uncheckCall(ThrowaleCallable<T, E> callable) {
try {
return callable.call();
} catch (Throwable e) {
ExceptionUtils.wrapAndThrow(e);
return null;//不会触发
}
}
public void waitCompleted() {
lock.lock();
try {
while (acquires != 0) {
try {
completed.await();
} catch (InterruptedException e) {
ExceptionUtils.wrapAndThrow(e);
}
}
} finally {
lock.unlock();
}
}
/**
* Creates a safe {@code Predicate},
*
* @param <T> the type of the input to the predicate
* @param throwablePredicate the predicate that may throw an exception
* @return the predicate result or {@code null} if exception was thrown
* @throws NullPointerException if {@code throwablePredicate} is null
*/
public static <T> Predicate<T> uncheck(
ThrowablePredicate<? super T, Throwable> throwablePredicate) {
Objects.requireNonNull(throwablePredicate);
return value -> {
try {
return throwablePredicate.test(value);
} catch (Throwable throwable) {
ExceptionUtils.wrapAndThrow(throwable);
//never
return false;
}
};
}
/**
* Creates a safe {@code BiPredicate},
*
* @param <T,U> the type of the input to the predicate
* @param throwableBiPredicate the predicate that may throw an exception
* @return the predicate result or {@code null} if exception was thrown
* @throws NullPointerException if {@code throwableBiPredicate} is null
*/
public static <T, U> BiPredicate<T, U> uncheck(
ThrowableBiPredicate<? super T, ? super U, Throwable> throwableBiPredicate) {
Objects.requireNonNull(throwableBiPredicate);
return (t, u) -> {
try {
return throwableBiPredicate.test(t, u);
} catch (Throwable throwable) {
ExceptionUtils.wrapAndThrow(throwable);
//never
return false;
}
};
}
/**
* Creates a safe {@code Supplier},
*
* @param <T> the type of the input of the supplier
* @param throwableSupplier the supplier that may throw an exception
* @return the supplier result or {@code null} if exception was thrown
* @throws NullPointerException if {@code throwableSupplier} is null
*/
public static <T> Supplier<T> uncheck(
ThrowableSupplier<? extends T, Throwable> throwableSupplier) {
Objects.requireNonNull(throwableSupplier);
return () -> {
try {
return throwableSupplier.get();
} catch (Throwable throwable) {
ExceptionUtils.wrapAndThrow(throwable);
//never
return null;
}
};
}
/**
* Creates a safe {@code Function},
*
* @param <T> the type of the input of the function
* @param <R> the type of the result of the function
* @param throwableFunction the function that may throw an exception
* @return the function result or {@code null} if exception was thrown
* @throws NullPointerException if {@code throwableFunction} is null
*/
public static <T, R> Function<T, R> uncheck(
ThrowableFunction<? super T, ? extends R, Throwable> throwableFunction) {
Objects.requireNonNull(throwableFunction);
return value -> {
try {
return throwableFunction.apply(value);
} catch (Throwable throwable) {
ExceptionUtils.wrapAndThrow(throwable);
//never
return null;
}
};
}
/**
* Creates a safe {@code BiFunction},
*
* @param <T> the type of the input of the biFunction
* @param <R> the type of the result of the biFunction
* @param throwableBiFunction the biFunction that may throw an exception
* @return the biFunction result or {@code null} if exception was thrown
* @throws NullPointerException if {@code throwableBiFunction} is null
*/
public static <T, U, R> BiFunction<T, U, R> uncheck(
ThrowableBiFunction<? super T, ? super U, ? extends R, Throwable> throwableBiFunction) {
Objects.requireNonNull(throwableBiFunction);
return (t, u) -> {
try {
return throwableBiFunction.apply(t, u);
} catch (Throwable throwable) {
ExceptionUtils.wrapAndThrow(throwable);
//never
return null;
}
};
}
/**
* Creates a safe {@code Consumer} without catch Throwable.
*
* @param <T> the type of the input to the consumer
* @param throwableConsumer the consumer that may throw an exception
* @return a {@code Consumer}
* @throws NullPointerException if {@code throwableConsumer} is null
* @see #safe(ThrowableConsumer, Consumer)
*/
public static <T> Consumer<T> uncheck(ThrowableConsumer<? super T, Throwable> throwableConsumer) {
Objects.requireNonNull(throwableConsumer);
return value -> {
try {
throwableConsumer.accept(value);
} catch (Throwable ex) {
ExceptionUtils.wrapAndThrow(ex);
}
};
}
/**
* Creates a safe {@code BiConsumer} without catch Throwable.
*
* @param <T> the type of the first argument to the operation
* @param <U> the type of the second argument to the operation
* @param throwableConsumer the consumer that may throw an exception
* @return a {@code BiConsumer}
* @throws NullPointerException if {@code throwableConsumer} is null
* @see #safe(ThrowableBiConsumer, BiConsumer)
*/
public static <T, U> BiConsumer<T, U> uncheck(ThrowableBiConsumer<? super T, ? super U, Throwable> throwableConsumer) {
Objects.requireNonNull(throwableConsumer);
return (t, u) -> {
try {
throwableConsumer.accept(t, u);
} catch (Throwable ex) {
ExceptionUtils.wrapAndThrow(ex);
}
};
}
public SequenceBuffer(Class<T> dataContainer, int bufferSize) {
numEntries = bufferSize;
entrySequence = new int[numEntries];
Arrays.fill(entrySequence, INVALID_SEQUENCE);
entryData = new Object[numEntries];
try {
for (int i = 0; i < numEntries; i++) entryData[i] = dataContainer.newInstance();
} catch (Throwable t) {
ExceptionUtils.wrapAndThrow(t);
}
sequence = 0;
}