001/*
002 * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
003 * Copyright (C) 2006 - JScience (http://jscience.org/)
004 * All rights reserved.
005 * 
006 * Permission to use, copy, modify, and distribute this software is
007 * freely granted, provided that this notice is preserved.
008 */
009package org.jscience.mathematics.function;
010
011import javolution.context.ObjectFactory;
012
013import org.jscience.mathematics.structure.Ring;
014
015/**
016 * <p> This class represents a constant function (polynomial of degree 0).<p>
017 * 
018 * @author  <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
019 * @version 3.1, April 1, 2006
020 */
021public final class Constant<R extends Ring<R>> extends Polynomial<R> {
022
023    /**
024     * Default constructor.
025     */
026    private Constant() {
027    }
028
029    /**
030     * Returns a constant function of specified value.
031     * 
032     * @param value the value returned by this function.
033     * @return the corresponding constant function.
034     */
035    @SuppressWarnings("unchecked")
036    public static <R extends Ring<R>> Constant<R> valueOf(R value) {
037        Constant<R> cst = FACTORY.object();
038        cst._termToCoef.put(Term.ONE, value);
039        return cst;
040    }
041
042    @SuppressWarnings("unchecked")
043    private static final ObjectFactory<Constant> FACTORY = new ObjectFactory<Constant>() {
044        protected Constant create() {
045            return new Constant();
046        }
047
048        protected void cleanup(Constant cst) {
049            cst._termToCoef.reset();
050        }
051    };
052
053    /**
054     * Returns the constant value for this function.
055     *
056     * @return <code>getCoefficient(Term.CONSTANT)</code>
057     */
058    public R getValue() {
059        return getCoefficient(Term.ONE);
060    }
061
062    private static final long serialVersionUID = 1L;
063}