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.g2d.diagram.impl;
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;
20 import org.simantics.utils.datastructures.MapList;
23 * @author Toni Kalajainen
25 public class AbstractHandlerClass<Handler> implements Serializable {
27 private static final long serialVersionUID = 4137854678128072467L;
29 private final MapList<Class<?>, Handler> handlers = new MapList<Class<?>, Handler>();
30 private final List<Handler> snapshot;
31 private final int hash;
33 public AbstractHandlerClass(Collection<Handler> contributions)
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();
42 private void _addClass(Class<?> clazz, Handler ec)
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);
53 * Get all interactors by class
57 @SuppressWarnings("unchecked")
58 public <R extends Handler> List<R> getItemsByClass(Class<R> clazz)
60 List<R> result = (List<R>) handlers.getValuesUnsafe(clazz);
63 return Collections.emptyList();
67 * Get a single item by class.
68 * (Convenience method)
73 * @throws RuntimeException if single interactor was not found
75 @SuppressWarnings("unchecked")
76 public <R extends Handler> R getSingleItem(Class<R> clazz)
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));
85 * Get a single item by class.
86 * (Convenience method)
90 * @return the item or <code>null</code>
91 * @throws RuntimeException if more than one items were found
93 @SuppressWarnings("unchecked")
94 public <R extends Handler> R getAtMostOneItemOfClass(Class<R> clazz)
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());
102 public <R extends Handler> R[] getAtMostItemsOfClass(Class<R> clazz, R[] result)
104 handlers.getAtMostValues(clazz, result);
108 public List<Handler> getAll()
113 public boolean contains(Handler handler)
115 return getAll().contains(handler);
118 @SuppressWarnings("unchecked")
119 public <R extends Handler> boolean containsClass(Class<R> clazz)
121 List<R> list = (List<R>) handlers.getValuesUnsafe(clazz);
122 if (list==null || list.size()==0) return false;
123 return list.size()>0;
127 public int hashCode() {
132 public boolean equals(Object obj) {
137 if (!(obj instanceof AbstractHandlerClass<?>))
139 AbstractHandlerClass<?> other = (AbstractHandlerClass<?>) obj;
140 return snapshot.equals(other.snapshot);