Java ArrayBlockingQueue getArrayBlockingQueue(final Object o, final Class clazz, final int capacity)

Here you can find the source of getArrayBlockingQueue(final Object o, final Class clazz, final int capacity)

Description

Cast an object into a typed ArrayBlockingQueue .

License

Apache License

Parameter

Parameter Description
o The input object
clazz The output class
capacity The initial capacity
T The type of the output

Return

The casted object

Declaration

public static <T> BlockingQueue<T> getArrayBlockingQueue(final Object o, final Class<T> clazz,
        final int capacity) 

Method Source Code

//package com.java2s;
/*/* ww  w  . j a v a 2s  .  co m*/
 * #%L
 * utils-commons
 * %%
 * Copyright (C) 2016 - 2018 Gilles Landel
 * %%
 * Licensed 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.
 * #L%
 */

import java.util.Queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Main {
    /**
     * Cast an object into a typed {@link ArrayBlockingQueue}.
     * 
     * @param o
     *            The input object
     * @param clazz
     *            The output class
     * @param capacity
     *            The initial capacity
     * @return The casted object
     * @param <T>
     *            The type of the output
     */
    public static <T> BlockingQueue<T> getArrayBlockingQueue(final Object o, final Class<T> clazz,
            final int capacity) {
        final BlockingQueue<T> queue = new ArrayBlockingQueue<>(capacity);

        queue(queue, o, clazz, true);

        return queue;
    }

    /**
     * Cast the object and feel the queue.
     * 
     * @param queue
     *            The output queue
     * @param o
     *            The input object
     * @param classElement
     *            The typed class
     * @param removeNull
     *            If null has to be removed
     */
    private static <T> void queue(final Queue<T> queue, final Object o, final Class<T> clazz,
            final boolean removeNull) {
        if (o != null && Queue.class.isAssignableFrom(o.getClass())) {
            Queue<?> mObj = (Queue<?>) o;
            for (Object obj : mObj) {
                setQueueValue(queue, obj, clazz, removeNull);
            }
        }
    }

    /**
     * Get the class of the object ({@code null} safe).
     * 
     * @param object
     *            The object (required)
     * @param <T>
     *            The object type
     * @return The class of the object or null
     */
    @SuppressWarnings("unchecked")
    public static <T> Class<T> getClass(final T object) {
        if (object != null) {
            return (Class<T>) object.getClass();
        }
        return null;
    }

    /**
     * Set the queue value.
     * 
     * @param queue
     *            the queue
     * @param obj
     *            the object
     * @param clazz
     *            the class
     * @param removeNull
     *            If null has to be removed
     * @param <T>
     *            The type of the element
     */
    private static <T> void setQueueValue(final Queue<T> queue, final Object obj, final Class<T> clazz,
            final boolean removeNull) {
        if (obj != null && clazz.isAssignableFrom(obj.getClass())) {
            queue.add(clazz.cast(obj));
        } else if (obj == null && !removeNull) {
            queue.add(null);
        }
    }

    /**
     * Auto cast an object.
     * 
     * @param object
     *            The object (required)
     * @param <T>
     *            The object type
     * @return The casted object
     * @throws ClassCastException
     *             on cast failure
     */
    @SuppressWarnings("unchecked")
    public static <T> T cast(final Object object) {
        return (T) object;
    }

    /**
     * Cast an object into the specified class (null safe).
     * 
     * @param o
     *            The input object (required)
     * @param clazz
     *            The output class (required)
     * @return The casted object or {@code null}
     * @param <T>
     *            The type of the output
     */
    public static <T> T cast(final Object o, final Class<T> clazz) {
        if (o != null && clazz != null && clazz.isAssignableFrom(o.getClass())) {
            return clazz.cast(o);
        }
        return null;
    }
}

Related

  1. createUniqueHelperParamQueue(String uniqueQueueName, int size)
  2. getParameterQueueSize(String queueName)
  3. newArrayBlockingQueue()
  4. newArrayBlockingQueue(final int capacity)
  5. newArrayBlockingQueue(int capacity)