]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/internal/IGECache.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / internal / IGECache.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.common.internal;
13
14 import gnu.trove.set.hash.THashSet;
15
16 import java.util.Collection;
17 import java.util.Set;
18
19 import org.simantics.browsing.ui.NodeContext;
20 import org.simantics.browsing.ui.NodeContext.CacheKey;
21
22 public interface IGECache {
23
24     interface IGECacheEntry {
25         Object getValue();
26         void setValue(Object value);
27 //        Collection<NodeQueryUpdater> getDependencies();
28 //        void addDependency(NodeQueryUpdater caller);
29         NodeContext getContext();
30         CacheKey<?> getKey();
31         Collection<IGECacheEntry> getDependencies();
32         void addDependency(IGECacheEntry caller);
33         void reset();
34     }
35
36     class GECacheEntry implements IGECacheEntry {
37
38         private Object result;
39         final private NodeContext context;
40         final private CacheKey<?> key;
41         private Set<IGECacheEntry> callers = new THashSet<IGECacheEntry>(2);
42
43         public <T> GECacheEntry(NodeContext context, CacheKey<T> key, T result) {
44             this.context = context;
45             this.key = key;
46             this.result = result;
47         }
48         @Override
49         public Collection<IGECacheEntry> getDependencies() {
50             return callers;
51         }
52         @Override
53         public Object getValue() {
54             return result;
55         }
56         @Override
57         public void addDependency(IGECacheEntry caller) {
58             assert(caller != null);
59             callers.add(caller);
60         }
61         @Override
62         public void setValue(Object value) {
63             result = value;
64         }
65         @Override
66         public NodeContext getContext() {
67             return context;
68         }
69         @Override
70         public CacheKey<?> getKey() {
71             return key;
72         }
73         @Override
74         public void reset() {
75             callers = new THashSet<IGECacheEntry>(2);
76         }
77
78         @Override
79         public int hashCode() {
80             return 31 * context.hashCode() | key.hashCode();
81         }
82
83         @Override
84         public boolean equals(Object object) {
85
86             if (this == object)
87                 return true;
88             else if (object == null)
89                 return false;
90             else if (getClass() != object.getClass())
91                 return false;
92
93             GECacheEntry i = (GECacheEntry)object;
94
95             return key.equals(i.key) && context.equals(i.context);
96
97         }
98
99 //        @Override
100 //        public String toString() {
101 //            return "GECacheEntry[" + result + ", " + context + ", " + key + "]";
102 //        }
103
104     }
105
106     <T> IGECacheEntry getEntry(NodeContext context, CacheKey<T> key);
107
108     <T> IGECacheEntry put(NodeContext context, CacheKey<T> key, T value);
109     <T> void putTreeReference(NodeContext context, CacheKey<T> key, UIElementReference reference);
110
111     <T> T get(NodeContext context, CacheKey<T> key);
112     <T> Set<UIElementReference> getTreeReference(NodeContext context, CacheKey<T> key);
113
114     <T> void remove(NodeContext context, CacheKey<T> key);
115     <T> Set<UIElementReference> removeTreeReference(NodeContext context, CacheKey<T> key);
116     
117     boolean isShown(NodeContext context);
118     void incRef(NodeContext context);
119     void decRef(NodeContext context);
120     
121     void dispose();
122 }