net.sf.javagimmicks.math.sequence.NumberSequences Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gimmicks Show documentation
Show all versions of gimmicks Show documentation
Utility classes, APIs and tools for Java
package net.sf.javagimmicks.math.sequence;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* A singleton registry for {@link NumberSequence} instances.
*/
public class NumberSequences
{
private static final Map, NumberSequence>> _cache = Collections
.synchronizedMap(new HashMap, NumberSequence>>());
static
{
register(new FactorialSequence());
register(new FibonacciSequence());
}
private NumberSequences()
{}
/**
* Retrieves the {@link NumberSequence} of the given {@link Class}.
*
* @param clazz
* the {@link Class} of the {@link NumberSequence} to retrieve
* @param
* the type of {@link NumberSequence} to retrieve
* @return the resulting {@link NumberSequence} or {@code null} of there is
* no such instance cached
*/
@SuppressWarnings("unchecked")
public static > S get(final Class clazz)
{
return (S) _cache.get(clazz);
}
/**
* Registers a new {@link NumberSequence} within the cache.
*
* @param sequence
* the new {@link NumberSequence} to cache
* @param
* the type of {@link NumberSequence} to register
*/
public static > void register(final S sequence)
{
_cache.put(sequence.getClass(), sequence);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy