Coverage Report - com.jcabi.http.ImmutableHeader
 
Classes in this File Line Coverage Branch Coverage Complexity
ImmutableHeader
90%
18/20
39%
11/28
1.833
 
 1  
 /**
 2  
  * Copyright (c) 2011-2017, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.http;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import java.util.Map;
 34  
 import lombok.EqualsAndHashCode;
 35  
 import lombok.ToString;
 36  
 
 37  
 /**
 38  
  * Immutable HTTP header.
 39  
  *
 40  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 41  
  * @version $Id: 19a8b0ebee91325b73330a1c20dfddac827a8a61 $
 42  
  * @since 0.10
 43  
  */
 44  2608
 @Immutable
 45  0
 @ToString
 46  2
 @EqualsAndHashCode(of = { "left", "right" })
 47  
 public final class ImmutableHeader implements Map.Entry<String, String> {
 48  
 
 49  
     /**
 50  
      * Key.
 51  
      */
 52  
     private final transient String left;
 53  
 
 54  
     /**
 55  
      * Value.
 56  
      */
 57  
     private final transient String right;
 58  
 
 59  
     /**
 60  
      * Public ctor.
 61  
      * @param key The name of it
 62  
      * @param value The value
 63  
      */
 64  506
     public ImmutableHeader(final String key, final String value) {
 65  510
         this.left = ImmutableHeader.normalize(key);
 66  505
         this.right = value;
 67  503
     }
 68  
 
 69  
     @Override
 70  
     public String getKey() {
 71  1458
         return this.left;
 72  
     }
 73  
 
 74  
     @Override
 75  
     public String getValue() {
 76  1149
         return this.right;
 77  
     }
 78  
 
 79  
     @Override
 80  
     public String setValue(final String value) {
 81  0
         throw new UnsupportedOperationException("#setValue()");
 82  
     }
 83  
 
 84  
     /**
 85  
      * Normalize key.
 86  
      * @param key The key to normalize
 87  
      * @return Normalized key
 88  
      */
 89  
     public static String normalize(final String key) {
 90  1215
         final char[] chars = key.toCharArray();
 91  1215
         chars[0] = ImmutableHeader.upper(chars[0]);
 92  10759
         for (int pos = 1; pos < chars.length; ++pos) {
 93  9551
             if (chars[pos - 1] == '-') {
 94  553
                 chars[pos] = ImmutableHeader.upper(chars[pos]);
 95  
             }
 96  
         }
 97  1212
         return new String(chars);
 98  
     }
 99  
 
 100  
     /**
 101  
      * Convert char to upper case, if required.
 102  
      * @param chr The char to convert
 103  
      * @return Upper-case char
 104  
      */
 105  
     private static char upper(final char chr) {
 106  
         final char upper;
 107  1790
         if (chr >= 'a' && chr <= 'z') {
 108  1034
             upper = (char) (chr - ('a' - 'A'));
 109  
         } else {
 110  750
             upper = chr;
 111  
         }
 112  1778
         return upper;
 113  
     }
 114  
 
 115  
 }