Coverage Report - org.apache.fulcrum.intake.model.ShortField
 
Classes in this File Line Coverage Branch Coverage Complexity
ShortField
29%
9/31
12%
2/16
2.833
 
 1  
 package org.apache.fulcrum.intake.model;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.fulcrum.intake.IntakeException;
 23  
 import org.apache.fulcrum.intake.validator.ShortValidator;
 24  
 
 25  
 /**
 26  
  * Processor for short fields.
 27  
  *
 28  
  * @version $Id: ShortField.java 1823858 2018-02-11 17:23:06Z tv $
 29  
  */
 30  
 public class ShortField
 31  
         extends Field<Short>
 32  
 {
 33  
     /** Serial version */
 34  
         private static final long serialVersionUID = -5838127207028024370L;
 35  
 
 36  
         /**
 37  
      * Constructor.
 38  
      *
 39  
      * @param field xml field definition object
 40  
      * @param group xml group definition object
 41  
      * @throws IntakeException thrown by superclass
 42  
      */
 43  
     public ShortField(XmlField field, Group group)
 44  
             throws IntakeException
 45  
     {
 46  2
         super(field, group);
 47  2
     }
 48  
 
 49  
     /**
 50  
      * Sets the default value for an Short Field
 51  
      *
 52  
      * @param prop Parameter for the default values
 53  
      */
 54  
     @Override
 55  
         public void setDefaultValue(String prop)
 56  
     {
 57  2
         defaultValue = null;
 58  
 
 59  2
         if (prop == null)
 60  
         {
 61  2
             return;
 62  
         }
 63  
 
 64  0
         defaultValue = Short.valueOf(prop);
 65  0
     }
 66  
 
 67  
     /**
 68  
      * Set the empty Value. This value is used if Intake
 69  
      * maps a field to a parameter returned by the user and
 70  
      * the corresponding field is either empty (empty string)
 71  
      * or non-existant.
 72  
      *
 73  
      * @param prop The value to use if the field is empty.
 74  
      */
 75  
     @Override
 76  
         public void setEmptyValue(String prop)
 77  
     {
 78  2
         emptyValue = null;
 79  
 
 80  2
         if (prop == null)
 81  
         {
 82  2
             return;
 83  
         }
 84  
 
 85  0
         emptyValue = Short.valueOf(prop);
 86  0
     }
 87  
 
 88  
     /**
 89  
      * Provides access to emptyValue such that the value returned will be
 90  
      * acceptable as an argument parameter to Method.invoke.  Subclasses
 91  
      * that deal with primitive types should ensure that they return an
 92  
      * appropriate value wrapped in the object wrapper class for the
 93  
      * primitive type.
 94  
      *
 95  
      * @return the value to use when the field is empty or an Object that
 96  
      * wraps the empty value for primitive types.
 97  
      */
 98  
     @Override
 99  
         protected Object getSafeEmptyValue()
 100  
     {
 101  0
         if (isMultiValued())
 102  
         {
 103  0
             return new short[0];
 104  
         }
 105  
         else
 106  
         {
 107  0
             return (null == getEmptyValue())
 108  0
                     ? Short.valueOf((short) 0) : getEmptyValue();
 109  
         }
 110  
     }
 111  
 
 112  
     /**
 113  
      * A suitable validator.
 114  
      *
 115  
      * @return A suitable validator
 116  
      */
 117  
     @Override
 118  
         protected String getDefaultValidator()
 119  
     {
 120  2
         return ShortValidator.class.getName();
 121  
     }
 122  
 
 123  
     /**
 124  
      * Sets the value of the field from data in the parser.
 125  
      */
 126  
     @Override
 127  
         protected void doSetValue()
 128  
     {
 129  0
         if (isMultiValued())
 130  
         {
 131  0
             Integer[] inputs = parser.getIntObjects(getKey());
 132  0
             short[] values = new short[inputs.length];
 133  
 
 134  0
             for (int i = 0; i < inputs.length; i++)
 135  
             {
 136  0
                 values[i] = inputs[i] == null
 137  0
                         ? getEmptyValue().shortValue()
 138  0
                         : inputs[i].shortValue();
 139  
             }
 140  
 
 141  0
             setTestValue(values);
 142  0
         }
 143  
         else
 144  
         {
 145  0
             Integer value = parser.getIntObject(getKey());
 146  
 
 147  0
             if (value == null)
 148  
             {
 149  0
                 setTestValue(getEmptyValue());
 150  
             }
 151  
             else
 152  
             {
 153  0
                 setTestValue(Short.valueOf(value.shortValue()));
 154  
             }
 155  
         }
 156  0
     }
 157  
 }