|
Java example source code file (ConcurrentUtils.java)
The ConcurrentUtils.java Java example source code/* * 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.commons.lang3.concurrent; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.apache.commons.lang3.Validate; /** * <p> * An utility class providing functionality related to the {@code * java.util.concurrent} package. * </p> * * @since 3.0 */ public class ConcurrentUtils { /** * Private constructor so that no instances can be created. This class * contains only static utility methods. */ private ConcurrentUtils() { } /** * Inspects the cause of the specified {@code ExecutionException} and * creates a {@code ConcurrentException} with the checked cause if * necessary. This method performs the following checks on the cause of the * passed in exception: * <ul> * <li>If the passed in exception is null or the cause is * <b>null, this method returns null. * <li>If the cause is a runtime exception, it is directly thrown. * <li>If the cause is an error, it is directly thrown, too. * <li>In any other case the cause is a checked exception. The method then * creates a {@link ConcurrentException}, initializes it with the cause, and * returns it.</li> * </ul> * * @param ex the exception to be processed * @return a {@code ConcurrentException} with the checked cause */ public static ConcurrentException extractCause(final ExecutionException ex) { if (ex == null || ex.getCause() == null) { return null; } throwCause(ex); return new ConcurrentException(ex.getMessage(), ex.getCause()); } /** * Inspects the cause of the specified {@code ExecutionException} and * creates a {@code ConcurrentRuntimeException} with the checked cause if * necessary. This method works exactly like * {@link #extractCause(ExecutionException)}. The only difference is that * the cause of the specified {@code ExecutionException} is extracted as a * runtime exception. This is an alternative for client code that does not * want to deal with checked exceptions. * * @param ex the exception to be processed * @return a {@code ConcurrentRuntimeException} with the checked cause */ public static ConcurrentRuntimeException extractCauseUnchecked( final ExecutionException ex) { if (ex == null || ex.getCause() == null) { return null; } throwCause(ex); return new ConcurrentRuntimeException(ex.getMessage(), ex.getCause()); } /** * Handles the specified {@code ExecutionException}. This method calls * {@link #extractCause(ExecutionException)} for obtaining the cause of the * exception - which might already cause an unchecked exception or an error * being thrown. If the cause is a checked exception however, it is wrapped * in a {@code ConcurrentException}, which is thrown. If the passed in * exception is <b>null or has no cause, the method simply returns * without throwing an exception. * * @param ex the exception to be handled * @throws ConcurrentException if the cause of the {@code * ExecutionException} is a checked exception */ public static void handleCause(final ExecutionException ex) throws ConcurrentException { final ConcurrentException cex = extractCause(ex); if (cex != null) { throw cex; } } /** * Handles the specified {@code ExecutionException} and transforms it into a * runtime exception. This method works exactly like * {@link #handleCause(ExecutionException)}, but instead of a * {@link ConcurrentException} it throws a * {@link ConcurrentRuntimeException}. This is an alternative for client * code that does not want to deal with checked exceptions. * * @param ex the exception to be handled * @throws ConcurrentRuntimeException if the cause of the {@code * ExecutionException} is a checked exception; this exception is then * wrapped in the thrown runtime exception */ public static void handleCauseUnchecked(final ExecutionException ex) { final ConcurrentRuntimeException crex = extractCauseUnchecked(ex); if (crex != null) { throw crex; } } /** * Tests whether the specified {@code Throwable} is a checked exception. If * not, an exception is thrown. * * @param ex the {@code Throwable} to check * @return a flag whether the passed in exception is a checked exception * @throws IllegalArgumentException if the {@code Throwable} is not a * checked exception */ static Throwable checkedException(final Throwable ex) { Validate.isTrue(ex != null && !(ex instanceof RuntimeException) && !(ex instanceof Error), "Not a checked exception: " + ex); return ex; } /** * Tests whether the cause of the specified {@code ExecutionException} * should be thrown and does it if necessary. * * @param ex the exception in question */ private static void throwCause(final ExecutionException ex) { if (ex.getCause() instanceof RuntimeException) { throw (RuntimeException) ex.getCause(); } if (ex.getCause() instanceof Error) { throw (Error) ex.getCause(); } } //----------------------------------------------------------------------- /** * Invokes the specified {@code ConcurrentInitializer} and returns the * object produced by the initializer. This method just invokes the {@code * get()} method of the given {@code ConcurrentInitializer}. It is * <b>null-safe: if the argument is null, result is also * <b>null. * * @param <T> the type of the object produced by the initializer * @param initializer the {@code ConcurrentInitializer} to be invoked * @return the object managed by the {@code ConcurrentInitializer} * @throws ConcurrentException if the {@code ConcurrentInitializer} throws * an exception */ public static <T> T initialize(final ConcurrentInitializer Other Java examples (source code examples)Here is a short list of links related to this Java ConcurrentUtils.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.