Home | History | Annotate | Download | only in header
      1 /*
      2  * Conditions Of Use
      3  *
      4  * This software was developed by employees of the National Institute of
      5  * Standards and Technology (NIST), an agency of the Federal Government.
      6  * Pursuant to title 15 Untied States Code Section 105, works of NIST
      7  * employees are not subject to copyright protection in the United States
      8  * and are considered to be in the public domain.  As a result, a formal
      9  * license is not needed to use the software.
     10  *
     11  * This software is provided by NIST as a service and is expressly
     12  * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
     13  * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
     14  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
     15  * AND DATA ACCURACY.  NIST does not warrant or make any representations
     16  * regarding the use of the software or the results thereof, including but
     17  * not limited to the correctness, accuracy, reliability or usefulness of
     18  * the software.
     19  *
     20  * Permission to use this software is contingent upon your acceptance
     21  * of the terms of this agreement
     22  *
     23  * .
     24  *
     25  */
     26 /*******************************************************************************
     27  * Product of NIST/ITL Advanced Networking Technologies Division (ANTD).        *
     28  *******************************************************************************/
     29 
     30 package gov.nist.javax.sip.header;
     31 
     32 import javax.sip.InvalidArgumentException;
     33 import javax.sip.header.*;
     34 
     35 /**
     36  * TimeStamp SIP Header.
     37  *
     38  * @version 1.2 $Revision: 1.7 $ $Date: 2009/10/18 13:46:31 $
     39  *
     40  * @author M. Ranganathan <br/>
     41  * @author Olivier Deruelle <br/>
     42  *
     43  *
     44  *
     45  */
     46 public class TimeStamp extends SIPHeader implements TimeStampHeader {
     47 
     48     /**
     49      * Comment for <code>serialVersionUID</code>
     50      */
     51     private static final long serialVersionUID = -3711322366481232720L;
     52 
     53     /**
     54      * timeStamp field
     55      */
     56     protected long timeStamp = -1;
     57 
     58     /**
     59      * delay field
     60      */
     61     protected int delay = -1;
     62 
     63     protected float delayFloat = -1;
     64 
     65     private float timeStampFloat = -1;
     66 
     67     /**
     68      * Default Constructor
     69      */
     70     public TimeStamp() {
     71         super(TIMESTAMP);
     72         delay = -1;
     73     }
     74 
     75     private String getTimeStampAsString() {
     76         if (timeStamp == -1 && timeStampFloat == -1)
     77             return "";
     78         else if (timeStamp != -1)
     79             return Long.toString(timeStamp);
     80         else
     81             return Float.toString(timeStampFloat);
     82     }
     83 
     84     private String getDelayAsString() {
     85         if (delay == -1 && delayFloat == -1)
     86             return "";
     87         else if (delay != -1)
     88             return Integer.toString(delay);
     89         else
     90             return Float.toString(delayFloat);
     91     }
     92 
     93     /**
     94      * Return canonical form of the header.
     95      *
     96      * @return String
     97      */
     98     public String encodeBody() {
     99         StringBuffer retval = new StringBuffer();
    100         String s1 = getTimeStampAsString();
    101         String s2 = getDelayAsString();
    102         if (s1.equals("") && s2.equals(""))
    103             return "";
    104         if (!s1.equals(""))
    105             retval.append(s1);
    106         if (!s2.equals(""))
    107             retval.append(" ").append(s2);
    108         return retval.toString();
    109 
    110     }
    111 
    112     /**
    113      * return true if delay exists
    114      *
    115      * @return boolean
    116      */
    117     public boolean hasDelay() {
    118         return delay != -1;
    119     }
    120 
    121     /*
    122      * remove the Delay field
    123      */
    124     public void removeDelay() {
    125         delay = -1;
    126     }
    127 
    128 
    129 
    130     public void setTimeStamp(float timeStamp) throws InvalidArgumentException {
    131         if (timeStamp < 0)
    132             throw new InvalidArgumentException(
    133                     "JAIN-SIP Exception, TimeStamp, "
    134                             + "setTimeStamp(), the timeStamp parameter is <0");
    135         this.timeStamp = -1;
    136         this.timeStampFloat = timeStamp;
    137     }
    138 
    139 
    140     public float getTimeStamp() {
    141         return this.timeStampFloat == -1 ? Float.valueOf(timeStamp).floatValue()
    142                 : this.timeStampFloat;
    143     }
    144 
    145 
    146 
    147     public float getDelay() {
    148         return delayFloat == -1 ? Float.valueOf(delay).floatValue() : delayFloat;
    149     }
    150 
    151     /**
    152      * Sets the new delay value of the TimestampHeader to the delay paramter
    153      * passed to this method
    154      *
    155      * @param delay -
    156      *            the Float.valueOf delay value
    157      * @throws InvalidArgumentException
    158      *             if the delay value argumenmt is a negative value other than
    159      *             <code>-1</code>.
    160      */
    161 
    162     public void setDelay(float delay) throws InvalidArgumentException {
    163         if (delay < 0 && delay != -1)
    164             throw new InvalidArgumentException(
    165                     "JAIN-SIP Exception, TimeStamp, "
    166                             + "setDelay(), the delay parameter is <0");
    167         this.delayFloat = delay;
    168         this.delay = -1;
    169     }
    170 
    171     public long getTime() {
    172         return this.timeStamp == -1 ? (long) timeStampFloat : timeStamp;
    173     }
    174 
    175     public int getTimeDelay() {
    176         return this.delay == -1 ? (int) delayFloat : delay;
    177 
    178     }
    179 
    180     public void setTime(long timeStamp) throws InvalidArgumentException {
    181         if (timeStamp < -1)
    182             throw new InvalidArgumentException("Illegal timestamp");
    183         this.timeStamp = timeStamp;
    184         this.timeStampFloat = -1;
    185 
    186     }
    187 
    188     public void setTimeDelay(int delay) throws InvalidArgumentException {
    189         if (delay < -1)
    190             throw new InvalidArgumentException("Value out of range " + delay);
    191         this.delay = delay;
    192         this.delayFloat = -1;
    193 
    194     }
    195 
    196 }
    197