Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * Conditions Of Use
  *
  * This software was developed by employees of the National Institute of
  * Standards and Technology (NIST), an agency of the Federal Government.
  * Pursuant to title 15 Untied States Code Section 105, works of NIST
  * employees are not subject to copyright protection in the United States
  * and are considered to be in the public domain.  As a result, a formal
  * license is not needed to use the software.
 *
 * This software is provided by NIST as a service and is expressly
 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
 * AND DATA ACCURACY.  NIST does not warrant or make any representations
 * regarding the use of the software or the results thereof, including but
 * not limited to the correctness, accuracy, reliability or usefulness of
 * the software.
 *
 * Permission to use this software is contingent upon your acceptance
 * of the terms of this agreement
 *
 * .
 *
 */
Product of NIST/ITL Advanced Networking Technologies Division (ANTD). * /
 
 package gov.nist.javax.sip.header;
 
 
 import java.util.*;

This is the root class for all lists of SIP headers. It imbeds a SIPObjectList object and extends SIPHeader Lists of ContactSIPObjects etc. derive from this class. This supports homogeneous lists (all elements in the list are of the same class). We use this for building type homogeneous lists of SIPObjects that appear in SIPHeaders

Version:
1.2 $Revision: 1.15 $ $Date: 2005/10/09 18:47:53
 
 public abstract class SIPHeaderList<HDR extends SIPHeaderextends SIPHeader implements java.util.List<HDR>, Header {
 
     private static boolean prettyEncode = false;
    
hlist field.
 
     protected List<HDR> hlist;
 
     private Class<HDR> myClass;
 
     public String getName() {
         return this.;
     }
 
 
     private SIPHeaderList() {
          = new LinkedList<HDR>();
     }

    
Constructor

Parameters:
objclass Class to set
hname String to set
 
     protected SIPHeaderList(Class<HDR> objclassString hname) {
         this();
         this. = hname;
         this. =  objclass;
     }

    
Concatenate the list of stuff that we are keeping around and also the text corresponding to these structures (that we parsed).

Parameters:
objectToAdd
 
     public boolean add(HDR objectToAdd) {
         .add((HDR)objectToAdd);
         return true;
     }

    
Concatenate the list of stuff that we are keeping around and also the text corresponding to these structures (that we parsed).

Parameters:
obj Genericobject to set
 
    public void addFirst(HDR obj) {
        .add(0,(HDR) obj);
    }

    
Add to this list.

Parameters:
sipheader SIPHeader to add.
top is true if we want to add to the top of the list.
    public void add(HDR sipheaderboolean top) {
        if (top)
            this.addFirst(sipheader);
        else
            this.add(sipheader);
    }

    
Concatenate two compatible lists. This appends or prepends the new list to the end of this list.

Parameters:
other SIPHeaderList to set
topFlag flag which indicates which end to concatenate the lists.
Throws:
java.lang.IllegalArgumentException if the two lists are not compatible
    public void concatenate(SIPHeaderList<HDR> otherboolean topFlag)
            throws IllegalArgumentException {
        if (!topFlag) {
            this.addAll(other);
        } else {
            // add given items to the top end of the list.
            this.addAll(0, other);
        }
    }



    
Encode a list of sip headers. Headers are returned in cannonical form.

Returns:
String encoded string representation of this list of headers. (Contains string append of each encoded header).
    public String encode() {
        return encode(new StringBuffer()).toString();
    }
    public StringBuffer encode(StringBuffer buffer) {
        if (.isEmpty()) {
            buffer.append().append(':').append(.);
        }
        else {
            // The following headers do not have comma separated forms for
            // multiple headers. Thus, they must be encoded separately.
            if (this..equals(.)
                    || this..equals(.)
                    || this..equals(.)
                    || this..equals(.)
                    || ( &&
                            (this..equals(.) || this..equals(.) || this..equals(.))) // Less confusing to read
                    || this.getClass().equalsExtensionHeaderList.class) ) {
                ListIterator<HDR> li = .listIterator();
                while (li.hasNext()) {
                    HDR sipheader = (HDR) li.next();
                    sipheader.encode(buffer);
                }
            } else {
                // These can be concatenated together in an comma separated
                // list.
                buffer.append().append(.).append(.);
                this.encodeBody(buffer);
                buffer.append(.);
            }
        }
        return buffer;
    }

    
Return a list of encoded strings (one for each sipheader).

