added explicit linking of required to externally required interfaces.
[utils] / system / general / src / main / java / org / wamblee / system / graph / component / ConnectRequiredExternallyRequiredEdgeFilter.java
1 /*
2  * Copyright 2008 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.system.graph.component;
17
18 import org.wamblee.system.graph.Edge;
19 import org.wamblee.system.graph.EdgeFilter;
20 import org.wamblee.system.graph.EdgeSelector;
21 import org.wamblee.system.graph.Node;
22
23 /**
24  * Filter used to explicitly connect required and provided interfaces within a
25  * container.
26  * 
27  * @author Erik Brakkee
28  * 
29  */
30 public class ConnectRequiredExternallyRequiredEdgeFilter implements EdgeFilter {
31
32     private String _client;
33     private String _required;
34     private String _externalRequired;
35
36     public ConnectRequiredExternallyRequiredEdgeFilter(String aClient, String aRequired,
37             String aExternalRequired) {
38         _client = aClient;
39         _required = aRequired;
40         _externalRequired = aExternalRequired; 
41         if ( _client == null ) { 
42             throw new IllegalArgumentException("Client component must be specified"); 
43         }
44         if ( _required == null ) { 
45             throw new IllegalArgumentException("Required interface must be specified");
46         }
47         if ( _externalRequired == null ) { 
48             throw new IllegalArgumentException("External required interface must be specified");
49         }
50     }
51
52     @Override
53     public boolean isViolated(Edge aEdge) {
54         if (aEdge.getFrom() instanceof RequiredInterfaceNode
55                 && aEdge.getTo() instanceof ExternalRequiredInterfaceNode) {
56             return isViolated((RequiredInterfaceNode) aEdge.getFrom(),
57                     (ExternalRequiredInterfaceNode) aEdge.getTo());
58         }
59         return false;
60     }
61
62     private boolean isViolated(RequiredInterfaceNode aFrom,
63             ExternalRequiredInterfaceNode aTo) {
64         if ( !aFrom.getComponent().getName().equals(_client)) { 
65             return false; // wrong component. 
66         }
67         if ( !(_required == null || aFrom.getRequired().getName().equals(_required))) { 
68             return false; // wrong interface
69         }
70         if ( !aTo.getRequired().getName().equals(_externalRequired)) { 
71             return true; // wrong externally required interface.  
72         }
73       
74         return false; 
75     }
76 }