001: package com.etymon.pj.object;
002:
003: import java.io.*;
004:
005: /**
006: A representation of the PDF number type.
007: @author Nassib Nassar
008: */
009: public class PjNumber extends PjObject {
010:
011: /**
012: Creates a number object.
013: @param f the numeric value to initialize this object to.
014: */
015: public PjNumber(float f) {
016: _f = f;
017: }
018:
019: /**
020: Returns the floating point value of this object.
021: @return the floating point value of this object.
022: */
023: public float getFloat() {
024: return _f;
025: }
026:
027: /**
028: Returns the integer value of this object.
029: @return the integer value of this object.
030: */
031: public int getInt() {
032: return new Float(_f).intValue();
033: }
034:
035: /**
036: Returns the long value of this object.
037: @return the long value of this object.
038: */
039: public long getLong() {
040: return new Float(_f).longValue();
041: }
042:
043: /**
044: Determines whether this number is an integer.
045: @return true if this number has no fractions.
046: */
047: public boolean isInteger() {
048: return (((float) (new Float(_f).intValue())) == _f);
049: }
050:
051: /**
052: Writes this number object to a stream in PDF format.
053: @param os the stream to write to.
054: @return the number of bytes written.
055: @exception IOException if an I/O error occurs.
056: */
057: public long writePdf(OutputStream os) throws IOException {
058: Float ft = new Float(_f);
059: int x = ft.intValue();
060: if ((float) x == _f) {
061: return write(os, new Integer(x));
062: } else {
063: return write(os, ft);
064: }
065: }
066:
067: /**
068: Returns a string representation of this number in PDF format.
069: @return the string representation.
070: public String toString() {
071: Float ft = new Float(_f);
072: int x = ft.intValue();
073: if ((float)x == _f) {
074: return new Integer(x).toString();
075: } else {
076: return ft.toString();
077: }
078: }
079: */
080:
081: /**
082: Returns a deep copy of this object.
083: @return a deep copy of this object.
084: */
085: public Object clone() {
086: return this ;
087: }
088:
089: /**
090: Compares two PjNumber objects for equality.
091: @param obj the reference object to compare to.
092: @return true if this object is the same as obj, false
093: otherwise.
094: */
095: public boolean equals(Object obj) {
096: if (obj == null) {
097: return false;
098: }
099: if (obj instanceof PjNumber) {
100: return (_f == ((PjNumber) obj)._f);
101: } else {
102: return false;
103: }
104: }
105:
106: /**
107: Returns a hash code value for the object.
108: @return a hashcode value for this object.
109: */
110: public int hashCode() {
111: return new Float(_f).hashCode();
112: }
113:
114: public static final PjNumber ZERO = new PjNumber(0);
115: public static final PjNumber ONE = new PjNumber(1);
116: public static final PjNumber TWO = new PjNumber(2);
117:
118: private float _f;
119:
120: }
|