📄 doubletype.java
字号:
/* * Core - Library of useful classes that are used in many CyberDemia projects. * Copyright (C) 2003 CyberDemia Research and Services * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * See the COPYING file located in the top-level-directory of * the archive of this library for complete text of license. */package com.cyberdemia.value;import java.io.Serializable;/*** <p>* DoubleType is a wrapper around a primitive double. * This differs from <code>java.lang.Double</code> in that it is mutable.* </p>* <p>* It adopts a simple Javabean pattern so that this and any derived classes* can be used as a Javabean with a property named <i>value</i>. * </p>** @author Alexander Yap* @version $Revision: 1.3 $ at $Date: 2004/03/12 16:42:50 $ by $Author: alexycyap $*/public class DoubleType implements IValueType,Serializable,Cloneable, Comparable{ private double m_value = 0.0; /** * Default constructor */ public DoubleType() { // Not used } /** * Convenient constructor. */ public DoubleType( double val) { m_value = val; } /** * Sets the value as a double. This method should map to * property <i>value</i> when this object is used as a JavaBean. * * @param val Value to set. */ public void setValue( double val ) { setDoubleValue(val); } /** * Gets the value as a double. This method should map to * property <i>value</i> when this object is used as a JavaBean. * * @return Value of this object as a double. */ public double getValue() { return getDoubleValue(); } /** * Sets the value as a double. * @param val Value to set. */ public void setDoubleValue( double val ) { m_value = val; } /** * Sets the value as a float. * @param val Value to set. */ public void setFloatValue( float val ) { m_value = val; } /** * Sets the value as a long. * @param val Value to set. */ public void setLongValue( long val ) { m_value = val; } /** * Sets the value as an int. * @param val Value to set. */ public void setIntValue( int val ) { m_value = val; } /** * Sets the value as a short. * @param val Value to set. */ public void setShortValue( short val ) { m_value = val; } /** * Sets the value as a char. * @param val Value to set. */ public void setCharValue( char val ) { m_value = val; } /** * Sets the value as a byte. * @param val Value to set. */ public void setByteValue( byte val ) { m_value = val; } /** * Sets the value as a String. * @param val Value to set. */ public void setStringValue( String val ) { try { m_value = Double.parseDouble(val); } catch (NumberFormatException nfex) { m_value = Double.NaN; } } /** * Gets the value as a double. * @return Value of this object as a double. */ public double getDoubleValue() { return m_value; } /** * Gets the value as a float. * @return Value of this object as a float. */ public float getFloatValue() { return (float)m_value; } /** * Gets the value as a long. * @return Value of this object as a long. */ public long getLongValue() { return (long)m_value; } /** * Gets the value as an int. * @return Value of this object as an int. */ public int getIntValue() { return (int)m_value; } /** * Gets the value as a short. * @return Value of this object as a short. */ public short getShortValue() { return (short)m_value; } /** * Gets the value as a char. * @return Value of this object as a char. */ public char getCharValue() { return (char)m_value; } /** * Gets the value as a byte. * @return Value of this object as a byte. */ public byte getByteValue() { return (byte)m_value; } /** * Gets the value as a String. * @return Value of this object as a String. */ public String getStringValue() { return String.valueOf(m_value); } /** * Adds this object with another IValueType. * @param val2 IValueType to add. */ public void add( IValueType val2 ) { m_value += val2.getDoubleValue(); } /** * Adds this object with a double. * @param val2 double to add. */ public void add( double val2 ) { m_value += val2; } /** * Subtracts another IValueType from this object. * @param val2 IValueType to subtract. */ public void subtract( IValueType val2 ) { m_value -= val2.getDoubleValue(); } /** * Subtracts a double from this object. * @param val2 double to subtract. */ public void subtract( double val2 ) { m_value -= val2; } /** * Multiplies this object with another IValueType. * @param val2 IValueType to multiply with. */ public void multiply( IValueType val2 ) { m_value *= val2.getDoubleValue(); } /** * Multiplies this object with a Number. * @param val2 Number to multiply with. */ public void multiply( Number val2 ) { m_value *= val2.doubleValue(); } /** * Multiplies this object with a double. * @param val2 double to multiply with. */ public void multiply( double val2 ) { m_value *= val2; } /** * Divides this object with another IValueType. * @param val2 IValueType to divide. */ public void divide( IValueType val2 ) { if (Math.abs(val2.getDoubleValue()) <= Double.MIN_VALUE) m_value = Double.POSITIVE_INFINITY; else m_value /= val2.getDoubleValue(); } /** * Divides this object with a double. * @param val2 double to divide. */ public void divide( double val2 ) { if (Math.abs(val2) <= Double.MIN_VALUE) m_value = Double.POSITIVE_INFINITY; else m_value /= val2; } /** * Divides this object with a Number. * @param val2 Number to divide. */ public void divide( Number val2 ) { if (Math.abs(val2.doubleValue()) <= Double.MIN_VALUE ) m_value = Double.POSITIVE_INFINITY; else m_value /= val2.doubleValue(); } /** * Gets the value as a String. * @return Value of this object as a String. */ public String toString() { return getStringValue(); } /** * Creates a clone of this object. * @return Clone of this object. */ public Object clone() { return new DoubleType( m_value ); } /** * Compares this DoubleType with the specified IValueType object for order, * based on their stored double value. Returns a negative integer, zero, or a positive integer * if this DoubleType is less than, equal to, or greater than the specified * IValueType. * * @param dt the IValueType to be compared * @return a negative integer, zero, or a positive integer if this DoubleType is less than, equal to, or greater than the specified IValueType. * @throws ClassCastException if dt is not a IValueType */ public int compareTo(Object dt) { if (!(dt instanceof IValueType)) throw new ClassCastException("Cannot compare DoubleType with a "+dt.getClass().getName()); double dtVal = ((IValueType)dt).getDoubleValue(); if (m_value > dtVal) return 1; if (m_value < dtVal) return -1; return 0; } /** * Gets the value as a Number object. * @return Number object */ public Number getNumber() { return new Double(m_value); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -