Mercurial > projects > dwt2
comparison org.eclipse.core.commands/src/org/eclipse/core/commands/common/HandleObject.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2004, 2006 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.core.commands.common.HandleObject; | |
14 | |
15 import org.eclipse.core.internal.commands.util.Util; | |
16 import org.eclipse.core.commands.common.IIdentifiable; | |
17 import org.eclipse.core.commands.common.EventManager; | |
18 import java.lang.all; | |
19 | |
20 /** | |
21 * <p> | |
22 * An object that can exist in one of two states: defined and undefined. This is | |
23 * used by APIs that want to give a handle to an object, even though the object | |
24 * does not fully exist yet. This way, users can attach listeners to objects | |
25 * before they come into existence. It also protects the API from users that do | |
26 * not release references when they should. | |
27 * </p> | |
28 * <p> | |
29 * To enforce good coding practice, all handle objects must implement | |
30 * <code>equals</code> and <code>toString</code>. Please use | |
31 * <code>string</code> to cache the result for <code>toString</code> once | |
32 * calculated. | |
33 * </p> | |
34 * <p> | |
35 * All handle objects are referred to using a single identifier. This identifier | |
36 * is a instance of <code>String</code>. It is important that this identifier | |
37 * remain unique within whatever context that handle object is being used. For | |
38 * example, there should only ever be one instance of <code>Command</code> | |
39 * with a given identifier. | |
40 * </p> | |
41 * | |
42 * @since 3.1 | |
43 */ | |
44 public abstract class HandleObject : EventManager, | |
45 IIdentifiable { | |
46 | |
47 /** | |
48 * The constant integer hash code value meaning the hash code has not yet | |
49 * been computed. | |
50 */ | |
51 private static const int HASH_CODE_NOT_COMPUTED = -1; | |
52 | |
53 /** | |
54 * A factor for computing the hash code for all schemes. | |
55 */ | |
56 private static const int HASH_FACTOR = 89; | |
57 | |
58 /** | |
59 * The seed for the hash code for all schemes. | |
60 */ | |
61 private static const int HASH_INITIAL; | |
62 | |
63 static this(){ | |
64 HASH_INITIAL = | |
65 //typeid(char[]).getHash(& HandleObject.classinfo.name ); | |
66 java.lang.all.toHash(HandleObject.classinfo.name); | |
67 } | |
68 /** | |
69 * Whether this object is defined. A defined object is one that has been | |
70 * fully initialized. By default, all objects start as undefined. | |
71 */ | |
72 protected /+transient+/ bool defined = false; | |
73 | |
74 /** | |
75 * The hash code for this object. This value is computed lazily, and marked | |
76 * as invalid when one of the values on which it is based changes. | |
77 */ | |
78 private /+transient+/ int hashCode = HASH_CODE_NOT_COMPUTED; | |
79 | |
80 /** | |
81 * The identifier for this object. This identifier should be unique across | |
82 * all objects of the same type and should never change. This value will | |
83 * never be <code>null</code>. | |
84 */ | |
85 protected /+final+/ String id; | |
86 | |
87 /** | |
88 * The string representation of this object. This string is for debugging | |
89 * purposes only, and is not meant to be displayed to the user. This value | |
90 * is computed lazily, and is cleared if one of its dependent values | |
91 * changes. | |
92 */ | |
93 protected /+transient+/ String string = null; | |
94 | |
95 /** | |
96 * Constructs a new instance of <code>HandleObject</code>. | |
97 * | |
98 * @param id | |
99 * The id of this handle; must not be <code>null</code>. | |
100 */ | |
101 protected this(String id) { | |
102 if (id is null) { | |
103 throw new NullPointerException( | |
104 "Cannot create a handle with a null id"); //$NON-NLS-1$ | |
105 } | |
106 | |
107 this.id = id; | |
108 } | |
109 | |
110 /** | |
111 * Tests whether this object is equal to another object. A handle object is | |
112 * only equal to another handle object with the same id and the same class. | |
113 * | |
114 * @param object | |
115 * The object with which to compare; may be <code>null</code>. | |
116 * @return <code>true</code> if the objects are equal; <code>false</code> | |
117 * otherwise. | |
118 */ | |
119 public override int opEquals(Object object) { | |
120 // Check if they're the same. | |
121 if (object is this) { | |
122 return true; | |
123 } | |
124 | |
125 // Check if they're the same type. | |
126 if (!(cast(HandleObject)object)) { | |
127 return false; | |
128 } | |
129 | |
130 // Check each property in turn. | |
131 final HandleObject handle= cast(HandleObject) object; | |
132 return Util.equals(id, handle.id) | |
133 && (this.classinfo is handle.classinfo); | |
134 } | |
135 | |
136 public /+override+/ final String getId() { | |
137 return id; | |
138 } | |
139 | |
140 /** | |
141 * Computes the hash code for this object based on the id. | |
142 * | |
143 * @return The hash code for this object. | |
144 */ | |
145 public final override hash_t toHash() { | |
146 if (hashCode is HASH_CODE_NOT_COMPUTED) { | |
147 hashCode = HASH_INITIAL * HASH_FACTOR + Util.toHash(id); | |
148 if (hashCode is HASH_CODE_NOT_COMPUTED) { | |
149 hashCode++; | |
150 } | |
151 } | |
152 return hashCode; | |
153 } | |
154 | |
155 /** | |
156 * Whether this instance is defined. A defined instance is one that has been | |
157 * fully initialized. This allows objects to effectively disappear even | |
158 * though other objects may still have references to them. | |
159 * | |
160 * @return <code>true</code> if this object is defined; <code>false</code> | |
161 * otherwise. | |
162 */ | |
163 public final bool isDefined() { | |
164 return defined; | |
165 } | |
166 | |
167 /** | |
168 * The string representation of this object -- for debugging purposes only. | |
169 * This string should not be shown to an end user. | |
170 * | |
171 * @return The string representation; never <code>null</code>. | |
172 */ | |
173 public override abstract String toString(); | |
174 | |
175 /** | |
176 * Makes this object becomes undefined. This method should make any defined | |
177 * properties <code>null</code>. It should also send notification to any | |
178 * listeners that these properties have changed. | |
179 */ | |
180 public abstract void undefine(); | |
181 } |