1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
14 * @author Toni Kalajainen
16 package org.simantics.utils.datastructures.hints;
21 * Hint context interface
26 * @see IHintObservable
28 public interface IHintContext extends IHintObservable {
31 * Set a value for a hint.
33 * @param value the value, <code>null</code> is not accepted
35 void setHint(Key key, Object value);
41 void setHints(Map<Key, Object> hints);
44 * Remove a hint. In stack hint implementation lower priority hint will
47 * @param <E> type of the object bound to the specified key
48 * @param key key to use for the hint
49 * @return the typed object currently bound to the specified key
51 <E> E removeHint(Key key);
54 * Clears the context of all current hints without notifying any listeners.
56 void clearWithoutNotification();
61 public static abstract class Key {
63 * Test if value is accepted
64 * @param value the value
65 * @return true if value is accepted, false if not
67 public abstract boolean isValueAccepted(Object value);
71 * Mouse id specific key. With this key there is unique value for each mouse id.
73 public static abstract class MouseSpecificKey extends Key {
74 private final int mouseId;
75 public MouseSpecificKey(int mouseId)
77 this.mouseId = mouseId;
79 public int getMouseId()
84 public int hashCode() {
85 return super.hashCode() ^ mouseId;
88 public boolean equals(Object obj) {
89 if (!(obj instanceof MouseSpecificKey)) return false;
90 if (((MouseSpecificKey)obj).mouseId != mouseId) return false;
91 if (!obj.getClass().equals(getClass())) return false;
97 * Key whose value is of a specific given class.
99 public static class KeyOf extends Key {
100 final Class<?> clazz;
101 final String keyName;
102 public KeyOf(Class<?> clazz)
107 public KeyOf(Class<?> clazz, String keyName)
110 this.keyName = keyName;
113 public boolean isValueAccepted(Object value) {
114 return clazz.isInstance(value);
117 public String toString() {
119 return "Key Of "+clazz.getName();
120 return keyName + "(" + clazz.getSimpleName() + ")";
127 public static abstract class StringKey extends Key {
128 protected final String str;
130 public StringKey(String str)
133 hash = getClass().hashCode() ^str.hashCode();
135 public String getString()
140 public int hashCode() {
144 public boolean equals(Object obj) {
145 if (!(obj instanceof StringKey)) return false;
146 if (!((StringKey)obj).str.equals(str)) return false;
147 if (obj.getClass()!=getClass()) return false;
153 * String based key whose value is of a specific class.
155 public static class StringKeyOf extends StringKey {
156 final Class<?> clazz;
157 public StringKeyOf(String str, Class<?> clazz)
163 public boolean isValueAccepted(Object value) {
164 return clazz.isInstance(value);
167 public String toString() {
168 return "Key Of ("+clazz.getName()+", "+str+")";
173 public static class MouseSpecificKeyOf extends MouseSpecificKey {
174 public final Class<?> clazz;
175 public final int mouseId;
177 public MouseSpecificKeyOf(int mouseId, Class<?> clazz)
181 this.mouseId = mouseId;
182 hash = getClass().hashCode() ^ mouseId ^ 24392439;
185 public boolean isValueAccepted(Object value) {
186 return clazz.isInstance(value);
189 public int hashCode() {
193 public boolean equals(Object obj) {
194 if (obj.getClass()!=this.getClass()) return false;
195 MouseSpecificKeyOf other = (MouseSpecificKeyOf) obj;
196 return other.mouseId == mouseId;