KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > io > Union


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.io;
10
11 /**
12  * <p> This class represents a <code>C/C++ union</code>; it works in the same
13  * way as {@link Struct} (sub-class) except that all members are mapped
14  * to the same location in memory.</p>
15  * <p> Here is an example of C union: [code]
16  * union Number {
17  * int asInt;
18  * float asFloat;
19  * char asString[12];
20  * };[/code]
21  * And its Java equivalent:[code]
22  * public class Number extends Union {
23  * Signed32 asInt = new Signed32();
24  * Float32 asFloat = new Float32();
25  * Utf8String asString = new Utf8String(12);
26  * }[/code]
27  * As for any {@link Struct}, fields are directly accessible:[code]
28  * Number num = new Number();
29  * num.asInt.set(23);
30  * num.asString.set("23"); // Null terminated (C compatible)
31  * float f = num.asFloat.get();[/code]
32  *
33  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
34  * @version 1.0, October 4, 2004
35  */

36 public abstract class Union extends Struct {
37
38     /**
39      * Default constructor.
40      */

41     public Union() {
42     }
43
44     /**
45      * Returns <code>true</code>.
46      *
47      * @return <code>true</code>
48      */

49     public final boolean isUnion() {
50         return true;
51     }
52 }
Popular Tags