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 4.0KB

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