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 package gov.nist.javax.sip.message;
27 
28 import gov.nist.javax.sip.header.*;
29 import java.util.ListIterator;
30 import java.util.NoSuchElementException;
31 
32 /**
33  * Iterator over lists of headers. Allows for uniform removal handling for singleton headers.
34  * @author M. Ranganathan
35  * @version 1.2 $Revision: 1.8 $ $Date: 2009/07/17 18:57:53 $
36  * @since 1.1
37  */
38 public class HeaderIterator implements ListIterator {
39     private boolean toRemove;
40     private int index;
41     private SIPMessage sipMessage;
42     private SIPHeader sipHeader;
43 
44     protected HeaderIterator(SIPMessage sipMessage, SIPHeader sipHeader) {
45         this.sipMessage = sipMessage;
46         this.sipHeader = sipHeader;
47     }
48 
49     public Object next() throws NoSuchElementException {
50         if (sipHeader == null || index == 1)
51             throw new NoSuchElementException();
52         toRemove = true;
53         index = 1;
54         return (Object) sipHeader;
55     }
56 
57     public Object previous() throws NoSuchElementException {
58         if (sipHeader == null || index == 0)
59             throw new NoSuchElementException();
60         toRemove = true;
61         index = 0;
62         return (Object) sipHeader;
63     }
64 
65     public int nextIndex() {
66         return 1;
67     }
68 
69     public int previousIndex() {
70         return index == 0 ? -1 : 0;
71     }
72 
73     public void set(Object header) {
74         throw new UnsupportedOperationException();
75     }
76 
77     public void add(Object header) {
78         throw new UnsupportedOperationException();
79     }
80 
81     public void remove() throws IllegalStateException {
82         if (this.sipHeader == null)
83             throw new IllegalStateException();
84         if (toRemove) {
85             this.sipHeader = null;
86             this.sipMessage.removeHeader(sipHeader.getName());
87         } else {
88             throw new IllegalStateException();
89         }
90     }
91 
92     public boolean hasNext() {
93         return index == 0;
94     }
95 
96     public boolean hasPrevious() {
97         return index == 1;
98     }
99 }
100