(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / graph / component / ConnectRequiredExternallyRequiredEdgeFilter.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 ConnectRequiredExternallyRequiredEdgeFilter implements EdgeFilter {
28     private String client;
29
30     private String required;
31
32     private String externalRequired;
33
34     /**
35      * Creates a new ConnectRequiredExternallyRequiredEdgeFilter object.
36      * 
37      */
38     public ConnectRequiredExternallyRequiredEdgeFilter(String aClient,
39         String aRequired, String aExternalRequired) {
40         client = aClient;
41         required = aRequired;
42         externalRequired = aExternalRequired;
43
44         if (client == null) {
45             throw new IllegalArgumentException(
46                 "Client component must be specified");
47         }
48
49         if (required == null) {
50             throw new IllegalArgumentException(
51                 "Required interface must be specified");
52         }
53
54         if (externalRequired == null) {
55             throw new IllegalArgumentException(
56                 "External required interface must be specified");
57         }
58     }
59
60     @Override
61     public boolean isViolated(Edge aEdge) {
62         if (aEdge.getFrom() instanceof RequiredInterfaceNode &&
63             aEdge.getTo() instanceof ExternalRequiredInterfaceNode) {
64             return isViolated((RequiredInterfaceNode) aEdge.getFrom(),
65                 (ExternalRequiredInterfaceNode) aEdge.getTo());
66         }
67
68         return false;
69     }
70
71     private boolean isViolated(RequiredInterfaceNode aFrom,
72         ExternalRequiredInterfaceNode aTo) {
73         if (!aFrom.getComponent().getName().equals(client)) {
74             return false; // wrong component.
75         }
76
77         if (!((required == null) || aFrom.getRequired().getName().equals(
78             required))) {
79             return false; // wrong interface
80         }
81
82         if (!aTo.getRequired().getName().equals(externalRequired)) {
83             return true; // wrong externally required interface.
84         }
85
86         return false;
87     }
88 }