]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbolcontribution/AbstractSymbolProvider.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbolcontribution / AbstractSymbolProvider.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.symbolcontribution;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import org.simantics.diagram.symbollibrary.ISymbolGroup;
20
21 public abstract class AbstractSymbolProvider implements ISymbolProvider {
22     protected volatile boolean         disposed = false;
23     protected Runnable                 listener;
24     protected Collection<ISymbolGroup> groups   = Collections.emptyList();
25     private Set<ISymbolGroup>          groupSet = null;
26
27     public Collection<ISymbolGroup> getSymbolGroups() {
28         return groups;
29     }
30
31     public void setListener(Runnable listener) {
32         this.listener = listener;
33     }
34
35     public void dispose() {
36         disposed = true;
37     }
38
39     void setSymbolGroup(Collection<ISymbolGroup> groups) {
40         if (groups == null)
41             throw new NullPointerException("null groups");
42         this.groups = groups;
43     }
44
45     protected Set<ISymbolGroup> getSymbolGroupSet() {
46         synchronized (this) {
47             if (groupSet != null)
48                 return groupSet;
49             groupSet = new HashSet<ISymbolGroup>(getSymbolGroups());
50             return groupSet;
51         }
52     }
53
54     void lockGroups() {
55         groups = Collections.unmodifiableCollection(groups);
56         groupSet = null;
57         getSymbolGroupSet();
58     }
59
60     @Override
61     public int hashCode() {
62         return getSymbolGroupSet().hashCode();
63     }
64
65     @Override
66     public boolean equals(Object obj) {
67         if (this == obj)
68             return true;
69         if (obj == null)
70             return false;
71         if (!(obj instanceof AbstractSymbolProvider))
72             return false;
73         AbstractSymbolProvider other = (AbstractSymbolProvider) obj;
74         return getSymbolGroupSet().equals( other.getSymbolGroupSet() );
75     }
76
77 }