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

net.sf.javagimmicks.math.sequence.NumberSequences Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
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);
   }
}