Returns:
LinkedList containing encoded strings in this header list. an empty list is returned if this header list contains no sip headers.
        List<Stringretval = new LinkedList<String>();
        ListIterator<HDR> li = .listIterator();
        while (li.hasNext()) {
            Header sipheader = li.next();
            retval.add(sipheader.toString());
        }
        return retval;
    }

    
Get the first element of this list.

Returns:
SIPHeader first element of the list.
    public Header getFirst() {
        if ( == null || .isEmpty())
            return null;
        else
            return  .get(0);
    }

    
Get the last element of this list.

Returns:
SIPHeader last element of the list.
    public Header getLast() {
        if ( == null || .isEmpty())
            return null;
        return  .get(.size() - 1);
    }

    
Get the class for the headers of this list.

Returns:
Class of header supported by this list.
    public Class<HDR> getMyClass() {
        return  this.;
    }

    
Empty check

Returns:
boolean true if list is empty
    public boolean isEmpty() {
        return .isEmpty();
    }

    
Get an initialized iterator for my imbedded list

Returns:
the generated ListIterator
    public ListIterator<HDR> listIterator() {
        return .listIterator(0);
    }

    
Get the imbedded linked list.

Returns:
the imedded linked list of SIP headers.
    public List<HDR> getHeaderList() {
        return this.;
    }

    
Get the list iterator for a given position.

Parameters:
position position for the list iterator to return
Returns:
the generated list iterator
    public ListIterator<HDR> listIterator(int position) {
        return .listIterator(position);
    }

    
Remove the first element of this list.
    public void removeFirst() {
        if (.size() != 0)
            .remove(0);
    }

    
Remove the last element of this list.
    public void removeLast() {
        if (.size() != 0)
            .remove(.size() - 1);
    }

    
Remove a sip header from this list of sip headers.

Parameters:
obj SIPHeader to remove
Returns:
boolean
    public boolean remove(HDR obj) {
        if (.size() == 0)
            return false;
        else
            return .remove(obj);
    }

    
Set the root class for all objects inserted into my list (for assertion check)

Parameters:
cl class to set
    protected void setMyClass(Class<HDR> cl) {
        this. = cl;
    }

    
convert to a string representation (for printing).

Parameters:
indentation int to set
Returns:
String string representation of object (for printing).
    public String debugDump(int indentation) {
         = "";
        String indent = new Indentation(indentation).getIndentation();
        String className = this.getClass().getName();
        sprint(indent + className);
        sprint(indent + "{");
        for (Iterator<HDR> it = .iterator(); it.hasNext();) {
            HDR sipHeader = (HDR) it.next();
            sprint(indent + sipHeader.debugDump());
        }
        sprint(indent + "}");
        return ;
    }

    
convert to a string representation

Returns:
String
    public String debugDump() {
        return debugDump(0);
    }

    
Array conversion.

Returns:
SIPHeader []
    public Object[] toArray() {
        return .toArray();
    }

    
index of an element.

Returns:
index of the given element (-1) if element does not exist.
    public int indexOf(GenericObject gobj) {
        return .indexOf(gobj);
    }

    
insert at a location.

Parameters:
index location where to add the sipHeader.
sipHeader SIPHeader structure to add.
    public void add(int index, HDR  sipHeader)
            throws IndexOutOfBoundsException {
        .add(indexsipHeader);
    }

    
Equality comparison operator.

Parameters:
other the other object to compare with. true is returned iff the classes match and list of headers herein is equal to the list of headers in the target (order of the headers is not important).
    @SuppressWarnings("unchecked")
    public boolean equals(Object other) {
        if (other == this)
            return true;
        if (other instanceof SIPHeaderList) {
            SIPHeaderList<SIPHeaderthat = (SIPHeaderList<SIPHeader>) other;
            if (this. == that.hlist)
                return true;
            else if (this. == null)
                return that.hlist == null || that.hlist.size() == 0;
            return this..equals(that.hlist);
        }
        return false;
    }

    
