/**
* PGTimestamp.java
*
* @author Created by Donald Fraser
*/
package org.postgresql.util;
import java.sql.Timestamp;
/**
* Class to catpure SQL Timestamps that inlcude negative infinity and infinity
*/
public class PGTimestamp extends Timestamp
{
boolean m_bIsInfinite = false;
public PGTimestamp(long lTime)
{
super(lTime);
}
public PGTimestamp(boolean bPostiveInfinity)
{
super(0);
setInfinite(bPostiveInfinity);
}
public void setTime(long lTime)
{
m_bIsInfinite = false;
super.setTime(lTime);
}
public void setNanos(int nanos)
{
m_bIsInfinite = false;
super.setNanos(nanos);
}
public boolean isInfinite() {return m_bIsInfinite;}
public boolean isNegativeInfinity()
{
return (m_bIsInfinite == true && getTime() == Long.MIN_VALUE);
}
public boolean isInfinity()
{
return (m_bIsInfinite == true && getTime() == Long.MAX_VALUE);
}
public void setInfinite(boolean bPostiveInfinity)
{
if (bPostiveInfinity)
{
super.setTime(Long.MAX_VALUE);
super.setNanos(999999999);
}
else
super.setTime(Long.MIN_VALUE);
m_bIsInfinite = true;
}
/**
* Tests to see if this PGTimestamp
object is
* equal to the given Timestamp
object.
* Note: This implementation tries to detect possible other
* implementations of Timestamp
that know about
* infinity. It assumes that the implementation will use the
* same maximum and minimum values for the internal storage
* of the java.util.Date
class. This can easily
* be changed if it turns out not to be the case.
*
* @param ts the Timestamp
value to compare with
* @return true
if the given Timestamp
* object is equal to this PGTimestamp
object;
* false
otherwise
*/
public boolean equals(Timestamp ts)
{
if (super.equals(ts))
{
if (m_bIsInfinite)
{
if (ts.toString().compareToIgnoreCase(toString()) != 0)
return false;
}
return true;
}
else
{
return false;
}
}
/**
* Indicates whether this PGTimestamp
object is
* earlier than the given Timestamp
object.
* Note: This implementation tries to detect possible other
* implementations of Timestamp
that know about
* infinity. It assumes that the implementation will use the
* same maximum and minimum values for the internal storage
* of the java.util.Date
class. This can easily
* be changed if it turns out not to be the case.
*
* @param ts the Timestamp
value to compare with
* @return true
if this PGTimestamp
object is
* earlier; false
otherwise
*/
public boolean before(Timestamp ts)
{
if (super.before(ts))
{
return true;
}
else
{
// look for the case when time values are equal
// but we are negative infinity and the timestamp value supplied is
// not negative infinity
if ( super.equals(ts) &&
!equals(ts) &&
m_bIsInfinite &&
getTime() == Long.MIN_VALUE )
return true;
return false;
}
}
/**
* Indicates whether this PGTimestamp
object is
* later than the given Timestamp
object.
* Note: This implementation tries to detect possible other
* implementations of Timestamp
that know about
* infinity. It assumes that the implementation will use the
* same maximum and minimum values for the internal storage
* of the java.util.Date
class. This can easily
* be changed if it turns out not to be the case.
*
* @param ts the Timestamp
value to compare with
* @return true
if this PGTimestamp
object
* is later; false
otherwise
*/
public boolean after(Timestamp ts)
{
if (super.after(ts))
{
return true;
}
else
{
// look for the case when time values are equal
// but we are infinity and the timestamp value supplied is
// not infinity
if ( super.equals(ts) &&
!equals(ts) &&
m_bIsInfinite &&
getTime() == Long.MAX_VALUE)
return true;
return false;
}
}
/**
* Compares this PGTimestamp
object to the given
* Timestamp
object.
* Note: This implementation tries to detect possible other
* implementations of Timestamp
that know about
* infinity. It assumes that the implementation will use the
* same maximum and minimum values for the internal storage
* of the java.util.Date
class. This can easily
* be changed if it turns out not to be the case.
*
* @param ts the Timestamp
object to be compared to
* this PGTimestamp
object
* @return the value 0
if the two Timestamp
* objects are equal; a value less than 0
if this
* Timestamp
object is before the given argument;
* and a value greater than 0
if this
* Timestamp
object is after the given argument.
*/
public int compareTo(Timestamp ts)
{
int i;
if (equals(ts))
i = 0;
else if (before(ts))
i = -1;
else
i = 1;
return i;
}
public String toString()
{
if (m_bIsInfinite)
{
if (getTime() == (Long.MAX_VALUE))
return "infinity";
return "-infinity";
}
else
return super.toString();
}
}