ConcurrentUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.concurrent;

  18. import java.util.concurrent.ConcurrentMap;
  19. import java.util.concurrent.ExecutionException;
  20. import java.util.concurrent.Future;
  21. import java.util.concurrent.TimeUnit;

  22. import org.apache.commons.lang3.Validate;
  23. import org.apache.commons.lang3.exception.ExceptionUtils;

  24. /**
  25.  * A utility class providing functionality related to the {@code
  26.  * java.util.concurrent} package.
  27.  *
  28.  * @since 3.0
  29.  */
  30. public class ConcurrentUtils {

  31.     /**
  32.      * A specialized {@link Future} implementation which wraps a constant value.
  33.      * @param <T> the type of the value wrapped by this class
  34.      */
  35.     static final class ConstantFuture<T> implements Future<T> {
  36.         /** The constant value. */
  37.         private final T value;

  38.         /**
  39.          * Creates a new instance of {@link ConstantFuture} and initializes it
  40.          * with the constant value.
  41.          *
  42.          * @param value the value (may be <b>null</b>)
  43.          */
  44.         ConstantFuture(final T value) {
  45.             this.value = value;
  46.         }

  47.         /**
  48.          * {@inheritDoc} The cancel operation is not supported. This
  49.          * implementation always returns <b>false</b>.
  50.          */
  51.         @Override
  52.         public boolean cancel(final boolean mayInterruptIfRunning) {
  53.             return false;
  54.         }

  55.         /**
  56.          * {@inheritDoc} This implementation just returns the constant value.
  57.          */
  58.         @Override
  59.         public T get() {
  60.             return value;
  61.         }

  62.         /**
  63.          * {@inheritDoc} This implementation just returns the constant value; it
  64.          * does not block, therefore the timeout has no meaning.
  65.          */
  66.         @Override
  67.         public T get(final long timeout, final TimeUnit unit) {
  68.             return value;
  69.         }

  70.         /**
  71.          * {@inheritDoc} This implementation always returns <b>false</b>; there
  72.          * is no background process which could be cancelled.
  73.          */
  74.         @Override
  75.         public boolean isCancelled() {
  76.             return false;
  77.         }

  78.         /**
  79.          * {@inheritDoc} This implementation always returns <b>true</b> because
  80.          * the constant object managed by this {@link Future} implementation is
  81.          * always available.
  82.          */
  83.         @Override
  84.         public boolean isDone() {
  85.             return true;
  86.         }
  87.     }

  88.     /**
  89.      * Tests whether the specified {@link Throwable} is a checked exception. If
  90.      * not, an exception is thrown.
  91.      *
  92.      * @param ex the {@link Throwable} to check
  93.      * @return a flag whether the passed in exception is a checked exception
  94.      * @throws IllegalArgumentException if the {@link Throwable} is not a
  95.      * checked exception
  96.      */
  97.     static Throwable checkedException(final Throwable ex) {
  98.         Validate.isTrue(ExceptionUtils.isChecked(ex), "Not a checked exception: " + ex);
  99.         return ex;
  100.     }

  101.     /**
  102.      * Gets an implementation of {@link Future} that is immediately done
  103.      * and returns the specified constant value.
  104.      *
  105.      * <p>
  106.      * This can be useful to return a simple constant immediately from the
  107.      * concurrent processing, perhaps as part of avoiding nulls.
  108.      * A constant future can also be useful in testing.
  109.      * </p>
  110.      *
  111.      * @param <T> the type of the value used by this {@link Future} object
  112.      * @param value  the constant value to return, may be null
  113.      * @return an instance of Future that will return the value, never null
  114.      */
  115.     public static <T> Future<T> constantFuture(final T value) {
  116.         return new ConstantFuture<>(value);
  117.     }

  118.     /**
  119.      * Checks if a concurrent map contains a key and creates a corresponding
  120.      * value if not. This method first checks the presence of the key in the
  121.      * given map. If it is already contained, its value is returned. Otherwise
  122.      * the {@code get()} method of the passed in {@link ConcurrentInitializer}
  123.      * is called. With the resulting object
  124.      * {@link #putIfAbsent(ConcurrentMap, Object, Object)} is called. This
  125.      * handles the case that in the meantime another thread has added the key to
  126.      * the map. Both the map and the initializer can be <b>null</b>; in this
  127.      * case this method simply returns <b>null</b>.
  128.      *
  129.      * @param <K> the type of the keys of the map
  130.      * @param <V> the type of the values of the map
  131.      * @param map the map to be modified
  132.      * @param key the key of the value to be added
  133.      * @param init the {@link ConcurrentInitializer} for creating the value
  134.      * @return the value stored in the map after this operation; this may or may
  135.      * not be the object created by the {@link ConcurrentInitializer}
  136.      * @throws ConcurrentException if the initializer throws an exception
  137.      */
  138.     public static <K, V> V createIfAbsent(final ConcurrentMap<K, V> map, final K key,
  139.             final ConcurrentInitializer<V> init) throws ConcurrentException {
  140.         if (map == null || init == null) {
  141.             return null;
  142.         }

  143.         final V value = map.get(key);
  144.         if (value == null) {
  145.             return putIfAbsent(map, key, init.get());
  146.         }
  147.         return value;
  148.     }

  149.     /**
  150.      * Checks if a concurrent map contains a key and creates a corresponding
  151.      * value if not, suppressing checked exceptions. This method calls
  152.      * {@code createIfAbsent()}. If a {@link ConcurrentException} is thrown, it
  153.      * is caught and re-thrown as a {@link ConcurrentRuntimeException}.
  154.      *
  155.      * @param <K> the type of the keys of the map
  156.      * @param <V> the type of the values of the map
  157.      * @param map the map to be modified
  158.      * @param key the key of the value to be added
  159.      * @param init the {@link ConcurrentInitializer} for creating the value
  160.      * @return the value stored in the map after this operation; this may or may
  161.      * not be the object created by the {@link ConcurrentInitializer}
  162.      * @throws ConcurrentRuntimeException if the initializer throws an exception
  163.      */
  164.     public static <K, V> V createIfAbsentUnchecked(final ConcurrentMap<K, V> map,
  165.             final K key, final ConcurrentInitializer<V> init) {
  166.         try {
  167.             return createIfAbsent(map, key, init);
  168.         } catch (final ConcurrentException cex) {
  169.             throw new ConcurrentRuntimeException(cex.getCause());
  170.         }
  171.     }

  172.     /**
  173.      * Inspects the cause of the specified {@link ExecutionException} and
  174.      * creates a {@link ConcurrentException} with the checked cause if
  175.      * necessary. This method performs the following checks on the cause of the
  176.      * passed in exception:
  177.      * <ul>
  178.      * <li>If the passed in exception is <b>null</b> or the cause is
  179.      * <b>null</b>, this method returns <b>null</b>.</li>
  180.      * <li>If the cause is a runtime exception, it is directly thrown.</li>
  181.      * <li>If the cause is an error, it is directly thrown, too.</li>
  182.      * <li>In any other case the cause is a checked exception. The method then
  183.      * creates a {@link ConcurrentException}, initializes it with the cause, and
  184.      * returns it.</li>
  185.      * </ul>
  186.      *
  187.      * @param ex the exception to be processed
  188.      * @return a {@link ConcurrentException} with the checked cause
  189.      */
  190.     public static ConcurrentException extractCause(final ExecutionException ex) {
  191.         if (ex == null || ex.getCause() == null) {
  192.             return null;
  193.         }
  194.         ExceptionUtils.throwUnchecked(ex.getCause());
  195.         return new ConcurrentException(ex.getMessage(), ex.getCause());
  196.     }

  197.     /**
  198.      * Inspects the cause of the specified {@link ExecutionException} and
  199.      * creates a {@link ConcurrentRuntimeException} with the checked cause if
  200.      * necessary. This method works exactly like
  201.      * {@link #extractCause(ExecutionException)}. The only difference is that
  202.      * the cause of the specified {@link ExecutionException} is extracted as a
  203.      * runtime exception. This is an alternative for client code that does not
  204.      * want to deal with checked exceptions.
  205.      *
  206.      * @param ex the exception to be processed
  207.      * @return a {@link ConcurrentRuntimeException} with the checked cause
  208.      */
  209.     public static ConcurrentRuntimeException extractCauseUnchecked(
  210.             final ExecutionException ex) {
  211.         if (ex == null || ex.getCause() == null) {
  212.             return null;
  213.         }

  214.         ExceptionUtils.throwUnchecked(ex.getCause());
  215.         return new ConcurrentRuntimeException(ex.getMessage(), ex.getCause());
  216.     }

  217.     /**
  218.      * Handles the specified {@link ExecutionException}. This method calls
  219.      * {@link #extractCause(ExecutionException)} for obtaining the cause of the
  220.      * exception - which might already cause an unchecked exception or an error
  221.      * being thrown. If the cause is a checked exception however, it is wrapped
  222.      * in a {@link ConcurrentException}, which is thrown. If the passed in
  223.      * exception is <b>null</b> or has no cause, the method simply returns
  224.      * without throwing an exception.
  225.      *
  226.      * @param ex the exception to be handled
  227.      * @throws ConcurrentException if the cause of the {@code
  228.      * ExecutionException} is a checked exception
  229.      */
  230.     public static void handleCause(final ExecutionException ex)
  231.             throws ConcurrentException {
  232.         final ConcurrentException cause = extractCause(ex);

  233.         if (cause != null) {
  234.             throw cause;
  235.         }
  236.     }

  237.     /**
  238.      * Handles the specified {@link ExecutionException} and transforms it into a
  239.      * runtime exception. This method works exactly like
  240.      * {@link #handleCause(ExecutionException)}, but instead of a
  241.      * {@link ConcurrentException} it throws a
  242.      * {@link ConcurrentRuntimeException}. This is an alternative for client
  243.      * code that does not want to deal with checked exceptions.
  244.      *
  245.      * @param ex the exception to be handled
  246.      * @throws ConcurrentRuntimeException if the cause of the {@code
  247.      * ExecutionException} is a checked exception; this exception is then
  248.      * wrapped in the thrown runtime exception
  249.      */
  250.     public static void handleCauseUnchecked(final ExecutionException ex) {
  251.         final ConcurrentRuntimeException cause = extractCauseUnchecked(ex);

  252.         if (cause != null) {
  253.             throw cause;
  254.         }
  255.     }

  256.     /**
  257.      * Invokes the specified {@link ConcurrentInitializer} and returns the
  258.      * object produced by the initializer. This method just invokes the {@code
  259.      * get()} method of the given {@link ConcurrentInitializer}. It is
  260.      * <b>null</b>-safe: if the argument is <b>null</b>, result is also
  261.      * <b>null</b>.
  262.      *
  263.      * @param <T> the type of the object produced by the initializer
  264.      * @param initializer the {@link ConcurrentInitializer} to be invoked
  265.      * @return the object managed by the {@link ConcurrentInitializer}
  266.      * @throws ConcurrentException if the {@link ConcurrentInitializer} throws
  267.      * an exception
  268.      */
  269.     public static <T> T initialize(final ConcurrentInitializer<T> initializer)
  270.             throws ConcurrentException {
  271.         return initializer != null ? initializer.get() : null;
  272.     }

  273.     /**
  274.      * Invokes the specified {@link ConcurrentInitializer} and transforms
  275.      * occurring exceptions to runtime exceptions. This method works like
  276.      * {@link #initialize(ConcurrentInitializer)}, but if the {@code
  277.      * ConcurrentInitializer} throws a {@link ConcurrentException}, it is
  278.      * caught, and the cause is wrapped in a {@link ConcurrentRuntimeException}.
  279.      * So client code does not have to deal with checked exceptions.
  280.      *
  281.      * @param <T> the type of the object produced by the initializer
  282.      * @param initializer the {@link ConcurrentInitializer} to be invoked
  283.      * @return the object managed by the {@link ConcurrentInitializer}
  284.      * @throws ConcurrentRuntimeException if the initializer throws an exception
  285.      */
  286.     public static <T> T initializeUnchecked(final ConcurrentInitializer<T> initializer) {
  287.         try {
  288.             return initialize(initializer);
  289.         } catch (final ConcurrentException cex) {
  290.             throw new ConcurrentRuntimeException(cex.getCause());
  291.         }
  292.     }

  293.     /**
  294.      * Puts a value in the specified {@link ConcurrentMap} if the key is not yet
  295.      * present. This method works similar to the {@code putIfAbsent()} method of
  296.      * the {@link ConcurrentMap} interface, but the value returned is different.
  297.      * Basically, this method is equivalent to the following code fragment:
  298.      *
  299.      * <pre>
  300.      * if (!map.containsKey(key)) {
  301.      *     map.put(key, value);
  302.      *     return value;
  303.      * } else {
  304.      *     return map.get(key);
  305.      * }
  306.      * </pre>
  307.      *
  308.      * <p>
  309.      * except that the action is performed atomically. So this method always
  310.      * returns the value which is stored in the map.
  311.      * </p>
  312.      * <p>
  313.      * This method is <b>null</b>-safe: It accepts a <b>null</b> map as input
  314.      * without throwing an exception. In this case the return value is
  315.      * <b>null</b>, too.
  316.      * </p>
  317.      *
  318.      * @param <K> the type of the keys of the map
  319.      * @param <V> the type of the values of the map
  320.      * @param map the map to be modified
  321.      * @param key the key of the value to be added
  322.      * @param value the value to be added
  323.      * @return the value stored in the map after this operation
  324.      */
  325.     public static <K, V> V putIfAbsent(final ConcurrentMap<K, V> map, final K key, final V value) {
  326.         if (map == null) {
  327.             return null;
  328.         }

  329.         final V result = map.putIfAbsent(key, value);
  330.         return result != null ? result : value;
  331.     }

  332.     /**
  333.      * Private constructor so that no instances can be created. This class
  334.      * contains only static utility methods.
  335.      */
  336.     private ConcurrentUtils() {
  337.     }

  338. }