View Javadoc
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.LongValidator;
24  
25  /**
26   * Processor for long fields.
27   *
28   * @version $Id$
29   */
30  public class LongField
31          extends Field<Long>
32  {
33      /** Serial version */
34  	private static final long serialVersionUID = -7142823708600937188L;
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 LongField(XmlField field, Group group)
44              throws IntakeException
45      {
46          super(field, group);
47      }
48  
49      /**
50       * Sets the default value for an Long Field
51       *
52       * @param prop Parameter for the default values
53       */
54      @Override
55  	public void setDefaultValue(String prop)
56      {
57          defaultValue = null;
58  
59          if (prop == null)
60          {
61              return;
62          }
63  
64          defaultValue = Long.valueOf(prop);
65      }
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          emptyValue = null;
79  
80          if (prop == null)
81          {
82              return;
83          }
84  
85          emptyValue = Long.valueOf(prop);
86      }
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         if (isMultiValued())
102         {
103             return new long[0];
104         }
105         else
106         {
107             return (null == getEmptyValue()) ? Long.valueOf(0l) : getEmptyValue();
108         }
109     }
110 
111     /**
112      * A suitable validator.
113      *
114      * @return A suitable validator
115      */
116     @Override
117 	protected String getDefaultValidator()
118     {
119         return LongValidator.class.getName();
120     }
121 
122     /**
123      * Sets the value of the field from data in the parser.
124      */
125     @Override
126 	protected void doSetValue()
127     {
128         if (isMultiValued())
129         {
130             Long[] inputs = parser.getLongObjects(getKey());
131             long[] values = new long[inputs.length];
132 
133             for (int i = 0; i < inputs.length; i++)
134             {
135                 values[i] = inputs[i] == null
136                         ? getEmptyValue().longValue()
137                         : inputs[i].longValue();
138             }
139 
140             setTestValue(values);
141         }
142         else
143         {
144             setTestValue(parser.getLongObject(getKey(), getEmptyValue()));
145         }
146     }
147 }