Coverage Report - com.damnhandy.uri.template.impl.Operator
 
Classes in this File Line Coverage Branch Coverage Complexity
Operator
96%
28/29
87%
7/8
1.667
 
 1  
 /*
 2  
  * Copyright 2012, Ryan J. McDonough
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package com.damnhandy.uri.template.impl;
 17  
 
 18  
 import static com.damnhandy.uri.template.UriTemplate.DEFAULT_SEPARATOR;
 19  
 
 20  
 import com.damnhandy.uri.template.UriTemplate.Encoding;
 21  
 
 22  
 /**
 23  
  * <p>
 24  
  * An enum representing an operator in a URI Template.
 25  
  * </p>
 26  
  * <pre>
 27  
  *  .------------------------------------------------------------------.
 28  
  *  |          NUL     +      .       /       ;      ?      &      #   |
 29  
  *  |------------------------------------------------------------------|
 30  
  *  | first |  ""     ""     "."     "/"     ";"    "?"    "&"    "#"  |
 31  
  *  | sep   |  ","    ","    "."     "/"     ";"    "&"    "&"    ","  |
 32  
  *  | named | false  false  false   false   true   true   true   false |
 33  
  *  | ifemp |  ""     ""     ""      ""      ""     "="    "="    ""   |
 34  
  *  | allow |   U     U+R     U       U       U      U      U     U+R  |
 35  
  *  `------------------------------------------------------------------'
 36  
  *  </pre>
 37  
  *
 38  
  * @author <a href="ryan@damnhandy.com">Ryan J. McDonough</a>
 39  
  * @version $Revision: 1.1 $
 40  
  */
 41  
 
 42  826
 public enum Operator {
 43  
 
 44  
 
 45  
 
 46  
 
 47  2
    NUL         ("",  DEFAULT_SEPARATOR,  false,  Encoding.U),
 48  2
    RESERVED    ("+", DEFAULT_SEPARATOR,  false,  Encoding.UR),
 49  2
    NAME_LABEL  (".", ".",                false,  Encoding.U),
 50  2
    PATH        ("/", "/",                false,  Encoding.U),
 51  2
    MATRIX      (";", ";",                true,   Encoding.U),
 52  2
    QUERY       ("?", "&",                true,   Encoding.U),
 53  2
    CONTINUATION("&", "&",                true,   Encoding.U),
 54  2
    FRAGMENT    ("#", DEFAULT_SEPARATOR,  false,  Encoding.UR);
 55  
 
 56  
 
 57  
    /**
 58  
     *
 59  
     */
 60  
    private String operator;
 61  
 
 62  
    /**
 63  
     *
 64  
     */
 65  
    private String separator;
 66  
 
 67  
    /**
 68  
     *
 69  
     */
 70  
    private boolean named;
 71  
 
 72  
    /**
 73  
     *
 74  
     */
 75  16
    private Encoding encoding = Encoding.U;
 76  
 
 77  
    /**
 78  
     *
 79  
     * Create a new Operator.
 80  
     *
 81  
     * @param operator
 82  
     * @param separator
 83  
     */
 84  
    Operator(String operator, String separator, boolean named, Encoding encoding)
 85  16
    {
 86  16
       this.operator = operator;
 87  16
       this.separator = separator;
 88  16
       this.named = named;
 89  16
       this.encoding = encoding;
 90  16
    }
 91  
 
 92  
    public String getOperator()
 93  
    {
 94  4562
       return this.operator;
 95  
    }
 96  
 
 97  
    public String getSeparator()
 98  
    {
 99  1016
       return this.separator;
 100  
    }
 101  
 
 102  
    /**
 103  
     *
 104  
     *
 105  
     * @return
 106  
     */
 107  
    public Encoding getEncoding() {
 108  1932
       return encoding;
 109  
    }
 110  
    /**
 111  
     *
 112  
     *
 113  
     * @return
 114  
     */
 115  
    public boolean isNamed()
 116  
    {
 117  1932
       return named;
 118  
    }
 119  
 
 120  
 
 121  
    /**
 122  
     */
 123  
    public String getListSeparator()
 124  
    {
 125  196
       return DEFAULT_SEPARATOR;
 126  
    }
 127  
 
 128  
    /**
 129  
     * When the variable is a Collection, this flag determines if we use
 130  
     * the VarSpec name to prefix values. For example:
 131  
     *
 132  
     * {&list} return false
 133  
     *
 134  
     * {&list*} will return true
 135  
     *
 136  
     * @return
 137  
     */
 138  
    public boolean useVarNameWhenExploded()
 139  
    {
 140  588
       return named;
 141  
    }
 142  
 
 143  
    /**
 144  
     *
 145  
     *
 146  
     * @return
 147  
     */
 148  
    public String getPrefix()
 149  
    {
 150  504
       return operator;
 151  
    }
 152  
 
 153  
    /**
 154  
     * FIXME Comment this
 155  
     *
 156  
     * @param opCode
 157  
     * @return
 158  
     */
 159  
    public static Operator fromOpCode(String opCode) throws IllegalArgumentException
 160  
    {
 161  4146
       for (Operator op : Operator.values())
 162  
       {
 163  4146
          if (op.getOperator().equalsIgnoreCase(opCode))
 164  
          {
 165  820
             return op;
 166  
          }
 167  3326
          else if ("!".equals(opCode) || "=".equals(opCode))
 168  
          {
 169  4
             throw new IllegalArgumentException(opCode + " is not a valid operator.");
 170  
          }
 171  
       }
 172  0
       return null;
 173  
    }
 174  
 }