Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / NumericValidator.java
blob4df77af206695c4c569ba59c2d15f2728769a3fb
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 public class NumericValidator extends ControlValidator
23 /** Creates a new instance of NumericValidator */
24 public NumericValidator( )
28 public String explainInvalid( Object Value )
30 try
32 double value = ((Double)Value).doubleValue();
33 if ( Double.compare( Double.NaN, value ) == 0 )
34 return "This is NotANumber";
35 if ( !isProperRange( value ) )
36 return "The value must be between 0 and 100";
37 if ( !isProperDigitCount( value ) )
38 return "The value must have at most one decimal digit";
40 catch( java.lang.Exception e )
42 return "This is no valid number";
44 return "";
47 public boolean isValid( Object Value )
49 try
51 double value = ((Double)Value).doubleValue();
52 if ( Double.compare( Double.NaN, value ) == 0 )
53 return false;
54 if ( !isProperRange( value ) )
55 return false;
56 if ( !isProperDigitCount( value ) )
57 return false;
58 return true;
60 catch( java.lang.Exception e )
63 return false;
66 private boolean isProperRange( double value)
68 return ( value >= 0 ) && ( value <= 100 );
71 private boolean isProperDigitCount( double value)
73 return ( Math.floor( value * 10 ) == value * 10 );
77 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */