All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.sisu.inject.Weak Maven / Gradle / Ivy

There is a newer version: 3.0.0-alpha-3
Show newest version
/*******************************************************************************
 * Copyright (c) 2010-present Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.sisu.inject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Utility methods for dealing with {@link WeakReference} collections.
 */
@SuppressWarnings( { "rawtypes", "unchecked" } )
public final class Weak
{
    // ----------------------------------------------------------------------
    // Constructors
    // ----------------------------------------------------------------------

    private Weak()
    {
        // static utility class, not allowed to create instances
    }

    // ----------------------------------------------------------------------
    // Utility methods
    // ----------------------------------------------------------------------

    /**
     * @return {@link Collection} whose elements are kept alive with {@link WeakReference}s
     */
    public static  Collection elements()
    {
        return elements( 10 );
    }

    /**
     * @param capacity The initial capacity
     * @return {@link Collection} whose elements are kept alive with {@link WeakReference}s
     */
    public static  Collection elements( final int capacity )
    {
        return new MildElements( new ArrayList( capacity ), false );
    }

    /**
     * @return {@link Map} whose keys are kept alive with {@link WeakReference}s
     */
    public static  Map keys()
    {
        return keys( 16 );
    }

    /**
     * @param capacity The initial capacity
     * @return {@link Map} whose keys are kept alive with {@link WeakReference}s
     */
    public static  Map keys( final int capacity )
    {
        return new MildKeys( new HashMap( capacity ), false );
    }

    /**
     * @return {@link ConcurrentMap} whose keys are kept alive with {@link WeakReference}s
     */
    public static  ConcurrentMap concurrentKeys()
    {
        return concurrentKeys( 16, 1 );
    }

    /**
     * @param capacity The initial capacity
     * @param concurrency The concurrency level
     * @return {@link ConcurrentMap} whose keys are kept alive with {@link WeakReference}s
     */
    public static  ConcurrentMap concurrentKeys( final int capacity, final int concurrency )
    {
        return new MildConcurrentKeys( new ConcurrentHashMap( capacity, 0.75f, concurrency ), false );
    }

    /**
     * @return {@link Map} whose values are kept alive with {@link WeakReference}s
     */
    public static  Map values()
    {
        return values( 16 );
    }

    /**
     * @param capacity The initial capacity
     * @return {@link Map} whose values are kept alive with {@link WeakReference}s
     */
    public static  Map values( final int capacity )
    {
        return new MildValues( new HashMap( capacity ), false );
    }

    /**
     * @return {@link ConcurrentMap} whose values are kept alive with {@link WeakReference}s
     */
    public static  ConcurrentMap concurrentValues()
    {
        return concurrentValues( 16, 1 );
    }

    /**
     * @param capacity The initial capacity
     * @param concurrency The concurrency level
     * @return {@link ConcurrentMap} whose values are kept alive with {@link WeakReference}s
     */
    public static  ConcurrentMap concurrentValues( final int capacity, final int concurrency )
    {
        return new MildConcurrentValues( new ConcurrentHashMap( capacity, 0.75f, concurrency ), false );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy