1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.beans;
  17. import java.io.Serializable;
  18. /**
  19. * Class to hold information and value for an individual property.
  20. * Using an object here, rather than just storing all properties in a
  21. * map keyed by property name, allows for more flexibility, and the
  22. * ability to handle indexed properties etc in a special way if necessary.
  23. *
  24. * <p>Note that the value doesn't need to be the final required type:
  25. * A BeanWrapper implementation should handle any necessary conversion, as
  26. * this object doesn't know anything about the objects it will be applied to.
  27. *
  28. * @author Rod Johnson
  29. * @since 13 May 2001
  30. * @version $Id: PropertyValue.java,v 1.5 2004/04/29 09:56:50 jhoeller Exp $
  31. * @see PropertyValues
  32. * @see BeanWrapper
  33. */
  34. public class PropertyValue implements Serializable {
  35. /** Property name */
  36. private final String name;
  37. /** Value of the property */
  38. private final Object value;
  39. /**
  40. * Create new PropertyValue.
  41. * @param name name of the property
  42. * @param value value of the property (possibly before type conversion)
  43. */
  44. public PropertyValue(String name, Object value) {
  45. if (name == null) {
  46. throw new IllegalArgumentException("Property name cannot be null");
  47. }
  48. this.name = name;
  49. this.value = value;
  50. }
  51. /**
  52. * Return the name of the property.
  53. */
  54. public String getName() {
  55. return name;
  56. }
  57. /**
  58. * Return the value of the property.
  59. * <p>Note that type conversion will <i>not</i> have occurred here.
  60. * It is the responsibility of the BeanWrapper implementation to
  61. * perform type conversion.
  62. */
  63. public Object getValue() {
  64. return value;
  65. }
  66. public String toString() {
  67. return "PropertyValue: name='" + this.name + "'; value=[" + this.value + "]";
  68. }
  69. public boolean equals(Object other) {
  70. if (this == other) {
  71. return true;
  72. }
  73. if (!(other instanceof PropertyValue)) {
  74. return false;
  75. }
  76. PropertyValue otherPv = (PropertyValue) other;
  77. return (this.name.equals(otherPv.name) &&
  78. ((this.value == null && otherPv.value == null) || this.value.equals(otherPv.value)));
  79. }
  80. public int hashCode() {
  81. return this.name.hashCode() * 29 + (this.value != null ? this.value.hashCode() : 0);
  82. }
  83. }