/*
 * $Id$
 */

package edu.jas.ps;


import java.io.Serializable;
import java.util.HashMap;

import edu.jas.structure.RingElem;


/**
 * Abstract class for generating functions for coefficients of power series. Was
 * an interface, now this class handles the caching itself.
 * @param  ring element type
 * @author Heinz Kredel
 */

public abstract class Coefficients> implements Serializable {


    /**
     * Cache for already computed coefficients.
     */
    public final HashMap coeffCache;


    /**
     * Public no arguments constructor.
     */
    public Coefficients() {
        this(new HashMap());
    }


    /**
     * Public constructor with pre-filled cache.
     * @param cache pre-filled coefficient cache.
     */
    public Coefficients(HashMap cache) {
        coeffCache = cache;
    }


    /**
     * Get cached coefficient or generate coefficient.
     * @param index of requested coefficient.
     * @return coefficient at index.
     */
    public C get(int index) {
        if (coeffCache == null) {
            return generate(index);
        }
        Integer i = index;
        C c = coeffCache.get(i);
        if (c != null) {
            return c;
        }
        c = generate(index);
        coeffCache.put(i, c);
        return c;
    }


    /**
     * Generate coefficient.
     * @param index of requested coefficient.
     * @return coefficient at index.
     */
    protected abstract C generate(int index);

}

 

Ads help maintain this website.