org.apfloat.jscience.AbstractField Maven / Gradle / Ivy
/*
* Apfloat arbitrary precision arithmetic library
* Copyright (C) 2002-2017 Mikko Tommila
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.apfloat.jscience;
import javolution.text.Text;
import org.apfloat.Apcomplex;
import org.apfloat.ApcomplexMath;
import org.jscience.mathematics.number.Number;
import org.jscience.mathematics.structure.Field;
/**
* This class represents an arbitrary precision field object.
*
* @param The type of the field.
* @param The type of the underlying value.
*
* @since 1.8.0
* @version 1.8.0
* @author Mikko Tommila
*/
public abstract class AbstractField, V extends Apcomplex>
extends Number
implements Field
{
/**
* Constructs a new field object with the specified value.
*
* @param value The value.
*/
protected AbstractField(V value)
{
if (value == null)
{
throw new NullPointerException("Value can't be null");
}
this.value = value;
}
/**
* Returns the sum of this object with the one specified.
*
* @param that The addend.
*
* @return this + that
*/
public abstract T plus(T that);
/**
* Returns the additive inverse of this object.
*
* @return -this
*/
public abstract T opposite();
/**
* Returns the product of this object with the one specified.
*
* @param that The multiplicand.
*
* @return this * that
*/
public abstract T times(T that);
/**
* Returns the multiplicative inverse of this object.
*
* @return 1 / this
*
* @exception java.lang.ArithmeticException If the divisor is zero.
*/
public abstract T inverse()
throws ArithmeticException;
/**
* Returns a copy of this object.
*
* @return A copy of this object.
*/
@Override
public abstract T copy();
/**
* Compares the absolute value of this number
* with the absolute value of the number specified.
*
* @param that The number to be compared with.
*
* @return |this| > |that|
*/
@Override
public boolean isLargerThan(T that)
{
return ApcomplexMath.abs(value()).compareTo(ApcomplexMath.abs(that.value())) > 0;
}
/**
* Returns the value of this number as the underlying type.
*
* @return The value.
*/
public V value()
{
return this.value;
}
/**
* Returns the value of this number as a double
.
*
* @return The value.
*/
@Override
public double doubleValue()
{
return value().doubleValue();
}
/**
* Returns the value of this number as a long
.
*
* @return The value.
*/
@Override
public long longValue()
{
return value().longValue();
}
/**
* Returns the text representation of this number.
*
* @return The string representation of this number as a Text
.
*/
@Override
public Text toText()
{
return Text.valueOf(value().toString());
}
/**
* Compares this number to another number.
*
* @param that The number to be compared with.
*
* @return -1, 0, or 1 depending on the ordering.
*/
@Override
public int compareTo(T that)
{
int result = value().real().compareTo(that.value().real());
if (result == 0)
{
result = value().imag().compareTo(that.value().imag());
}
return result;
}
/**
* Returns the hash code for this number.
*
* @return The hash code value.
*/
@Override
public int hashCode()
{
return value().hashCode();
}
/**
* Compares for equality.
*
* @return If the objects are equal.
*/
@Override
public boolean equals(Object obj)
{
if (obj instanceof AbstractField)
{
AbstractField, ?> that = (AbstractField, ?>) obj;
return value().equals(that.value());
}
return false;
}
private static final long serialVersionUID = -7725271295007354895L;
private V value;
}