]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/LinkedListRuleFactory2.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / annotations / factories / LinkedListRuleFactory2.java
1 /*******************************************************************************
2  * Copyright (c) 2019 Association for Decentralized Information Management in
3  * 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  *     Semantum oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.objmap.graph.annotations.factories;
13
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Method;
16
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
21 import org.simantics.objmap.graph.annotations.LinkedListAdd;
22 import org.simantics.objmap.graph.annotations.LinkedListGet;
23 import org.simantics.objmap.graph.annotations.LinkedListRem;
24 import org.simantics.objmap.graph.rules.MappedElementsRule;
25 import org.simantics.objmap.graph.rules.domain.LinkedListAccessor;
26 import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory;
27 import org.simantics.objmap.graph.rules.range.CollectionAccessor;
28
29 public class LinkedListRuleFactory2<Range> implements ICollectionRuleFactory<Resource,Range> {
30         
31         @Override
32         public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation annotation,
33                         Method getter, Method adder, Method remover)
34                         throws DatabaseException {
35                 LinkedListGet getterAnn = (LinkedListGet)annotation;
36                 return new MappedElementsRule<Resource,Range>(new LinkedListAccessor(g.getResource(getterAnn.value()), g.getResource(getterAnn.type()), getterAnn.composition()),
37                                         new CollectionAccessor<Range,Range>(getter, adder, remover));
38         }
39         
40         @Override
41         public boolean isAdder(Annotation getterAnnotation, Annotation annotation) {
42                 LinkedListGet getterAnn = (LinkedListGet)getterAnnotation;
43                 LinkedListAdd adderAnn = (LinkedListAdd)annotation;
44                 return getterAnn.value().equals(adderAnn.value());
45         }
46         
47         @Override
48         public boolean isRemover(Annotation getterAnnotation, Annotation annotation) {
49                 LinkedListGet getterAnn = (LinkedListGet)getterAnnotation;
50                 LinkedListRem adderAnn = (LinkedListRem)annotation;
51                 return getterAnn.value().equals(adderAnn.value());
52         }
53
54 }