]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/hints/IHintContext.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / hints / IHintContext.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  *\r
14  * @author Toni Kalajainen\r
15  */\r
16 package org.simantics.utils.datastructures.hints;\r
17 \r
18 import java.util.Map;\r
19 \r
20 /**\r
21  * Hint context interface\r
22  *\r
23  * @see Key\r
24  * @see HintContext\r
25  * @see HintStack\r
26  * @see IHintObservable\r
27  */\r
28 public interface IHintContext extends IHintObservable {\r
29 \r
30         /**\r
31          * Set a value for a hint. \r
32          * @param key key\r
33          * @param value the value, <code>null</code> is not accepted\r
34          */\r
35         void setHint(Key key, Object value);\r
36         \r
37         /**\r
38          * Set a set of hints\r
39          * @param hints\r
40          */\r
41         void setHints(Map<Key, Object> hints);\r
42         \r
43         /**\r
44          * Remove a hint. In stack hint implementation lower priority hint will\r
45          * become effective. \r
46 \r
47          * @param <E> type of the object bound to the specified key\r
48          * @param key key to use for the hint\r
49          * @return the typed object currently bound to the specified key\r
50          */\r
51         <E> E removeHint(Key key);\r
52 \r
53         /**\r
54          * Clears the context of all current hints without notifying any listeners.\r
55          */\r
56         void clearWithoutNotification();\r
57 \r
58         /**\r
59          * Hint key\r
60          */\r
61         public static abstract class Key {\r
62                 /**\r
63                  * Test if value is accepted\r
64                  * @param value the value\r
65                  * @return true if value is accepted, false if not\r
66                  */\r
67                 public abstract boolean isValueAccepted(Object value);          \r
68         }\r
69         \r
70         /**\r
71          * Mouse id specific key. With this key there is unique value for each mouse id.\r
72          */\r
73         public static abstract class MouseSpecificKey extends Key {\r
74                 private final int mouseId;\r
75                 public MouseSpecificKey(int mouseId)\r
76                 {\r
77                         this.mouseId = mouseId;\r
78                 }\r
79                 public int getMouseId()\r
80                 {\r
81                         return mouseId;\r
82                 }\r
83                 @Override\r
84                 public int hashCode() {\r
85                         return super.hashCode() ^ mouseId;\r
86                 }               \r
87                 @Override\r
88                 public boolean equals(Object obj) {\r
89                         if (!(obj instanceof MouseSpecificKey)) return false;\r
90                         if (((MouseSpecificKey)obj).mouseId != mouseId) return false;\r
91                         if (!obj.getClass().equals(getClass())) return false;\r
92                         return true;\r
93                 }\r
94         }\r
95 \r
96         /**\r
97          * Key whose value is of a specific given class. \r
98          */\r
99         public static class KeyOf extends Key {\r
100                 final Class<?> clazz;\r
101                 final String keyName;\r
102         public KeyOf(Class<?> clazz)\r
103         {\r
104             this.clazz = clazz;\r
105             this.keyName = null;\r
106         }\r
107         public KeyOf(Class<?> clazz, String keyName)\r
108         {\r
109             this.clazz = clazz;\r
110             this.keyName = keyName;\r
111         }\r
112                 @Override\r
113                 public boolean isValueAccepted(Object value) {\r
114                         return clazz.isInstance(value);\r
115                 }\r
116                 @Override\r
117                 public String toString() {\r
118                     if (keyName == null)\r
119                         return "Key Of "+clazz.getName();\r
120                     return keyName + "(" + clazz.getSimpleName() + ")";\r
121                 }\r
122         }\r
123         \r
124         /**\r
125          * String based key\r
126          */\r
127         public static abstract class StringKey extends Key {\r
128                 protected final String str;             \r
129                 protected int hash;\r
130                 public StringKey(String str)\r
131                 {\r
132                         this.str = str;\r
133                         hash = getClass().hashCode() ^str.hashCode();                   \r
134                 }\r
135                 public String getString()\r
136                 {\r
137                         return str;\r
138                 }\r
139                 @Override\r
140                 public int hashCode() {\r
141                         return hash;\r
142                 }               \r
143                 @Override\r
144                 public boolean equals(Object obj) {\r
145                         if (!(obj instanceof StringKey)) return false;\r
146                         if (!((StringKey)obj).str.equals(str)) return false;\r
147                         if (obj.getClass()!=getClass()) return false;\r
148                         return true;\r
149                 }\r
150         }       \r
151         \r
152         /**\r
153          * String based key whose value is of a specific class.\r
154          */\r
155         public static class StringKeyOf extends StringKey {\r
156                 final Class<?> clazz;           \r
157                 public StringKeyOf(String str, Class<?> clazz)\r
158                 {\r
159                         super(str);\r
160                         this.clazz = clazz;\r
161                 }\r
162                 @Override\r
163                 public boolean isValueAccepted(Object value) {\r
164                         return clazz.isInstance(value);\r
165                 }\r
166                 @Override\r
167                 public String toString() {\r
168                         return "Key Of ("+clazz.getName()+", "+str+")";\r
169                 }               \r
170         }\r
171         \r
172 \r
173         public static class MouseSpecificKeyOf extends MouseSpecificKey {\r
174                 public final Class<?> clazz;\r
175                 public final int mouseId;\r
176                 final int hash;\r
177                 public MouseSpecificKeyOf(int mouseId, Class<?> clazz)\r
178                 {\r
179                         super(mouseId);\r
180                         this.clazz = clazz;\r
181                         this.mouseId = mouseId;\r
182                         hash = getClass().hashCode() ^ mouseId ^ 24392439;\r
183                 }\r
184                 @Override\r
185                 public boolean isValueAccepted(Object value) {\r
186                         return clazz.isInstance(value);\r
187                 }\r
188                 @Override\r
189                 public int hashCode() {\r
190                         return hash;\r
191                 }\r
192                 @Override\r
193                 public boolean equals(Object obj) {\r
194                         if (obj.getClass()!=this.getClass()) return false;\r
195                         MouseSpecificKeyOf other = (MouseSpecificKeyOf) obj;\r
196                         return other.mouseId == mouseId;\r
197                 }\r
198         }\r
199         \r
200         \r
201 }\r