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.

DateToLongConverter.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2000-2014 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.data.util.converter;
  17. import java.util.Date;
  18. import java.util.Locale;
  19. /**
  20. * A converter that converts from {@link Long} to {@link Date} and back.
  21. *
  22. * @author Vaadin Ltd
  23. * @since 7.0
  24. */
  25. public class DateToLongConverter implements Converter<Date, Long> {
  26. /*
  27. * (non-Javadoc)
  28. *
  29. * @see
  30. * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
  31. * java.lang.Class, java.util.Locale)
  32. */
  33. @Override
  34. public Long convertToModel(Date value, Class<? extends Long> targetType,
  35. Locale locale) {
  36. if (value == null) {
  37. return null;
  38. }
  39. return value.getTime();
  40. }
  41. /*
  42. * (non-Javadoc)
  43. *
  44. * @see
  45. * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
  46. * .Object, java.lang.Class, java.util.Locale)
  47. */
  48. @Override
  49. public Date convertToPresentation(Long value,
  50. Class<? extends Date> targetType, Locale locale) {
  51. if (targetType != getPresentationType()) {
  52. throw new ConversionException("Converter only supports "
  53. + getPresentationType().getName() + " (targetType was "
  54. + targetType.getName() + ")");
  55. }
  56. if (value == null) {
  57. return null;
  58. }
  59. return new Date(value);
  60. }
  61. /*
  62. * (non-Javadoc)
  63. *
  64. * @see com.vaadin.data.util.converter.Converter#getModelType()
  65. */
  66. @Override
  67. public Class<Long> getModelType() {
  68. return Long.class;
  69. }
  70. /*
  71. * (non-Javadoc)
  72. *
  73. * @see com.vaadin.data.util.converter.Converter#getPresentationType()
  74. */
  75. @Override
  76. public Class<Date> getPresentationType() {
  77. return Date.class;
  78. }
  79. }