578bc72f4d4b3c232668c83a005d7d9db446f082
[utils] / system / general / src / main / java / org / wamblee / system / graph / component / ConnectExternalProvidedProvidedFilter.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
21 /**
22  * Filter used to explicitly connect required and provided interfaces within a
23  * container.
24  * 
25  * @author Erik Brakkee
26  * 
27  */
28 public class ConnectExternalProvidedProvidedFilter implements EdgeFilter {
29
30     private String externalProvided;
31     private String component;
32     private String provided;
33
34     public ConnectExternalProvidedProvidedFilter(String aExternalProvided, String aComponent,
35             String aProvided) {
36         externalProvided = aExternalProvided; 
37         component = aComponent; 
38         provided = aProvided; 
39         if ( externalProvided == null ) { 
40             throw new IllegalArgumentException("External provided interface name must be specified."); 
41         }
42         if ( component == null ) { 
43             throw new IllegalArgumentException("Component name must be specified");
44         }
45         if ( provided == null ) { 
46             throw new IllegalArgumentException("Provided interface name of internal component must be specified");
47         }
48     }
49
50     @Override
51     public boolean isViolated(Edge aEdge) {
52         if (aEdge.getFrom() instanceof ExternalProvidedInterfaceNode
53                 && aEdge.getTo() instanceof ProvidedInterfaceNode) {
54             return isViolated((ExternalProvidedInterfaceNode) aEdge.getFrom(),
55                     (ProvidedInterfaceNode) aEdge.getTo());
56         }
57         return false;
58     }
59
60     private boolean isViolated(ExternalProvidedInterfaceNode aFrom,
61             ProvidedInterfaceNode aTo) {
62         if ( !aFrom.getName().equals(externalProvided)) { 
63             return false; // wrong provided interface.
64         }
65         if ( aTo.getComponent().getName().equals(component) && 
66                aTo.getProvided().getName().equals(provided) ) { 
67             return false; // ok 
68         }
69         return true; 
70     }
71 }