]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/VirtualGraphObjectSource.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / VirtualGraphObjectSource.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.db.common;
13
14 import org.simantics.databoard.binding.Binding;
15 import org.simantics.db.VirtualGraphContext;
16 import org.simantics.db.VirtualGraphSource;
17 import org.simantics.utils.datastructures.BijectionMap;
18 import org.simantics.utils.datastructures.Pair;
19
20 public abstract class VirtualGraphObjectSource implements VirtualGraphSource {
21
22     private BijectionMap<Object, Integer> hash = new BijectionMap<Object, Integer>();
23     
24     class ResourceObject {
25         public int id;
26         public ResourceObject(int id) {
27             this.id = id;
28         }
29         @Override
30         public int hashCode() {
31             return id;
32         }
33         @Override
34         public boolean equals(Object object) {
35             if (this == object)
36                 return true;
37             else if (object == null)
38                 return false;
39             else if (ResourceObject.class != object.getClass())
40                 return false;
41             ResourceObject r = (ResourceObject)object;
42             return r.id == id;
43         }
44     }
45     
46 //    protected void invalidate(VirtualGraphContext context, Object subject, Object predicate) {
47 //        context.invalidate(getOrCreate(context, subject), getOrCreate(context, predicate));
48 //    }
49
50 //    protected Object getObject(VirtualGraphContext context, org.simantics.db.Resource resource) {
51 //        int id = context.getIndex(resource);
52 //        Object result = new Resource(id);
53 //        hash.map(result, id);
54 //        return result;
55 //    }
56     
57 //    private int getOrCreate(VirtualGraphContext context, Object object) {
58 //        if(object instanceof org.simantics.db.Resource) return context.getIndex((org.simantics.db.Resource)object);
59 //        Integer value = hash.getRight(object);
60 //        if(value == null) {
61 //            value = context.newResource();
62 //            hash.map(object, value);
63 //        }
64 //        return value;
65 //    }
66     
67     private Object existingOrResource(VirtualGraphContext context, int id) {
68         Object value = hash.getLeft(id);
69         if(value == null) return context.getResource(id);
70         else return value;
71     }
72
73 //    @Override
74 //    final public int[] getStatements(VirtualGraphContext context, int resource) {
75 //        Object[] stms = getStatements(existingOrResource(context, resource));
76 //        int[] result = new int[stms.length];
77 //        for(int i=0;i<stms.length;i++) result[i] = getOrCreate(context, stms[i]);
78 //        return result;
79 //    }
80 //
81 //    @Override
82 //    final public Pair<Object, Binding> getValue(VirtualGraphContext context, int resource) {
83 //        return getValue(context, existingOrResource(context, resource));
84 //    }
85     
86 //    public org.simantics.db.Resource getResource(VirtualGraphContext context, Object virtual) {
87 //        return (org.simantics.db.Resource)context.getResource(getOrCreate(context, virtual));
88 //    }
89
90     abstract public Object[] getStatements(Object resource);
91     abstract public Pair<Object, Binding> getValue(VirtualGraphContext context, Object resource);
92     
93 }