]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.message/src/org/simantics/message/AbstractMessageSchemeHandler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.message / src / org / simantics / message / AbstractMessageSchemeHandler.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.message;
13
14
15 /**
16  * Example usage:
17  * <pre>
18  * public class MyResourceSchemeHandler extends AbstractMessageSchemeHandler<MyResource> {
19  *     public MyResourceSchemeHandler() {
20  *         super("myresource", MyResource.class);
21  *     }
22  *     public void doPerform(MyResource r) {
23  *         // Do your stuff...
24  *     }
25  * }
26  * </pre>
27  * 
28  * @author Tuukka Lehtonen
29  */
30 public abstract class AbstractMessageSchemeHandler<T> implements IMessageSchemeHandler {
31
32     private String scheme;
33     private Class<?> expectedDataClass;
34     
35     public AbstractMessageSchemeHandler(String scheme, Class<?> expectedDataClass) {
36         this.scheme = scheme;
37         this.expectedDataClass = expectedDataClass;
38     }
39     
40     @Override
41     public String getScheme() {
42         return scheme;
43     }
44
45     @SuppressWarnings("unchecked")
46     @Override
47     public void perform(Object o) throws MessageSchemeException {
48         if (expectedDataClass.isInstance(o)) {
49             doPerform((T) o);
50         } else {
51             handleInvalidData(o);
52         }
53     }
54
55     public void handleInvalidData(Object o) {
56         // Override to handle differently
57         throw new RuntimeException("Object " + o + " is not an instance of " + expectedDataClass.getCanonicalName());
58     }
59
60     public abstract void doPerform(T o) throws MessageSchemeException;
61
62 }