KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > Union2


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util;
21
22 import java.io.Serializable JavaDoc;
23
24 /**
25  * A union type which can contain one of two kinds of objects.
26  * {@link Object#equals} and {@link Object#hashCode} treat this as a container,
27  * not identical to the contained object, but the identity is based on the contained
28  * object. The union is serialiable if its contained object is.
29  * {@link Object#toString} delegates to the contained object.
30  * @author Jesse Glick
31  * @since org.openide.util 7.1
32  */

33 public abstract class Union2<First,Second> implements Cloneable JavaDoc, Serializable JavaDoc {
34
35     private static final long serialVersionUID = 1L;
36
37     Union2() {}
38
39     /**
40      * Retrieve the union member of the first type.
41      * @return the object of the first type
42      * @throws IllegalArgumentException if the union really contains the second type
43      */

44     public abstract First first() throws IllegalArgumentException JavaDoc;
45
46     /**
47      * Retrieve the union member of the second type.
48      * @return the object of the second type
49      * @throws IllegalArgumentException if the union really contains the first type
50      */

51     public abstract Second second() throws IllegalArgumentException JavaDoc;
52
53     /**
54      * Check if the union contains the first type.
55      * @return true if it contains the first type, false if it contains the second type
56      */

57     public abstract boolean hasFirst();
58
59     /**
60      * Check if the union contains the second type.
61      * @return true if it contains the second type, false if it contains the first type
62      */

63     public abstract boolean hasSecond();
64
65     @Override JavaDoc
66     public abstract Union2<First,Second> clone();
67
68     /**
69      * Construct a union based on the first type.
70      * @param first an object of the first type
71      * @return a union containing that object
72      */

73     public static <First,Second> Union2<First,Second> createFirst(First first) {
74         return new Union2First<First,Second>(first);
75     }
76
77     /**
78      * Construct a union based on the second type.
79      * @param second an object of the second type
80      * @return a union containing that object
81      */

82     public static <First,Second> Union2<First,Second> createSecond(Second second) {
83         return new Union2Second<First,Second>(second);
84     }
85
86     private static final class Union2First<First,Second> extends Union2<First,Second> {
87
88         private static final long serialVersionUID = 1L;
89
90         private final First first;
91
92         public Union2First(First first) {
93             this.first = first;
94         }
95
96         @Override JavaDoc
97         public First first() throws IllegalArgumentException JavaDoc {
98             return first;
99         }
100
101         @Override JavaDoc
102         public Second second() throws IllegalArgumentException JavaDoc {
103             throw new IllegalArgumentException JavaDoc();
104         }
105
106         @Override JavaDoc
107         public boolean hasFirst() {
108             return true;
109         }
110
111         @Override JavaDoc
112         public boolean hasSecond() {
113             return false;
114         }
115
116         @Override JavaDoc
117         public String JavaDoc toString() {
118             return String.valueOf(first);
119         }
120
121         @Override JavaDoc
122         public boolean equals(Object JavaDoc obj) {
123             return (obj instanceof Union2First) && first.equals(((Union2First) obj).first);
124         }
125
126         @Override JavaDoc
127         public int hashCode() {
128             return first.hashCode();
129         }
130
131         @Override JavaDoc
132         public Union2<First,Second> clone() {
133             return createFirst(first);
134         }
135
136     }
137
138     private static final class Union2Second<First,Second> extends Union2<First,Second> {
139
140         private static final long serialVersionUID = 1L;
141
142         private final Second second;
143
144         public Union2Second(Second second) {
145             this.second = second;
146         }
147
148         @Override JavaDoc
149         public First first() throws IllegalArgumentException JavaDoc {
150             throw new IllegalArgumentException JavaDoc();
151         }
152
153         @Override JavaDoc
154         public Second second() throws IllegalArgumentException JavaDoc {
155             return second;
156         }
157
158         @Override JavaDoc
159         public boolean hasFirst() {
160             return false;
161         }
162
163         @Override JavaDoc
164         public boolean hasSecond() {
165             return true;
166         }
167
168         @Override JavaDoc
169         public String JavaDoc toString() {
170             return String.valueOf(second);
171         }
172
173         @Override JavaDoc
174         public boolean equals(Object JavaDoc obj) {
175             return (obj instanceof Union2Second) && second.equals(((Union2Second) obj).second);
176         }
177
178         @Override JavaDoc
179         public int hashCode() {
180             return second.hashCode();
181         }
182
183         @Override JavaDoc
184         public Union2<First,Second> clone() {
185             return createSecond(second);
186         }
187
188     }
189
190 }
191
Popular Tags