org.eclipse.sisu.inject.Soft Maven / Gradle / Ivy
/*******************************************************************************
* 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.SoftReference;
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 SoftReference} collections.
*/
@SuppressWarnings( { "rawtypes", "unchecked" } )
public final class Soft
{
// ----------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------
private Soft()
{
// static utility class, not allowed to create instances
}
// ----------------------------------------------------------------------
// Utility methods
// ----------------------------------------------------------------------
/**
* @return {@link Collection} whose elements are kept alive with {@link SoftReference}s
*/
public static Collection elements()
{
return elements( 10 );
}
/**
* @param capacity The initial capacity
* @return {@link Collection} whose elements are kept alive with {@link SoftReference}s
*/
public static Collection elements( final int capacity )
{
return new MildElements( new ArrayList( capacity ), true );
}
/**
* @return {@link Map} whose keys are kept alive with {@link SoftReference}s
*/
public static Map keys()
{
return keys( 16 );
}
/**
* @param capacity The initial capacity
* @return {@link Map} whose keys are kept alive with {@link SoftReference}s
*/
public static Map keys( final int capacity )
{
return new MildKeys( new HashMap( capacity ), true );
}
/**
* @return {@link ConcurrentMap} whose keys are kept alive with {@link SoftReference}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 SoftReference}s
*/
public static ConcurrentMap concurrentKeys( final int capacity, final int concurrency )
{
return new MildConcurrentKeys( new ConcurrentHashMap( capacity, 0.75f, concurrency ), true );
}
/**
* @return {@link Map} whose values are kept alive with {@link SoftReference}s
*/
public static Map values()
{
return values( 16 );
}
/**
* @param capacity The initial capacity
* @return {@link Map} whose values are kept alive with {@link SoftReference}s
*/
public static Map values( final int capacity )
{
return new MildValues( new HashMap( capacity ), true );
}
/**
* @return {@link ConcurrentMap} whose values are kept alive with {@link SoftReference}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 SoftReference}s
*/
public static ConcurrentMap concurrentValues( final int capacity, final int concurrency )
{
return new MildConcurrentValues( new ConcurrentHashMap( capacity, 0.75f, concurrency ), true );
}
}