]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/impl/AbstractHandlerClass.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / diagram / impl / AbstractHandlerClass.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.g2d.diagram.impl;
13
14 import java.io.Serializable;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
19
20 import org.simantics.utils.datastructures.MapList;
21
22 /**
23  * @author Toni Kalajainen
24  */
25 public class AbstractHandlerClass<Handler> implements Serializable {
26
27     private static final long serialVersionUID = 4137854678128072467L;
28
29     private final MapList<Class<?>, Handler> handlers = new MapList<Class<?>, Handler>();
30     private final List<Handler> snapshot;
31     private final int hash;
32
33     public AbstractHandlerClass(Collection<Handler> contributions)
34     {
35         for (Handler c : contributions)
36             _addClass(c.getClass(), c);
37         this.handlers.makeImmutable();
38         this.snapshot = Collections.unmodifiableList(new ArrayList<Handler>(contributions));
39         this.hash = snapshot.hashCode();
40     }
41
42     private void _addClass(Class<?> clazz, Handler ec)
43     {
44         if (handlers.contains(clazz, ec)) return;
45         handlers.add(clazz, ec);
46         Class<?> superClazz = clazz.getSuperclass();
47         if (superClazz!=null) _addClass(superClazz, ec);
48         for (Class<?> interphase : clazz.getInterfaces())
49             _addClass(interphase, ec);
50     }
51
52     /**
53      * Get all interactors by class
54      * @param clazz
55      * @return collection
56      */
57     @SuppressWarnings("unchecked")
58     public <R extends Handler> List<R> getItemsByClass(Class<R> clazz)
59     {
60         List<R> result = (List<R>) handlers.getValuesUnsafe(clazz);
61         if (result!=null)
62             return result;
63         return Collections.emptyList();
64     }
65
66     /**
67      * Get a single item by class.
68      * (Convenience method)
69      * 
70      * @param <R>
71      * @param clazz
72      * @return the item
73      * @throws RuntimeException if single interactor was not found
74      */
75     @SuppressWarnings("unchecked")
76     public <R extends Handler> R getSingleItem(Class<R> clazz)
77     {
78         List<R> list = (List<R>) handlers.getValuesUnsafe(clazz);
79         if (list==null || list.size()!=1)
80             throw new RuntimeException("Invalid element class "+toString()+": One and only one "+clazz.getName()+" is expected, got " + (list != null ? list.size() : 0));
81         return list.get(0);
82     }
83
84     /**
85      * Get a single item by class.
86      * (Convenience method)
87      * 
88      * @param <R>
89      * @param clazz
90      * @return the item or <code>null</code>
91      * @throws RuntimeException if more than one items were found
92      */
93     @SuppressWarnings("unchecked")
94     public <R extends Handler> R getAtMostOneItemOfClass(Class<R> clazz)
95     {
96         List<R> list = (List<R>) handlers.getValuesUnsafe(clazz);
97         if (list==null || list.size()==0) return null;
98         if (list.size()==1) return list.get(0);
99         throw new RuntimeException("At most one was "+clazz.getName()+" expected in "+this.getClass().getName() + ", got " + list.size());
100     }
101
102     public <R extends Handler> R[] getAtMostItemsOfClass(Class<R> clazz, R[] result)
103     {
104         handlers.getAtMostValues(clazz, result);
105         return result;
106     }
107
108     public List<Handler> getAll()
109     {
110         return snapshot;
111     }
112
113     public boolean contains(Handler handler)
114     {
115         return getAll().contains(handler);
116     }
117
118     @SuppressWarnings("unchecked")
119     public <R extends Handler> boolean containsClass(Class<R> clazz)
120     {
121         List<R> list = (List<R>) handlers.getValuesUnsafe(clazz);
122         if (list==null || list.size()==0) return false;
123         return list.size()>0;
124     }
125
126     @Override
127     public int hashCode() {
128         return hash;
129     }
130
131     @Override
132     public boolean equals(Object obj) {
133         if (this == obj)
134             return true;
135         if (obj == null)
136             return false;
137         if (!(obj instanceof AbstractHandlerClass<?>))
138             return false;
139         AbstractHandlerClass<?> other = (AbstractHandlerClass<?>) obj;
140         return snapshot.equals(other.snapshot);
141     }
142
143 }