001    /*--------------------------------------------------------------------------+
002    $Id: SimulinkOutPort.java 26285 2010-02-18 11:22:54Z 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.model;
019    
020    import java.util.ArrayList;
021    import java.util.Set;
022    
023    import edu.tum.cs.commons.assertion.CCSMPre;
024    import edu.tum.cs.commons.assertion.PreconditionException;
025    import edu.tum.cs.commons.collections.CollectionUtils;
026    import edu.tum.cs.commons.collections.IdentityHashSet;
027    import edu.tum.cs.commons.collections.UnmodifiableSet;
028    
029    /**
030     * A Simulink inport. An outport can be connected to multiple
031     * {@link SimulinkLine}s.
032     * 
033     * @author deissenb
034     * @author $Author: juergens $
035     * @version $Rev: 26285 $
036     * @levd.rating GREEN Hash: 879597C4C4DBC4EBFA19F2BE4A419CCC
037     */
038    public class SimulinkOutPort extends SimulinkPortBase {
039    
040            /** The lines connected to this port. */
041            private final Set<SimulinkLine> lines = new IdentityHashSet<SimulinkLine>();
042    
043            /**
044             * Create simulink outport.
045             * 
046             * @param block
047             *            The block this port belongs to.
048             * @param index
049             *            The port index. This may be a number or a string like 'enable'
050             */
051            public SimulinkOutPort(SimulinkBlock block, String index) {
052                    super(block, index);
053                    block.addOutPort(this);
054            }
055    
056            /**
057             * Add line connected to this port. This is only called from the
058             * {@link SimulinkLine}.
059             * 
060             * @throws PreconditionException
061             *             if this port is already connected to the line or the line's
062             *             source port does not match this port.
063             */
064            /* package */void addLine(SimulinkLine line)
065                            throws IllegalArgumentException {
066                    CCSMPre.isFalse(lines.contains(line),
067                                    "Line is already connected to this port.");
068                    CCSMPre
069                                    .isTrue(line.getSrcPort() == this,
070                                                    "Line's port does not match.");
071    
072                    lines.add(line);
073            }
074    
075            /**
076             * Get lines connected to this port.
077             */
078            public UnmodifiableSet<SimulinkLine> getLines() {
079                    return CollectionUtils.asUnmodifiable(lines);
080            }
081    
082            /**
083             * Remove line. This is only called from the {@link SimulinkLine}.
084             * 
085             * @throws PreconditionException
086             *             if the provided line is not connected to this port
087             */
088            /* package */void removeLine(SimulinkLine line)
089                            throws IllegalArgumentException {
090                    CCSMPre.isTrue(lines.contains(line), "Line is not connected to port.");
091                    lines.remove(line);
092            }
093    
094            /** {@inheritDoc} */
095            @Override
096            public void remove() {
097                    getBlock().removeOutPort(this);
098                    for (SimulinkLine line : new ArrayList<SimulinkLine>(lines)) {
099                            line.remove();
100                    }
101                    super.remove();
102            }
103    
104    }