Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / NumericValidator.java
blobec1f2cf969656f939252b3fb9baad990b1ddf740
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 public class NumericValidator extends ControlValidator
22 /** Creates a new instance of NumericValidator */
23 public NumericValidator( )
27 public String explainInvalid( Object Value )
29 try
31 double value = ((Double)Value).doubleValue();
32 if ( Double.compare( Double.NaN, value ) == 0 )
33 return "This is NotANumber";
34 if ( !isProperRange( value ) )
35 return "The value must be between 0 and 100";
36 if ( !isProperDigitCount( value ) )
37 return "The value must have at most one decimal digit";
39 catch( java.lang.Exception e )
41 return "This is no valid number";
43 return "";
46 public boolean isValid( Object Value )
48 try
50 double value = ((Double)Value).doubleValue();
51 if ( Double.compare( Double.NaN, value ) == 0 )
52 return false;
53 if ( !isProperRange( value ) )
54 return false;
55 if ( !isProperDigitCount( value ) )
56 return false;
57 return true;
59 catch( java.lang.Exception e )
62 return false;
65 private boolean isProperRange( double value)
67 return ( value >= 0 ) && ( value <= 100 );
70 private boolean isProperDigitCount( double value)
72 return ( Math.floor( value * 10 ) == value * 10 );