001 /* 002 * $Id: Coefficients.java 3336 2010-09-27 20:14:44Z kredel $ 003 */ 004 005 package edu.jas.ps; 006 007 008 import java.util.HashMap; 009 010 import edu.jas.structure.RingElem; 011 012 013 /** 014 * Abstract class for generating functions for coefficients of power series. Was 015 * an interface, now this class handles the caching itself. 016 * @param <C> ring element type 017 * @author Heinz Kredel 018 */ 019 020 public abstract class Coefficients<C extends RingElem<C>> { 021 022 023 /** 024 * Cache for already computed coefficients. 025 */ 026 public final HashMap<Integer, C> coeffCache; 027 028 029 /** 030 * Public no arguments constructor. 031 */ 032 public Coefficients() { 033 this(new HashMap<Integer, C>()); 034 } 035 036 037 /** 038 * Public constructor with pre-filled cache. 039 * @param cache pre-filled coefficient cache. 040 */ 041 public Coefficients(HashMap<Integer, C> cache) { 042 coeffCache = cache; 043 } 044 045 046 /** 047 * Get cached coefficient or generate coefficient. 048 * @param index of requested coefficient. 049 * @return coefficient at index. 050 */ 051 public C get(int index) { 052 if (coeffCache == null) { 053 return generate(index); 054 } 055 Integer i = index; 056 C c = coeffCache.get(i); 057 if (c != null) { 058 return c; 059 } 060 c = generate(index); 061 coeffCache.put(i, c); 062 return c; 063 } 064 065 066 /** 067 * Generate coefficient. 068 * @param index of requested coefficient. 069 * @return coefficient at index. 070 */ 071 protected abstract C generate(int index); 072 073 }