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 *******************************************************************************/
12 package org.simantics.db.common;
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;
20 public abstract class VirtualGraphObjectSource implements VirtualGraphSource {
22 private BijectionMap<Object, Integer> hash = new BijectionMap<Object, Integer>();
24 class ResourceObject {
26 public ResourceObject(int id) {
30 public int hashCode() {
34 public boolean equals(Object object) {
37 else if (object == null)
39 else if (ResourceObject.class != object.getClass())
41 ResourceObject r = (ResourceObject)object;
46 // protected void invalidate(VirtualGraphContext context, Object subject, Object predicate) {
47 // context.invalidate(getOrCreate(context, subject), getOrCreate(context, predicate));
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);
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);
67 private Object existingOrResource(VirtualGraphContext context, int id) {
68 Object value = hash.getLeft(id);
69 if(value == null) return context.getResource(id);
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]);
82 // final public Pair<Object, Binding> getValue(VirtualGraphContext context, int resource) {
83 // return getValue(context, existingOrResource(context, resource));
86 // public org.simantics.db.Resource getResource(VirtualGraphContext context, Object virtual) {
87 // return (org.simantics.db.Resource)context.getResource(getOrCreate(context, virtual));
90 abstract public Object[] getStatements(Object resource);
91 abstract public Pair<Object, Binding> getValue(VirtualGraphContext context, Object resource);