001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: */
027: package gov.nist.siplite.header;
028:
029: import gov.nist.core.*;
030:
031: /**
032: * MaxForwards Header
033: *
034: * @version JAIN-SIP-1.1
035: *
036: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
037: *
038: * IMPL_NOTE: think about removing the specific parser for MaxForwardsHeader.
039: */
040: public class MaxForwardsHeader extends ParameterLessHeader {
041:
042: /**
043: * Max forwards field.
044: */
045: protected int maxForwards;
046: /** Max forwards header field label. */
047: public static final String NAME = Header.MAX_FORWARDS;
048: /** Class handle. */
049: public final static Class clazz;
050:
051: static {
052: clazz = new MaxForwardsHeader().getClass();
053: }
054:
055: /**
056: * Default constructor.
057: */
058: public MaxForwardsHeader() {
059: super (Header.MAX_FORWARDS, "");
060: }
061:
062: /**
063: * Gets the MaxForwards field.
064: * @return the maxForwards member.
065: */
066: public int getMaxForwards() {
067: return maxForwards;
068: }
069:
070: /**
071: * Sets the maxForwards member.
072: * @param maxForwards maxForwards parameter to set
073: */
074: public void setMaxForwards(int maxForwards)
075: throws IllegalArgumentException {
076: if (maxForwards < 0 || maxForwards > 255)
077: throw new IllegalArgumentException(
078: "bad max forwards value " + maxForwards);
079: this .maxForwards = maxForwards;
080: }
081:
082: /**
083: * Encodes into a string.
084: * @return encoded string.
085: *
086: */
087: public String encodeBody() {
088: return new Integer(maxForwards).toString();
089: }
090:
091: /**
092: * Returns true if max forwards is zero.
093: * @return true if MaxForwards field reached zero.
094: */
095: public boolean hasReachedZero() {
096: return maxForwards == 0;
097: }
098:
099: /**
100: * Decrements max forwards field one by one.
101: */
102: public void decrementMaxForwards() {
103: if (maxForwards >= 0)
104: maxForwards--;
105: }
106:
107: /**
108: * Gets the max forwards header field value.
109: * @return the max forwards header field value
110: */
111: public Object getValue() {
112: return new Integer(maxForwards);
113: }
114:
115: /**
116: * Sets the header value field.
117: * @param value is the value field to set.
118: * @throws IllegalArgumentException if the value is invalid.
119: */
120: public void setHeaderValue(String value)
121: throws IllegalArgumentException {
122: int val;
123:
124: try {
125: val = Integer.parseInt(value);
126: setMaxForwards(val);
127: } catch (IllegalArgumentException iae) {
128: throw iae;
129: }
130: }
131: }
|