Coverage Report - com.mattunderscore.filter.contentnegotiation.parser.Header
 
Classes in this File Line Coverage Branch Coverage Complexity
Header
97%
35/36
86%
26/30
3.5
 
 1  
 /* Copyright © 2012 Matthew Champion
 2  
 All rights reserved.
 3  
 
 4  
 Redistribution and use in source and binary forms, with or without
 5  
 modification, are permitted provided that the following conditions are met:
 6  
     * Redistributions of source code must retain the above copyright
 7  
       notice, this list of conditions and the following disclaimer.
 8  
     * Redistributions in binary form must reproduce the above copyright
 9  
       notice, this list of conditions and the following disclaimer in the
 10  
       documentation and/or other materials provided with the distribution.
 11  
     * Neither the name of mattunderscore.com nor the
 12  
       names of its contributors may be used to endorse or promote products
 13  
       derived from this software without specific prior written permission.
 14  
 
 15  
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 16  
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 17  
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 18  
 DISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY
 19  
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 20  
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 21  
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 22  
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 23  
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 24  
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 25  
 
 26  
 package com.mattunderscore.filter.contentnegotiation.parser;
 27  
 
 28  
 import java.util.ArrayList;
 29  
 import java.util.List;
 30  
 
 31  
 /**
 32  
  * The description of a header returned by the content negotiation filter.
 33  
  * <P>
 34  
  * Used to configure responses.
 35  
  * 
 36  
  * @author Matt Champion
 37  
  * @since 0.0.13
 38  
  */
 39  
 public class Header
 40  
 {
 41  
     private static final int HASH_SEED = 5;
 42  
     private static final int HASH_MULT = 11;
 43  
 
 44  
     protected int code;
 45  
     protected List<HeaderPart> parts;
 46  
 
 47  
     public Header()
 48  62
     {
 49  62
     }
 50  
     
 51  
     /**
 52  
      * Get the status code of the header
 53  
      * @return The status code of the header
 54  
      * @since 0.0.13
 55  
      */
 56  
     public int getCode()
 57  
     {
 58  10
         return code;
 59  
     }
 60  
 
 61  
     /**
 62  
      * Set the status code of the header
 63  
      * @param value The status code of the header
 64  
      * @since 0.0.13
 65  
      */
 66  
     public void setCode(final int value)
 67  
     {
 68  30
         this.code = value;
 69  30
     }
 70  
 
 71  
     /**
 72  
      * Get the header parts of the header
 73  
      * @return The header parts of the header
 74  
      * @since 0.0.13
 75  
      */
 76  
     public List<HeaderPart> getPart()
 77  
     {
 78  18
         if (parts == null)
 79  
         {
 80  2
             parts = new ArrayList<HeaderPart>();
 81  
         }
 82  18
         return this.parts;
 83  
     }
 84  
 
 85  
     /**
 86  
      * Set the header parts of the header
 87  
      * @param list The header parts of the header
 88  
      * @since 0.0.13
 89  
      */
 90  
     public void setPart(final List<HeaderPart> list)
 91  
     {
 92  56
         this.parts = list;
 93  56
     }
 94  
 
 95  
     @Override
 96  
     public int hashCode()
 97  
     {
 98  104
         int hash = HASH_SEED;
 99  104
         hash = (hash * HASH_MULT) + code;
 100  104
         if (parts != null)
 101  
         {
 102  88
             for (final HeaderPart part : parts)
 103  
             {
 104  76
                 hash = (hash * HASH_MULT) + part.hashCode();
 105  76
             }
 106  
         }
 107  104
         return hash;
 108  
     }
 109  
 
 110  
     @Override
 111  
     public boolean equals(Object o)
 112  
     {
 113  158
         if (o == null)
 114  
         {
 115  20
             return false;
 116  
         }
 117  138
         else if (o == this)
 118  
         {
 119  42
             return true;
 120  
         }
 121  96
         else if (o.getClass().equals(getClass()))
 122  
         {
 123  76
             final Header co = (Header)o;
 124  76
             return equalObjects(co);
 125  
         }
 126  
         else
 127  
         {
 128  20
             return false;
 129  
         }
 130  
     }
 131  
 
 132  
     private boolean equalObjects(Header that)
 133  
     {
 134  76
         boolean partsEqual = true;
 135  76
         if (parts != null && that.parts != null)
 136  
         {
 137  64
             if (parts.size() == that.parts.size())
 138  
             {
 139  26
                 for (final HeaderPart part: parts)
 140  
                 {
 141  14
                     if (!that.parts.contains(part))
 142  
                     {
 143  4
                         return false;
 144  
                     }
 145  10
                 }
 146  
             }
 147  
             else
 148  
             {
 149  38
                 return false;
 150  
             }
 151  
         }
 152  12
         else if (parts != null || that.parts != null)
 153  
         {
 154  0
             return false;
 155  
         }
 156  34
         return this.code == that.code && partsEqual;
 157  
     }
 158  
 }