📄 integertype.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>* IntegerType is a wrapper around a primitive int.* This differs from <code>java.lang.Integer</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*/public class IntegerType implements IValueType,Serializable,Cloneable, Comparable{ private int m_value = 0; /** * Default constructor */ public IntegerType() { } /** * Convenient constructor. */ public IntegerType( int val) { m_value = val; } /** * Sets the value as an int. 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( int val ) { setIntValue(val); } /** * Gets the value as an int. This method should map to * property <i>value</i> when this object is used as a JavaBean. * * @return Value of this object as an int. */ public int getValue() { return getIntValue(); } /** * Sets the value as a double. * @param val Value to set. */ public void setDoubleValue( double val ) { m_value = (int)val; } /** * Sets the value as a float. * @param val Value to set. */ public void setFloatValue( float val ) { m_value = (int)val; } /** * Sets the value as a long. * @param val Value to set. */ public void setLongValue( long val ) { m_value = (int)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 = Integer.parseInt(val); } catch (NumberFormatException nfex) { m_value = 0; } } /** * 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 m_value; } /** * Gets the value as a long. * @return Value of this object as a long. */ public long getLongValue() { return m_value; } /** * Gets the value as an int. * @return Value of this object as an int. */ public int getIntValue() { return 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.getIntValue(); } /** * Adds this object with an int. * @param val2 int to add. */ public void add( int val2 ) { m_value += val2; } /** * Subtracts another IValueType from this object. * @param val2 IValueType to subtract. */ public void subtract( IValueType val2 ) { m_value -= val2.getIntValue(); } /** * Subtracts an int from this object. * @param val2 int to subtract. */ public void subtract( int val2 ) { m_value -= val2; } /** * Multiplies this object with another IValueType. * @param val2 IValueType to multiply with. */ public void multiply( IValueType val2 ) { m_value *= val2.getIntValue(); } /** * Multiplies this object with a Number. * @param val2 Number to multiply with. */ public void multiply( Number val2 ) { m_value *= val2.intValue(); } /** * Multiplies this object with an int. * @param val2 int to multiply with. */ public void multiply( int val2 ) { m_value *= val2; } /** * Divides this object with another IValueType. * @param val2 IValueType to divide. */ public void divide( IValueType val2 ) { if (val2.getIntValue()==0) m_value = 0; else m_value /= val2.getIntValue(); } /** * Divides this object with a Number. * @param val2 Number to divide. */ public void divide( Number val2 ) { if (val2.intValue()==0 ) m_value = 0; else m_value /= val2.intValue(); } /* * Divides this object with an int. * @param val2 int to divide. */ public void divide( int val2 ) { if (val2==0 ) m_value = 0; else m_value /= val2; } /** * 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 IntegerType( m_value ); } /** * Compares this IntegerType with the specified IValueType object for order, * based on their stored int value. Returns a negative integer, zero, or a positive integer * if this IntegerType is less than, equal to, or greater than the specified * IValueType. * * @param it the IValueType to be compared * @return a negative integer, zero, or a positive integer if this IntegerType is less than, equal to, or greater than the specified IValueType. * @throws ClassCastException if it is not a IValueType */ public int compareTo(Object it) { if (!(it instanceof IValueType)) throw new ClassCastException("Cannot compare IntegerType with a "+it.getClass().getName()); int itVal = ((IValueType)it).getIntValue(); if (m_value > itVal) return 1; if (m_value < itVal) return -1; return 0; } /** * Gets the value as a Number object. * @return Number object */ public Number getNumber() { return new Integer(m_value); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -