001
002 /*
003 * Copyright (C) 2011 Archie L. Cobbs. All rights reserved.
004 *
005 * $Id: PropertyDef.java 139 2011-10-06 22:16:40Z archie.cobbs $
006 */
007
008 package org.dellroad.stuff.vaadin;
009
010 import com.vaadin.data.Item;
011
012 /**
013 * Defines a Vaadin property, having a name, which is also the property ID, and its type.
014 */
015 public final class PropertyDef<T> {
016
017 private final String name;
018 private final Class<T> type;
019
020 public PropertyDef(String name, Class<T> type) {
021 if (name == null)
022 throw new IllegalArgumentException("null name");
023 if (type == null)
024 throw new IllegalArgumentException("null type");
025 this.name = name;
026 this.type = type;
027 }
028
029 /**
030 * Get the name of this property.
031 */
032 public String getName() {
033 return this.name;
034 }
035
036 /**
037 * Get the type of the property value that this instance represents.
038 */
039 public Class<T> getType() {
040 return this.type;
041 }
042
043 /**
044 * Read the property that this instance represents from the given {@link Item}.
045 */
046 public T read(Item item) {
047 return this.type.cast(item.getItemProperty(this.name).getValue());
048 }
049
050 @Override
051 public int hashCode() {
052 return this.name.hashCode() ^ this.type.hashCode();
053 }
054
055 @Override
056 public boolean equals(Object obj) {
057 if (!(obj instanceof PropertyDef))
058 return false;
059 PropertyDef<?> that = (PropertyDef<?>)obj;
060 return this.name.equals(that.name) && this.type == that.type;
061 }
062 }
063