create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / plugin / framework / AbstractService.java
1 package net.sf.openrocket.plugin.framework;
2
3 import java.util.Collections;
4 import java.util.List;
5
6 import net.sf.openrocket.util.BugException;
7
8 /**
9  * An abstract service implementation that returns plugins of type P.
10  * 
11  * @param <P>   the plugin type that this service returns.
12  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
13  */
14 public abstract class AbstractService<P> implements Service {
15         
16         private final Class<P> type;
17         
18         protected AbstractService(Class<P> type) {
19                 this.type = type;
20         }
21         
22         @SuppressWarnings("unchecked")
23         @Override
24         public <E> List<E> getPlugins(Class<E> e, Object... args) {
25                 
26                 if (e != type) {
27                         return Collections.emptyList();
28                 }
29                 
30                 List<P> plugins = getPlugins(args);
31                 
32                 // Check list content types to avoid mysterious bugs later on
33                 for (P p : plugins) {
34                         if (!type.isInstance(p)) {
35                                 throw new BugException("Requesting plugins of type " + type + " but received " +
36                                                 ((p != null) ? p.getClass() : "null"));
37                         }
38                 }
39                 
40                 return (List<E>) plugins;
41         }
42         
43         protected abstract List<P> getPlugins(Object... args);
44         
45 }