All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.mind.AbstractVisitorDispatcher Maven / Gradle / Ivy

There is a newer version: 2.0
Show newest version
/**
 * Copyright (C) 2009 STMicroelectronics
 *
 * This file is part of "Mind Compiler" is free software: you can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public License 
 * as published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 *
 * Contact: [email protected]
 *
 * Authors: Matthieu Leclercq
 * Contributors: 
 */

package org.ow2.mind;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.objectweb.fractal.adl.ADLException;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;

public abstract class AbstractVisitorDispatcher
    implements
      Visitor,
      BindingController {

  // --------------------------------------------------------------------------
  // Client interfaces
  // --------------------------------------------------------------------------

  /** The name of the {@link Visitor} client interface of this component. */
  public static final String              CLIENT_VISITOR = "client-visitor";

  /** The builders client interfaces. */
  // use a LinkedHashMap to ensure the iteration order which improves
  // stability when debugging
  public Map> visitorsItf    = new LinkedHashMap>();

  // --------------------------------------------------------------------------
  // Abstract methods
  // --------------------------------------------------------------------------

  /**
   * This method is used by {@link #bindFc(String, Object) bindFc} to cast the
   * given serverItf to a correct type. Sub-classes must implements
   * this method to guaranty type safety.
   */
  protected abstract Visitor castVisitorInterface(Object serverItf);

  protected abstract O aggregateResults(List results);

  // --------------------------------------------------------------------------
  // Implementation of the Visitor interface
  // --------------------------------------------------------------------------

  public O visit(final I input, final Map context)
      throws ADLException {
    final List results = new ArrayList();
    for (final Visitor visitorItf : visitorsItf.values()) {
      results.add(visitorItf.visit(input, context));
    }
    return aggregateResults(results);
  }

  // --------------------------------------------------------------------------
  // Implementation of the BindingController interface
  // --------------------------------------------------------------------------

  public String[] listFc() {
    final List interfaceList = new ArrayList(visitorsItf
        .keySet());
    return interfaceList.toArray(new String[interfaceList.size()]);
  }

  public void bindFc(final String clientItfName, final Object serverItf)
      throws NoSuchInterfaceException, IllegalBindingException,
      IllegalLifeCycleException {

    if (clientItfName == null) {
      throw new IllegalArgumentException("Interface name can't be null");
    }

    if (clientItfName.startsWith(CLIENT_VISITOR))
      visitorsItf.put(clientItfName, castVisitorInterface(serverItf));
    else
      throw new NoSuchInterfaceException("There is no interface named '"
          + clientItfName + "'");
  }

  public Object lookupFc(final String clientItfName)
      throws NoSuchInterfaceException {

    if (clientItfName == null) {
      throw new IllegalArgumentException("Interface name can't be null");
    }

    if (clientItfName.startsWith(CLIENT_VISITOR))
      return visitorsItf.get(clientItfName);
    else
      throw new NoSuchInterfaceException("There is no interface named '"
          + clientItfName + "'");
  }

  public void unbindFc(final String clientItfName)
      throws NoSuchInterfaceException, IllegalBindingException,
      IllegalLifeCycleException {

    if (clientItfName == null) {
      throw new IllegalArgumentException("Interface name can't be null");
    }

    if (clientItfName.startsWith(CLIENT_VISITOR)) {
      if (visitorsItf.remove(clientItfName) == null) {
        throw new NoSuchInterfaceException("There is no interface named '"
            + clientItfName + "'");
      }
    } else
      throw new NoSuchInterfaceException("There is no interface named '"
          + clientItfName + "'");
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy