You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DoubleValidator.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.data.validator;
  17. import com.vaadin.v7.data.Property;
  18. import com.vaadin.v7.data.util.converter.StringToDoubleConverter;
  19. /**
  20. * String validator for a double precision floating point number. See
  21. * {@link com.vaadin.v7.data.validator.AbstractStringValidator} for more
  22. * information.
  23. *
  24. * @author Vaadin Ltd.
  25. * @since 5.4
  26. * @deprecated As of 7.0. Use a {@link StringToDoubleConverter} converter on the
  27. * field instead or bind the field to a {@link Property} of type
  28. * {@link Double}.
  29. */
  30. @Deprecated
  31. @SuppressWarnings("serial")
  32. public class DoubleValidator extends AbstractStringValidator {
  33. /**
  34. * Creates a validator for checking that a string can be parsed as an
  35. * double.
  36. *
  37. * @param errorMessage
  38. * the message to display in case the value does not validate.
  39. * @deprecated As of 7.0. Use a Double converter on the field instead and/or
  40. * use a {@link DoubleRangeValidator} for validating that the
  41. * value is inside a given range.
  42. */
  43. @Deprecated
  44. public DoubleValidator(String errorMessage) {
  45. super(errorMessage);
  46. }
  47. @Override
  48. protected boolean isValidValue(String value) {
  49. try {
  50. Double.parseDouble(value);
  51. return true;
  52. } catch (Exception e) {
  53. return false;
  54. }
  55. }
  56. @Override
  57. public void validate(Object value) throws InvalidValueException {
  58. if (value != null && value instanceof Double) {
  59. // Allow Doubles to pass through the validator for easier
  60. // migration. Otherwise a TextField connected to an double property
  61. // with a DoubleValidator will fail.
  62. return;
  63. }
  64. super.validate(value);
  65. }
  66. }