f04b381f0abb3d0749d132d8a1d66cb4b0ec2b0b
[utils] / system / general / src / main / java / org / wamblee / system / graph / component / ConnectRequiredProvidedEdgeFilter.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 ConnectRequiredProvidedEdgeFilter implements EdgeFilter {
31
32     private String _client;
33     private String _required;
34     private String _server;
35     private String _provided;
36
37     public ConnectRequiredProvidedEdgeFilter(String aClient, String aRequired,
38             String aServer, String aProvided) {
39         _client = aClient;
40         _required = aRequired;
41         _server = aServer;
42         _provided = aProvided;
43         if ( _client == null ) { 
44             throw new IllegalArgumentException("Client component must be specified"); 
45         }
46     }
47
48     @Override
49     public boolean isViolated(Edge aEdge) {
50         if (aEdge.getFrom() instanceof RequiredInterfaceNode
51                 && aEdge.getTo() instanceof ProvidedInterfaceNode) {
52             return isViolated((RequiredInterfaceNode) aEdge.getFrom(),
53                     (ProvidedInterfaceNode) aEdge.getTo());
54         }
55         return false;
56     }
57
58     private boolean isViolated(RequiredInterfaceNode aFrom,
59             ProvidedInterfaceNode aTo) {
60         if (_client.equals(aFrom.getComponent().getName())
61                 && (_required == null || _required.equals(aFrom.getRequired()
62                         .getName()))) {
63             // From part matches.
64             if ( _server == null ) { 
65                 return true; // all connections are eliminated
66             }
67             if (_server.equals(aTo.getComponent().getName())
68                     && (_provided == null || _provided.equals(aTo.getProvided()
69                             .getName()))) {
70                 // to part matches also
71                 return false;
72             }
73             else { 
74                 // From matches and to doesn't so edgefilter is violated.
75                 return true; 
76             }
77         } else {
78             // From part does not match, restriction does not apply.
79             return false;
80         }
81     }
82 }