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.

ValueContext.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.data;
  17. import java.io.Serializable;
  18. import java.util.Locale;
  19. import java.util.Objects;
  20. import java.util.Optional;
  21. import com.vaadin.ui.Component;
  22. import com.vaadin.ui.UI;
  23. /**
  24. * Value context for {@code Converter}s. Contains relevant information for
  25. * converting values.
  26. *
  27. * @author Vaadin Ltd.
  28. * @since 8.0
  29. */
  30. public class ValueContext implements Serializable {
  31. private final Component component;
  32. private final Locale locale;
  33. /**
  34. * Constructor for {@code ValueContext} without a {@code Locale}.
  35. */
  36. public ValueContext() {
  37. component = null;
  38. locale = findLocale();
  39. }
  40. /**
  41. * Constructor for {@code ValueContext} without a {@code Component}.
  42. *
  43. * @param locale
  44. * The locale used with conversion. Can be null.
  45. */
  46. public ValueContext(Locale locale) {
  47. component = null;
  48. this.locale = locale;
  49. }
  50. /**
  51. * Constructor for {@code ValueContext}.
  52. *
  53. * @param component
  54. * The component related to current value. Can be null.
  55. */
  56. public ValueContext(Component component) {
  57. Objects.requireNonNull(component,
  58. "Component can't be null in ValueContext construction");
  59. this.component = component;
  60. locale = findLocale();
  61. }
  62. private Locale findLocale() {
  63. Locale l = null;
  64. if (component != null) {
  65. l = component.getLocale();
  66. }
  67. if (l == null && UI.getCurrent() != null) {
  68. l = UI.getCurrent().getLocale();
  69. }
  70. if (l == null) {
  71. l = Locale.getDefault();
  72. }
  73. return l;
  74. }
  75. /**
  76. * Returns an {@code Optional} for the {@code Component} related to value
  77. * conversion.
  78. *
  79. * @return the optional of component
  80. */
  81. public Optional<Component> getComponent() {
  82. return Optional.ofNullable(component);
  83. }
  84. /**
  85. * Returns an {@code Optional} for the {@code Locale} used in the value
  86. * conversion.
  87. *
  88. * @return the optional of locale
  89. */
  90. public Optional<Locale> getLocale() {
  91. return Optional.ofNullable(locale);
  92. }
  93. }