/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.message; /** * Example usage: *
 * public class MyResourceSchemeHandler extends AbstractMessageSchemeHandler {
 *     public MyResourceSchemeHandler() {
 *         super("myresource", MyResource.class);
 *     }
 *     public void doPerform(MyResource r) {
 *         // Do your stuff...
 *     }
 * }
 * 
* * @author Tuukka Lehtonen */ public abstract class AbstractMessageSchemeHandler implements IMessageSchemeHandler { private String scheme; private Class expectedDataClass; public AbstractMessageSchemeHandler(String scheme, Class expectedDataClass) { this.scheme = scheme; this.expectedDataClass = expectedDataClass; } @Override public String getScheme() { return scheme; } @SuppressWarnings("unchecked") @Override public void perform(Object o) throws MessageSchemeException { if (expectedDataClass.isInstance(o)) { doPerform((T) o); } else { handleInvalidData(o); } } public void handleInvalidData(Object o) { // Override to handle differently throw new RuntimeException("Object " + o + " is not an instance of " + expectedDataClass.getCanonicalName()); } public abstract void doPerform(T o) throws MessageSchemeException; }