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 package gov.nist.javax.sip.header;
30 import javax.sip.header.*;
31 import javax.sip.InvalidArgumentException;
32 
33 /**
34  * MaxForwards SIPHeader
35  *
36  * @version 1.2 $Revision: 1.8 $ $Date: 2009/07/17 18:57:32 $
37  *
38  * @author M. Ranganathan   <br/>
39  * @author Olivier Deruelle <br/>
40  *
41  */
42 public class MaxForwards extends SIPHeader implements MaxForwardsHeader {
43 
44     /**
45      * Comment for <code>serialVersionUID</code>
46      */
47     private static final long serialVersionUID = -3096874323347175943L;
48     /** maxForwards field.
49      */
50     protected int maxForwards;
51 
52     /** Default constructor.
53      */
MaxForwards()54     public MaxForwards() {
55         super(NAME);
56     }
57 
MaxForwards( int m )58   public MaxForwards( int m ) throws InvalidArgumentException {
59         super(NAME);
60         this.setMaxForwards( m );
61     }
62 
63     /** get the MaxForwards field.
64      * @return the maxForwards member.
65      */
getMaxForwards()66     public int getMaxForwards() {
67         return maxForwards;
68     }
69 
70     /**
71          * Set the maxForwards member
72          * @param maxForwards maxForwards parameter to set
73          */
setMaxForwards(int maxForwards)74     public void setMaxForwards(int maxForwards)
75         throws InvalidArgumentException {
76         if (maxForwards < 0 || maxForwards > 255)
77             throw new InvalidArgumentException(
78                 "bad max forwards value " + maxForwards);
79         this.maxForwards = maxForwards;
80     }
81 
82     /**
83          * Encode into a string.
84          * @return encoded string.
85          *
86          */
encodeBody()87     public String encodeBody() {
88         return encodeBody(new StringBuffer()).toString();
89     }
90 
encodeBody(StringBuffer buffer)91     protected StringBuffer encodeBody(StringBuffer buffer) {
92         return buffer.append(maxForwards);
93     }
94 
95     /** Boolean function
96      * @return true if MaxForwards field reached zero.
97      */
hasReachedZero()98     public boolean hasReachedZero() {
99         return maxForwards == 0;
100     }
101 
102     /** decrement MaxForwards field one by one.
103      */
decrementMaxForwards()104     public void decrementMaxForwards() throws TooManyHopsException {
105         if (maxForwards > 0)
106             maxForwards--;
107         else throw new TooManyHopsException ("has already reached 0!");
108     }
109 
equals(Object other)110     public boolean equals(Object other) {
111         if (this==other) return true;
112         if (other instanceof MaxForwardsHeader) {
113             final MaxForwardsHeader o = (MaxForwardsHeader) other;
114             return this.getMaxForwards() == o.getMaxForwards();
115         }
116         return false;
117     }
118 }
119