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.

ObjectProperty.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import com.vaadin.data.Property;
  6. /**
  7. * A simple data object containing one typed value. This class is a
  8. * straightforward implementation of the the {@link com.vaadin.data.Property}
  9. * interface.
  10. *
  11. * @author Vaadin Ltd.
  12. * @since 3.0
  13. */
  14. @SuppressWarnings("serial")
  15. public class ObjectProperty<T> extends AbstractProperty<T> {
  16. /**
  17. * The value contained by the Property.
  18. */
  19. private T value;
  20. /**
  21. * Data type of the Property's value.
  22. */
  23. private final Class<T> type;
  24. /**
  25. * Creates a new instance of ObjectProperty with the given value. The type
  26. * of the property is automatically initialized to be the type of the given
  27. * value.
  28. *
  29. * @param value
  30. * the Initial value of the Property.
  31. */
  32. @SuppressWarnings("unchecked")
  33. // the cast is safe, because an object of type T has class Class<T>
  34. public ObjectProperty(T value) {
  35. this(value, (Class<T>) value.getClass());
  36. }
  37. /**
  38. * Creates a new instance of ObjectProperty with the given value and type.
  39. *
  40. * Since Vaadin 7, only values of the correct type are accepted, and no
  41. * automatic conversions are performed.
  42. *
  43. * @param value
  44. * the Initial value of the Property.
  45. * @param type
  46. * the type of the value. The value must be assignable to given
  47. * type.
  48. */
  49. public ObjectProperty(T value, Class<T> type) {
  50. // Set the values
  51. this.type = type;
  52. setValue(value);
  53. }
  54. /**
  55. * Creates a new instance of ObjectProperty with the given value, type and
  56. * read-only mode status.
  57. *
  58. * Since Vaadin 7, only the correct type of values is accepted, see
  59. * {@link #ObjectProperty(Object, Class)}.
  60. *
  61. * @param value
  62. * the Initial value of the property.
  63. * @param type
  64. * the type of the value. <code>value</code> must be assignable
  65. * to this type.
  66. * @param readOnly
  67. * Sets the read-only mode.
  68. */
  69. public ObjectProperty(T value, Class<T> type, boolean readOnly) {
  70. this(value, type);
  71. setReadOnly(readOnly);
  72. }
  73. /**
  74. * Returns the type of the ObjectProperty. The methods <code>getValue</code>
  75. * and <code>setValue</code> must be compatible with this type: one must be
  76. * able to safely cast the value returned from <code>getValue</code> to the
  77. * given type and pass any variable assignable to this type as an argument
  78. * to <code>setValue</code>.
  79. *
  80. * @return type of the Property
  81. */
  82. @Override
  83. public final Class<T> getType() {
  84. return type;
  85. }
  86. /**
  87. * Gets the value stored in the Property.
  88. *
  89. * @return the value stored in the Property
  90. */
  91. @Override
  92. public T getValue() {
  93. return value;
  94. }
  95. /**
  96. * Sets the value of the property.
  97. *
  98. * Note that since Vaadin 7, no conversions are performed and the value must
  99. * be of the correct type.
  100. *
  101. * @param newValue
  102. * the New value of the property.
  103. * @throws <code>Property.ReadOnlyException</code> if the object is in
  104. * read-only mode
  105. */
  106. @Override
  107. @SuppressWarnings("unchecked")
  108. public void setValue(Object newValue) throws Property.ReadOnlyException {
  109. // Checks the mode
  110. if (isReadOnly()) {
  111. throw new Property.ReadOnlyException();
  112. }
  113. // Checks the type of the value
  114. if (newValue != null && !type.isAssignableFrom(newValue.getClass())) {
  115. throw new IllegalArgumentException("Invalid value type "
  116. + newValue.getClass().getName()
  117. + " for ObjectProperty of type " + type.getName() + ".");
  118. }
  119. // the cast is safe after an isAssignableFrom check
  120. this.value = (T) newValue;
  121. fireValueChange();
  122. }
  123. }