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: AttributeValue.java
13   * Created: 2007-00-00
14   * Author: apohllo, awos
15   * $Id: AttributeValue.java 2467 2009-02-05 11:04:08Z kpietak $
16   */
17  
18  package pl.edu.agh.cast.model.attributes;
19  
20  import java.io.Serializable;
21  
22  import org.eclipse.osgi.util.NLS;
23  
24  import pl.edu.agh.cast.util.Messages;
25  
26  import com.thoughtworks.xstream.annotations.XStreamAlias;
27  
28  /**
29   * Class representing a single value of an {@link Attribute}.
30   *
31   * @author AGH CAST Team
32   */
33  @XStreamAlias("attributeValue")
34  public final class AttributeValue implements Cloneable, Serializable {
35  
36  	private static final long serialVersionUID = 3043241931570536587L;
37  
38  	@XStreamAlias("attribute")
39  	private Attribute attribute;
40  
41  	@XStreamAlias("value")
42  	private Object value;
43  
44  	/**
45  	 * Constructor.
46  	 *
47  	 * @param attribute the attribute
48  	 * @param value the value
49  	 */
50  	public AttributeValue(Attribute attribute, Object value) {
51  		super();
52  		if (!attribute.validateValue(value)) {
53  			throw new IllegalArgumentException("Value " + value //$NON-NLS-1$
54  			        + " is not valid class for type " + attribute.getType()); //$NON-NLS-1$
55  		}
56  
57  		this.attribute = attribute;
58  		this.value = value;
59  	}
60  
61  	/**
62  	 * Returns a copy of this {@link AttributeValue} instance.
63  	 *
64  	 * @return copy of this instance
65  	 */
66  	public AttributeValue copy() {
67  		return new AttributeValue(attribute, value);
68  	}
69  
70  	public Object getValue() {
71  		return value;
72  	}
73  
74  	public Attribute getAttribute() {
75  		return attribute;
76  	}
77  
78  	/**
79  	 * {@inheritDoc}
80  	 * @see java.lang.Object#equals(java.lang.Object)
81  	 */
82  	@Override
83  	public boolean equals(Object obj) {
84  		if (obj == this) {
85  			return true;
86  		}
87  		if (!(obj instanceof AttributeValue)) {
88  			return false;
89  		}
90  		AttributeValue other = (AttributeValue)obj;
91  		if (!this.attribute.equals(other.attribute)) {
92  			return false;
93  		}
94  		if (this.value == null) {
95  			if (other.value != null) {
96  				return false;
97  			}
98  		} else if (!this.value.equals(other.value)) {
99  			return false;
100 		}
101 		return true;
102 	}
103 
104 	/**
105 	 * {@inheritDoc}
106 	 * @see java.lang.Object#hashCode()
107 	 */
108 	@Override
109 	public int hashCode() {
110 		if (this.value == null) {
111 			return attribute.hashCode();
112 		} else {
113 			return attribute.hashCode() ^ value.hashCode();
114 		}
115 	}
116 
117 	/**
118 	 * {@inheritDoc}
119 	 * @see java.lang.Object#toString()
120 	 */
121 	@Override
122 	public String toString() {
123 		return NLS.bind("AttributeValue \"{0}\" for \"{1}\"", value, attribute); //$NON-NLS-1$
124 	}
125 
126 	/**
127 	 * Returns a string representation of this value, to be used.
128 	 *
129 	 * @return string representation of this value
130 	 */
131 	public String format() {
132 		if (null == value) {
133 			return Messages.AttributeEditorDialog_5;
134 		} else {
135 			return getAttribute().getType().getValidator().convertToStringFormat(value);
136 		}
137 	}
138 }