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