]> gerrit.simantics Code Review - simantics/3d.git/blob - javax.vecmath/src/javax/vecmath/Tuple4b.java
Included old javax.vecmath 1.5.2 to org.simantics.g3d.feature
[simantics/3d.git] / javax.vecmath / src / javax / vecmath / Tuple4b.java
1 /*
2  * $RCSfile: Tuple4b.java,v $
3  *
4  * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
7  * This code is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 only, as
9  * published by the Free Software Foundation.  Sun designates this
10  * particular file as subject to the "Classpath" exception as provided
11  * by Sun in the LICENSE file that accompanied this code.
12  *
13  * This code is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16  * version 2 for more details (a copy is included in the LICENSE file that
17  * accompanied this code).
18  *
19  * You should have received a copy of the GNU General Public License version
20  * 2 along with this work; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24  * CA 95054 USA or visit www.sun.com if you need additional information or
25  * have any questions.
26  *
27  * $Revision: 1.7 $
28  * $Date: 2008/02/28 20:18:51 $
29  * $State: Exp $
30  */
31
32 package javax.vecmath;
33
34 import java.lang.Math;
35
36 /**
37  * A four byte tuple.  Note that Java defines a byte as a signed integer
38  * in the range [-128, 127]. However, colors are more typically
39  * represented by values in the range [0, 255]. Java 3D recognizes this
40  * and, in those cases where Tuple4b is used to represent color, treats
41  * the bytes as if the range were [0, 255]---in other words, as if the
42  * bytes were unsigned.
43  * Values greater than 127 can be assigned to a byte variable using a
44  * type cast.  For example:
45  * <ul>byteVariable = (byte) intValue; // intValue can be > 127</ul>
46  * If intValue is greater than 127, then byteVariable will be negative.  The
47  * correct value will be extracted when it is used (by masking off the upper
48  * bits).
49  */
50 public abstract class Tuple4b implements java.io.Serializable, Cloneable {
51
52     static final long serialVersionUID = -8226727741811898211L;
53
54     /**
55      * The first value.
56      */
57     public      byte    x;
58
59     /**
60      * The second value.
61      */
62     public      byte    y;
63
64     /**
65      * The third value.
66      */
67     public      byte    z;
68
69     /**
70      * The fourth value.
71      */
72     public      byte    w;
73
74
75     /**
76      * Constructs and initializes a Tuple4b from the specified four values.
77      * @param b1 the first value
78      * @param b2 the second value
79      * @param b3 the third value
80      * @param b4 the fourth value
81      */
82     public Tuple4b(byte b1, byte b2, byte b3, byte b4)
83     {
84         this.x = b1;
85         this.y = b2;
86         this.z = b3;
87         this.w = b4;
88     }
89
90
91     /**
92      * Constructs and initializes a Tuple4b from the array of length 4.
93      * @param t the array of length 4 containing b1 b2 b3 b4 in order
94      */
95     public Tuple4b(byte[] t)
96     {
97         this.x = t[0];
98         this.y = t[1];
99         this.z = t[2];
100         this.w = t[3];
101     }
102
103
104     /**
105      * Constructs and initializes a Tuple4b from the specified Tuple4b.
106      * @param t1 the Tuple4b containing the initialization x y z w data
107      */
108     public Tuple4b(Tuple4b t1)
109     {
110         this.x = t1.x;
111         this.y = t1.y;
112         this.z = t1.z;
113         this.w = t1.w;
114     }
115
116
117     /**
118      * Constructs and initializes a Tuple4b to (0,0,0,0).
119      */
120     public Tuple4b()
121     {
122         this.x = (byte) 0;
123         this.y = (byte) 0;
124         this.z = (byte) 0;
125         this.w = (byte) 0;
126     }
127
128
129    /**
130      * Returns a string that contains the values of this Tuple4b.
131      * @return the String representation
132      */  
133     public String toString()
134     {
135         return("("  + ((int)this.x & 0xff) +
136                ", " + ((int)this.y & 0xff) +
137                ", " + ((int)this.z & 0xff) +
138                ", " + ((int)this.w & 0xff) + ")");
139     }
140
141
142    /**
143      * Places the value of the x,y,z,w components of this Tuple4b
144      * into the array of length 4.
145      * @param b   array of length 4 into which the values are placed
146      */
147     public final void get(byte[] b)
148     {
149         b[0] = this.x;
150         b[1] = this.y;
151         b[2] = this.z;
152         b[3] = this.w;
153     }
154
155
156    /**   
157      * Places the value of the x,y,z,w components of this
158      * Tuple4b into the tuple t1.
159      * @param t1   tuple into which the values are placed
160      */
161     public final void get(Tuple4b t1)
162     {
163        t1.x = this.x;
164        t1.y = this.y;
165        t1.z = this.z;
166        t1.w = this.w;
167    }
168
169
170    /**
171      * Sets the value of the data members of this tuple to the value
172      * of the argument tuple t1.
173      * @param t1  the source tuple
174      */
175     public final void set(Tuple4b t1)
176     {
177         this.x = t1.x;
178         this.y = t1.y;
179         this.z = t1.z;
180         this.w = t1.w;
181     }
182
183
184    /**
185      * Sets the value of the data members of this tuple to the value
186      * of the array b of length 4.
187      * @param b   the source array of length 4
188      */
189     public final void set(byte[] b)
190     {
191         this.x = b[0];
192         this.y = b[1];
193         this.z = b[2];
194         this.w = b[3];
195     }
196
197  
198    /**
199      * Returns true if all of the data members of tuple t1 are equal to
200      * the corresponding data members in this tuple.
201      * @param t1  the tuple with which the comparison is made
202      */
203     public boolean equals(Tuple4b t1)
204     {
205         try {
206            return(this.x == t1.x && this.y == t1.y && 
207                   this.z == t1.z && this.w == t1.w);
208         }
209         catch (NullPointerException e2) {return false;}
210
211     }
212
213    /**
214      * Returns true if the Object t1 is of type Tuple4b and all of the
215      * data members of t1 are equal to the corresponding data members in
216      * this Tuple4b.
217      * @param t1  the object with which the comparison is made
218      */
219     public boolean equals(Object t1)
220     {
221         try {
222            Tuple4b t2 = (Tuple4b) t1;
223            return(this.x == t2.x && this.y == t2.y && 
224                   this.z == t2.z && this.w == t2.w);
225         }
226         catch (NullPointerException e2) {return false;}
227         catch (ClassCastException   e1) {return false;}
228
229     }
230
231
232     /**
233      * Returns a hash code value based on the data values in this
234      * object.  Two different Tuple4b objects with identical data values
235      * (i.e., Tuple4b.equals returns true) will return the same hash
236      * code value.  Two objects with different data members may return the
237      * same hash value, although this is not likely.
238      * @return the integer hash code value
239      */  
240     public int hashCode() {
241         return ((((int)x & 0xff) <<  0) |
242                 (((int)y & 0xff) <<  8) |
243                 (((int)z & 0xff) << 16) |
244                 (((int)w & 0xff) << 24));
245     }
246
247     /**
248      * Creates a new object of the same class as this object.
249      *
250      * @return a clone of this instance.
251      * @exception OutOfMemoryError if there is not enough memory.
252      * @see java.lang.Cloneable
253      * @since vecmath 1.3
254      */
255     public Object clone() {
256         // Since there are no arrays we can just use Object.clone()
257         try {
258             return super.clone();
259         } catch (CloneNotSupportedException e) {
260             // this shouldn't happen, since we are Cloneable
261             throw new InternalError();
262         }
263     }
264
265
266     /**
267          * Get <i>x</i>, the first value.
268          * 
269          * @return Returns <i>x</i>, the first value.
270          * 
271          * @since vecmath 1.5
272          */
273         public final byte getX() {
274                 return x;
275         }
276
277
278         /**
279          * Set <i>x</i>,  the first value.
280          * 
281          * @param x the first value.
282          * 
283          * @since vecmath 1.5
284          */
285         public final void setX(byte x) {
286                 this.x = x;
287         }
288
289
290         /**
291          * Get <i>y</i>, the second value.
292          * 
293          * @return Returns <i>y</i>, the second value.
294          * 
295          * @since vecmath 1.5
296          */
297         public final byte getY() {
298                 return y;
299         }
300
301
302         /**
303          * Set <i>y</i>, the second value.
304          * 
305          * @param y the second value.
306          * 
307          * @since vecmath 1.5
308          */
309         public final void setY(byte y) {
310                 this.y = y;
311         }
312
313         /**
314          * Get <i>z</i>, the third value.
315          * 
316          *  @return Returns <i>z</i>, the third value.  
317          *   
318          * @since vecmath 1.5
319          */
320         public final byte getZ() {
321                 return z;
322         }
323
324
325         /**
326          * Set <i>z</i>,  the third value.
327          * 
328          * @param z  the third value.
329          * 
330          * @since vecmath 1.5
331          */
332         public final void setZ(byte z) {
333                 this.z = z;
334         }
335
336
337         /**
338          * Get <i>w</i>, the fourth value.
339          * 
340          * @return Returns <i>w</i> - the fourth value.
341          * 
342          * @since vecmath 1.5
343          */
344         public final byte getW() {
345                 return w;
346         }
347
348
349         /**
350          * Set <i>w</i>, the fourth value.
351          * 
352          * @param w the fourth value.
353          * 
354          * @since vecmath 1.5
355          */
356         public final void setW(byte w) {
357                 this.w = w;
358         }
359 }