View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
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 p3j.misc.math;
17  
18  import java.io.Serializable;
19  
20  /**
21   * Matrix object that contains its value as a {@link Matrix2D}. It also provides
22   * an ID and hash code calculation routines, so that similar matrices can be
23   * found easily by the storage system.
24   * 
25   * Created: August 18, 2008
26   * 
27   * @author Christina Bohk
28   * @author Roland Ewald
29   * 
30   */
31  public class Matrix implements Serializable {
32  
33  	/** Serialization ID. */
34  	private static final long serialVersionUID = 5456211627452741990L;
35  
36  	/** ID of this matrix. */
37  	private int id = -1;
38  
39  	/** Hash, for quick lookup in database. */
40  	private long hash;
41  
42  	/** Value of this matrix. */
43  	private Matrix2D value;
44  
45  	/**
46  	 * Default constructor.
47  	 * 
48  	 * @param val
49  	 *          the associated value
50  	 */
51  	public Matrix(Matrix2D val) {
52  		storeValueAndHash(val);
53  	}
54  
55  	/**
56  	 * Instantiates a new matrix (only required for beans compliance, do not use
57  	 * manually).
58  	 */
59  	public Matrix() {
60  	}
61  
62  	/**
63  	 * Creates (deep) copy of this matrix, apart from the ID. The ID of the copy
64  	 * is set to -1.
65  	 * 
66  	 * @return copy of this matrix
67  	 */
68  	public Matrix copy() {
69  		return new Matrix(value.copy());
70  	}
71  
72  	public int getID() {
73  		return id;
74  	}
75  
76  	public void setID(int id) {
77  		this.id = id;
78  	}
79  
80  	public long getHash() {
81  		return hash;
82  	}
83  
84  	public void setHash(long hash) {
85  		this.hash = hash;
86  	}
87  
88  	public Matrix2D getValue() {
89  		return value;
90  	}
91  
92  	/**
93  	 * Sets the value of the matrix. Also updates the hash code of the object.
94  	 * 
95  	 * @param value
96  	 *          the new value
97  	 */
98  	public void setValue(Matrix2D value) {
99  		storeValueAndHash(value);
100 	}
101 
102 	/**
103 	 * Sets the matrix value and updates the hash code of the matrix.
104 	 * 
105 	 * @param value
106 	 *          the new value
107 	 */
108 	private void storeValueAndHash(Matrix2D value) {
109 		this.value = value;
110 		hash = value.hashCode();
111 	}
112 
113 	@Override
114 	public boolean equals(Object o) {
115 		if (o == this) {
116 			return true;
117 		}
118 		if (!(o instanceof Matrix)) {
119 			return false;
120 		}
121 		Matrix2D val = ((Matrix) o).getValue();
122 		return val.equals(value);
123 	}
124 
125 	@Override
126 	public int hashCode() {
127 		return (int) hash;
128 	}
129 }