Added hashcode/equals methods to generic profile Group implementations
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / DiagramElementGroup.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.diagram.profile;
13
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.RequestProcessor;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.primitiverequest.OrderedSet;
22 import org.simantics.db.common.procedure.wrapper.SetListenerToSingleSetListener;
23 import org.simantics.db.common.request.BinaryRead;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.procedure.SetListener;
26 import org.simantics.diagram.stubs.DiagramResource;
27 import org.simantics.scenegraph.profile.Group;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public abstract class DiagramElementGroup implements Group {
33
34     protected DiagramResource DIA;
35
36     public DiagramElementGroup(ReadGraph graph) {
37         this.DIA = DiagramResource.getInstance(graph);
38     }
39
40     @Override
41     public void trackItems(RequestProcessor processor, final Resource runtimeDiagram, final SetListener<Resource> listener) throws DatabaseException {
42         processor.syncRequest(new BinaryRead<Class<?>, Resource, Collection<Resource>>(getClass(), runtimeDiagram) {
43
44             @Override
45             public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
46                 HashSet<Resource> result = new HashSet<Resource>();
47
48                 Resource realDiagram = graph.getPossibleObject(parameter2, DIA.RuntimeDiagram_HasConfiguration);
49                 if (realDiagram == null)
50                     return result;
51
52                 Collection<Resource> elements = graph.syncRequest(new OrderedSet(realDiagram));
53                 for (Resource element : elements) {
54                     if (test(graph, element)) {
55                         result.add(element);
56                     }
57                 }
58
59                 return result;
60             }
61
62         }, new SetListenerToSingleSetListener<Resource>(listener));
63     }
64
65     protected abstract boolean test(ReadGraph graph, Resource element) throws DatabaseException;
66     
67     @Override
68     public int hashCode() {
69         return this.getClass().hashCode();
70     }
71
72     @Override
73     public boolean equals(Object obj) {
74         return obj.getClass().equals(this.getClass());
75     }
76
77 }