1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
)
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";
47 public boolean isValid( Object Value
)
51 double value
= ((Double
)Value
).doubleValue();
52 if ( Double
.compare( Double
.NaN
, value
) == 0 )
54 if ( !isProperRange( value
) )
56 if ( !isProperDigitCount( value
) )
60 catch( java
.lang
.Exception e
)
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: */