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