1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.objects.classes

File NumberClass.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

22
49
9
1
148
109
25
0.51
5.44
9
2.78

Classes

Class Line # Actions
NumberClass 36 49 0% 25 14
0.82582.5%
 

Contributing tests

This file is covered by 34 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.objects.classes;
21   
22    import org.apache.ecs.xhtml.input;
23    import org.slf4j.Logger;
24    import org.slf4j.LoggerFactory;
25   
26    import com.xpn.xwiki.XWikiContext;
27    import com.xpn.xwiki.internal.xml.XMLAttributeValueFilter;
28    import com.xpn.xwiki.objects.BaseCollection;
29    import com.xpn.xwiki.objects.BaseProperty;
30    import com.xpn.xwiki.objects.DoubleProperty;
31    import com.xpn.xwiki.objects.FloatProperty;
32    import com.xpn.xwiki.objects.IntegerProperty;
33    import com.xpn.xwiki.objects.LongProperty;
34    import com.xpn.xwiki.objects.meta.PropertyMetaClass;
35   
 
36    public class NumberClass extends PropertyClass
37    {
38    private static final String XCLASSNAME = "number";
39   
40    /** Logging helper object. */
41    private static final Logger LOG = LoggerFactory.getLogger(NumberClass.class);
42   
 
43  2794 toggle public NumberClass(PropertyMetaClass wclass)
44    {
45  2794 super(XCLASSNAME, "Number", wclass);
46  2794 setSize(30);
47  2794 setNumberType("long");
48    }
49   
 
50  953 toggle public NumberClass()
51    {
52  953 this(null);
53    }
54   
 
55  354 toggle public int getSize()
56    {
57  354 return getIntValue("size");
58    }
59   
 
60  3941 toggle public void setSize(int size)
61    {
62  3941 setIntValue("size", size);
63    }
64   
 
65  146786 toggle public String getNumberType()
66    {
67  146791 return getStringValue("numberType");
68    }
69   
 
70  4826 toggle public void setNumberType(String ntype)
71    {
72  4826 setStringValue("numberType", ntype);
73    }
74   
 
75  73405 toggle @Override
76    public BaseProperty newProperty()
77    {
78  73407 String ntype = getNumberType();
79  73412 BaseProperty property;
80  73412 if (ntype.equals("integer")) {
81  73224 property = new IntegerProperty();
82  187 } else if (ntype.equals("float")) {
83  0 property = new FloatProperty();
84  187 } else if (ntype.equals("double")) {
85  0 property = new DoubleProperty();
86    } else {
87  187 property = new LongProperty();
88    }
89  73412 property.setName(getName());
90  73412 return property;
91    }
92   
 
93  73383 toggle @Override
94    public BaseProperty fromString(String value)
95    {
96  73383 BaseProperty property = newProperty();
97  73386 String ntype = getNumberType();
98  73387 Number nvalue = null;
99   
100  73387 try {
101  73387 if (ntype.equals("integer")) {
102  73217 if ((value != null) && (!value.equals(""))) {
103  72551 nvalue = Integer.valueOf(value);
104    }
105  171 } else if (ntype.equals("float")) {
106  0 if ((value != null) && (!value.equals(""))) {
107  0 nvalue = Float.valueOf(value);
108    }
109  171 } else if (ntype.equals("double")) {
110  0 if ((value != null) && (!value.equals(""))) {
111  0 nvalue = Double.valueOf(value);
112    }
113    } else {
114  171 if ((value != null) && (!value.equals(""))) {
115  164 nvalue = Long.valueOf(value);
116    }
117    }
118    } catch (NumberFormatException e) {
119  2 LOG.warn("Invalid number entered for property " + getName() + " of class " + getObject().getName() + ": "
120    + value);
121    // Returning null makes sure that the old value (if one exists) will not be discarded/replaced
122  2 return null;
123    }
124   
125  73385 property.setValue(nvalue);
126  73386 return property;
127    }
128   
 
129  352 toggle @Override
130    public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context)
131    {
132  352 input input = new input();
133  352 input.setAttributeFilter(new XMLAttributeValueFilter());
134   
135  352 BaseProperty prop = (BaseProperty) object.safeget(name);
136  352 if (prop != null) {
137  290 input.setValue(prop.toText());
138    }
139   
140  352 input.setType("text");
141  352 input.setName(prefix + name);
142  352 input.setID(prefix + name);
143  352 input.setSize(getSize());
144  352 input.setDisabled(isDisabled());
145  352 buffer.append(input.toString());
146    }
147   
148    }