Template match against a template. null field in template indicates wild card match.
    public boolean match(SIPHeaderList<?> template) {
        if (template == null)
            return true;
        if (!this.getClass().equals(template.getClass()))
            return false;
        SIPHeaderList<SIPHeaderthat = (SIPHeaderList<SIPHeader>) template;
        if (this. == that.hlist)
            return true;
        else if (this. == null)
            return false;
        else {
            for (Iterator<SIPHeaderit = that.hlist.iterator(); it.hasNext();) {
                SIPHeader sipHeader = (SIPHeaderit.next();
                boolean found = false;
                for (Iterator<HDR> it1 = this..iterator(); it1.hasNext()
                        && !found;) {
                    SIPHeader sipHeader1 = (SIPHeaderit1.next();
                    found = sipHeader1.match(sipHeader);
                }
                if (!found)
                    return false;
            }
            return true;
        }
    }


    
make a clone of this header list.

Returns:
clone of this Header.
    public Object clone() {
        try {
            Class<?> clazz = this.getClass();
            Constructor<?> cons = clazz.getConstructor((Class[])null);
            SIPHeaderList<HDR> retval = (SIPHeaderList<HDR>) cons.newInstance((Object[])null);
            retval.headerName = this.;
            retval.myClass  = this.;
            return retval.clonehlist(this.);
        } catch (Exception ex) {
            throw new RuntimeException("Could not clone!"ex);
        }
    }
    protected final SIPHeaderList<HDR> clonehlist(List<HDR> hlistToClone) {
        if (hlistToClone != null) {
            for (Iterator<HDR> it = (Iterator<HDR>) hlistToClone.iterator(); it.hasNext();) {
                Header h = it.next();
                this..add((HDR)h.clone());
            }
        }
        return this;
    }

    
Get the number of headers in the list.
    public int size() {
        return .size();
    }

    
Return true if this is a header list (overrides the base class method which returns false).

Returns:
true
    public boolean isHeaderList() {
        return true;
    }

    
Encode the body of this header (the stuff that follows headerName). A.K.A headerValue. This will not give a reasonable result for WWW-Authenticate, Authorization, Proxy-Authenticate and Proxy-Authorization and hence this is protected.
    protected String encodeBody() {
        return encodeBody(new StringBuffer()).toString();
    }
    protected StringBuffer encodeBody(StringBuffer buffer) {
        ListIterator<HDR> iterator = this.listIterator();
        while (true) {
            SIPHeader sipHeader = (SIPHeaderiterator.next();
            if ( sipHeader == this ) throw new RuntimeException ("Unexpected circularity in SipHeaderList");
            sipHeader.encodeBody(buffer);
            // if (body.equals("")) System.out.println("BODY == ");
            if (iterator.hasNext()) {
                if (!this..equals(.))
                    buffer.append(.);
                else
                    buffer.append(.);
                continue;
            } else
                break;
        }
        return buffer;
    }
    public boolean addAll(Collection<? extends HDR> collection) {
        return this..addAll(collection);
    }
    public boolean addAll(int indexCollection<? extends HDR> collection) {
        return this..addAll(indexcollection);
    }
    public boolean containsAll(Collection<?> collection) {
        return this..containsAll(collection);
    }
    public void clear() {
        this..clear();
    }
    public boolean contains(Object header) {
        return this..contains(header);
    }


    
Get the object at the specified location.

Parameters:
index -- location from which to get the object.
    public HDR get(int index) {
        return  this..get(index);
    }

    
Return the index of a given object.

Parameters:
obj -- object whose index to compute.
    public int indexOf(Object obj) {
        return this..indexOf(obj);
    }

    
Return the iterator to the imbedded list.

Returns:
iterator to the imbedded list.
    public java.util.Iterator<HDR> iterator() {
        return this..listIterator();
    }

    
Get the last index of the given object.

Parameters:
obj -- object whose index to find.
    public int lastIndexOf(Object obj) {
        return this..lastIndexOf(obj);
    }

    
Remove the given object.

Parameters:
obj -- object to remove.
    public boolean remove(Object obj) {
        return this..remove(obj);
    }

    
Remove the object at a given index.

Parameters:
index -- index at which to remove the object
    public HDR remove(int index) {
        return this..remove(index);
    }

    
Remove all the elements.

    public boolean removeAll(java.util.Collection<?> collection) {
        return this..removeAll(collection);
    }


    

Parameters:
collection
See also:
java.util.List.retainAll(java.util.Collection)
    public boolean retainAll(java.util.Collection<?> collection) {
        return this..retainAll(collection);
    }

    
Get a sublist of the list.

    public java.util.List<HDR> subList(int index1int index2) {
        return this..subList(index1index2);
    }

    

Returns:
-- the computed hashcode.
See also:
java.lang.Object.hashCode()
    public int hashCode() {
        return this..hashCode();
    }

    
Set a SIPHeader at a particular position in the list.

    public HDR set(int position, HDR sipHeader) {
        return .set(positionsipHeader);
    }
    public static void setPrettyEncode(boolean flag) {
         = flag;
    }
    public <T> T[] toArray(T[] array) {
        return this..toArray(array);
    }
New to GrepCode? Check out our FAQ X