001    /*--------------------------------------------------------------------------+
002    $Id: SimulinkIdProvider.java 26277 2010-02-18 10:46:58Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.simulink.util;
019    
020    import edu.tum.cs.commons.collections.IIdProvider;
021    import edu.tum.cs.commons.test.DeepCloneTestUtils;
022    import edu.tum.cs.simulink.model.SimulinkElementBase;
023    import edu.tum.cs.simulink.model.SimulinkLine;
024    import edu.tum.cs.simulink.model.SimulinkPortBase;
025    import edu.tum.cs.simulink.model.stateflow.IStateflowElement;
026    import edu.tum.cs.simulink.model.stateflow.StateflowElementBase;
027    import edu.tum.cs.simulink.model.stateflow.StateflowTransition;
028    
029    /**
030     * Id provider to be used for {@link DeepCloneTestUtils}.
031     * 
032     * @author deissenb
033     * @author $Author: juergens $
034     * @version $Rev: 26277 $
035     * @levd.rating GREEN Hash: F18B1CA1B02EFCE38AD48C47DA487704
036     */
037    public class SimulinkIdProvider implements IIdProvider<String, Object> {
038            /**
039             * Obtain id for element.
040             * 
041             * @throws RuntimeException
042             *             if an unknown type was encountered.
043             */
044            public String obtainId(Object object) {
045                    if (object instanceof SimulinkElementBase) {
046                            return ((SimulinkElementBase) object).getName();
047                    }
048                    if (object instanceof StateflowElementBase<?>) {
049                            return ((IStateflowElement<?>) object).getStateflowId();
050                    }
051                    if (object instanceof SimulinkPortBase) {
052                            SimulinkPortBase port = (SimulinkPortBase) object;
053                            return port.getBlock().getId() + "-" + port.getIndex();
054                    }
055                    if (object instanceof SimulinkLine) {
056                            SimulinkLine line = (SimulinkLine) object;
057                            return obtainId(line.getSrcPort()) + "-"
058                                            + obtainId(line.getDstPort());
059                    }
060                    if (object instanceof StateflowTransition) {
061                            StateflowTransition transition = (StateflowTransition) object;
062    
063                            if (transition.getSrc() == null) {
064                                    return "null-" + obtainId(transition.getDst());
065                            }
066    
067                            return obtainId(transition.getSrc()) + "-"
068                                            + obtainId(transition.getDst());
069                    }
070                    throw new RuntimeException("Unknown type " + object.getClass());
071            }
072    }