Class AbstractHeader

  • All Implemented Interfaces:
    java.lang.Iterable<Field>, Header
    Direct Known Subclasses:
    HeaderImpl

    public abstract class AbstractHeader
    extends java.lang.Object
    implements Header
    Abstract MIME header.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.lang.String,​java.util.List<Field>> fieldMap  
      private java.util.List<Field> fields  
    • Constructor Summary

      Constructors 
      Constructor Description
      AbstractHeader()
      Creates a new empty Header.
      AbstractHeader​(Header other)
      Creates a new Header from the specified Header.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addField​(Field field)
      Adds a field to the end of the list of fields.
      Field getField​(java.lang.String name)
      Gets a Field given a field name.
      <F extends Field>
      F
      getField​(java.lang.String name, java.lang.Class<F> clazz)
      Gets a Field given a field name and of the given type.
      java.util.List<Field> getFields()
      Gets the fields of this header.
      java.util.List<Field> getFields​(java.lang.String name)
      Gets all Fields having the specified field name.
      <F extends Field>
      java.util.List<F>
      getFields​(java.lang.String name, java.lang.Class<F> clazz)
      Gets all Fields having the specified field name and of the given type.
      java.util.Map<java.lang.String,​java.util.List<Field>> getFieldsAsMap()
      Gets the fields of this header.
      java.util.Iterator<Field> iterator()
      Returns an iterator over the list of fields of this header.
      int removeFields​(java.lang.String name)
      Removes all Fields having the specified field name.
      void setField​(Field field)
      Sets or replaces a field.
      java.lang.String toString()
      Return Header Object as String representation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • fields

        private final java.util.List<Field> fields
      • fieldMap

        private final java.util.Map<java.lang.String,​java.util.List<Field>> fieldMap
    • Constructor Detail

      • AbstractHeader

        public AbstractHeader()
        Creates a new empty Header.
      • AbstractHeader

        public AbstractHeader​(Header other)
        Creates a new Header from the specified Header. The Header instance is initialized with a copy of the list of Fields of the specified Header. The Field objects are not copied because they are immutable and can safely be shared between headers.
        Parameters:
        other - header to copy.
    • Method Detail

      • addField

        public void addField​(Field field)
        Adds a field to the end of the list of fields.
        Specified by:
        addField in interface Header
        Parameters:
        field - the field to add.
      • getFields

        public java.util.List<Field> getFields()
        Gets the fields of this header. The returned list will not be modifiable.
        Specified by:
        getFields in interface Header
        Returns:
        the list of Field objects.
      • getFieldsAsMap

        public java.util.Map<java.lang.String,​java.util.List<Field>> getFieldsAsMap()
        Gets the fields of this header. The returned map will not be modifiable. For each header name, values are ordered by which they appear in the underlying entity.
        Specified by:
        getFieldsAsMap in interface Header
        Returns:
        the map of Field objects indexed by names.
      • getField

        public Field getField​(java.lang.String name)
        Gets a Field given a field name. If there are multiple such fields defined in this header the first one will be returned.
        Specified by:
        getField in interface Header
        Parameters:
        name - the field name (e.g. From, Subject).
        Returns:
        the field or null if none found.
      • getField

        public <F extends Field> F getField​(java.lang.String name,
                                            java.lang.Class<F> clazz)
        Gets a Field given a field name and of the given type. If there are multiple such fields defined in this header the first one will be returned.
        Specified by:
        getField in interface Header
        Parameters:
        name - the field name (e.g. From, Subject).
        clazz - the field class.
        Returns:
        the field or null if none found.
      • getFields

        public java.util.List<Field> getFields​(java.lang.String name)
        Gets all Fields having the specified field name.
        Specified by:
        getFields in interface Header
        Parameters:
        name - the field name (e.g. From, Subject).
        Returns:
        the list of fields.
      • getFields

        public <F extends Field> java.util.List<F> getFields​(java.lang.String name,
                                                             java.lang.Class<F> clazz)
        Gets all Fields having the specified field name and of the given type.
        Specified by:
        getFields in interface Header
        Parameters:
        name - the field name (e.g. From, Subject).
        clazz - the field class.
        Returns:
        the list of fields.
      • iterator

        public java.util.Iterator<Field> iterator()
        Returns an iterator over the list of fields of this header.
        Specified by:
        iterator in interface Header
        Specified by:
        iterator in interface java.lang.Iterable<Field>
        Returns:
        an iterator.
      • removeFields

        public int removeFields​(java.lang.String name)
        Removes all Fields having the specified field name.
        Specified by:
        removeFields in interface Header
        Parameters:
        name - the field name (e.g. From, Subject).
        Returns:
        number of fields removed.
      • setField

        public void setField​(Field field)
        Sets or replaces a field. This method is useful for header fields such as Subject or Message-ID that should not occur more than once in a message. If this Header does not already contain a header field of the same name as the given field then it is added to the end of the list of fields (same behavior as addField(Field)). Otherwise the first occurrence of a field with the same name is replaced by the given field and all further occurrences are removed.
        Specified by:
        setField in interface Header
        Parameters:
        field - the field to set.
      • toString

        public java.lang.String toString()
        Return Header Object as String representation. Each headerline is seperated by "\r\n"
        Overrides:
        toString in class java.lang.Object
        Returns:
        headers