View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: Property.java
13   * Created: 2009-03-14
14   * Author: tmilos
15   * $Id: Property.java 3272 2009-08-28 15:13:14Z tmilos $
16   */
17  
18  package pl.edu.agh.cast.data.model.property;
19  
20  import java.io.Serializable;
21  import java.util.Arrays;
22  
23  /**
24   * Abstract property of {@link IElement} and {@link IDataSet}.
25   *
26   * @param <T> type of {@link MetaProperty) describing the property
27   *
28   * @author AGH CAST Team
29   */
30  public abstract class Property<T extends MetaProperty> implements Serializable {
31  
32  	/**
33  	 * Serial version UID.
34  	 */
35      private static final long serialVersionUID = 6713226359813402652L;
36  
37      private T metaProperty;
38  
39  	/**
40  	 * Initializes property with {@link MetaProperty} which describes it.
41  	 *
42  	 * @param metaProperty
43  	 *            meta-property describing this property
44  	 */
45  	protected Property(T metaProperty) {
46  		if (metaProperty == null) {
47  			throw new IllegalArgumentException("Meta property cannot be null"); //$NON-NLS-1$
48  		}
49  		this.metaProperty = metaProperty;
50  	}
51  
52  	/**
53  	 * Returns property value.
54  	 *
55  	 * @return the value of the property
56  	 */
57  	public abstract Object getValue();
58  
59  	/**
60  	 * Sets property value.
61  	 *
62  	 * @param value
63  	 *            value to set
64  	 * @throws PropertyException
65  	 *             if this property cannot be modified (check {@link MetaProperty#isWritable()}) or if the value type is
66  	 *             invalid for this property
67  	 */
68  	public abstract void setValue(Object value);
69  
70  	/**
71  	 * Returns {@link MetaProperty} describing this property.
72  	 *
73  	 * @return meta-property describing this property
74  	 */
75  	public T getMetaProperty() {
76  		return this.metaProperty;
77  	}
78  
79  	/**
80  	 * Checks if a value can be assigned to this property.
81  	 *
82  	 * @param value
83  	 *            the value object to check
84  	 * @return <code>true</code> if the value can be assigned to this property
85  	 */
86  	protected boolean isValidValue(Object value) {
87  		return this.getMetaProperty().getType().getValidator().isValidClass(value);
88  	}
89  
90  	/**
91  	 * {@inheritDoc}
92  	 *
93  	 * @see java.lang.Object#equals(java.lang.Object)
94  	 */
95  	@SuppressWarnings("unchecked")
96  	@Override
97  	public boolean equals(Object other) {
98  		if (other == null || !(other instanceof Property)) {
99  			return false;
100 		}
101 
102 		Property<T> otherProperty = (Property<T>)other;
103 
104 		if (!this.getMetaProperty().equals(otherProperty.getMetaProperty())) {
105 			return false;
106 		}
107 
108 		Object thisValue = null;
109 		try {
110 			thisValue = this.getValue();
111 		} catch (PropertyException e) {
112 			// do nothing
113 		}
114 
115 		Object otherValue = null;
116 		try {
117 			otherValue = otherProperty.getValue();
118 		} catch (PropertyException e) {
119 			// do nothing
120 		}
121 
122 		if (thisValue == null) {
123 			return otherValue == null;
124 		} else {
125 			if (thisValue.getClass().isArray() && otherValue.getClass().isArray()) {
126 				return Arrays.deepEquals((Object[])thisValue, (Object[])otherValue);
127 			} else {
128 				return thisValue.equals(otherValue);
129 			}
130 		}
131 	}
132 
133 	/**
134 	 * {@inheritDoc}
135 	 *
136 	 * @see java.lang.Object#hashCode()
137 	 */
138 	@Override
139 	public int hashCode() {
140 		final int prime = 31;
141 		int result = prime;
142 
143 		result = prime * result + this.getMetaProperty().hashCode();
144 		try {
145 			int valResult = 0;
146 			if (this.getValue() != null) {
147 				if (this.getValue().getClass().isArray()) {
148 					valResult = Arrays.deepHashCode((Object[])getValue());
149 				} else {
150 					valResult = this.getValue().hashCode();
151 				}
152 			}
153 
154 			result = prime * result + valResult;
155 		} catch (PropertyException e) {
156 			// do nothing
157 		}
158 		return result;
159 	}
160 
161 	/**
162 	 * {@inheritDoc}
163 	 *
164 	 * @see java.lang.Object#toString()
165 	 */
166 	@Override
167 	public String toString() {
168 		return getValue() == null ? "NULL" : getValue().toString(); //$NON-NLS-1$
169 	}
170 
171 }