source code formatting.
[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 /**
23  * Filter used to explicitly connect required and provided interfaces
24  * within a container.
25  *
26  * @author Erik Brakkee
27  */
28 public class ConnectRequiredProvidedEdgeFilter implements EdgeFilter {
29     /**
30      * DOCUMENT ME!
31      */
32     private String client;
33
34     /**
35      * DOCUMENT ME!
36      */
37     private String required;
38
39     /**
40      * DOCUMENT ME!
41      */
42     private String server;
43
44     /**
45      * DOCUMENT ME!
46      */
47     private String provided;
48
49     /**
50      * Creates a new ConnectRequiredProvidedEdgeFilter object.
51      *
52      * @param aClient DOCUMENT ME!
53      * @param aRequired DOCUMENT ME!
54      * @param aServer DOCUMENT ME!
55      * @param aProvided DOCUMENT ME!
56      */
57     public ConnectRequiredProvidedEdgeFilter(String aClient, String aRequired,
58         String aServer, String aProvided) {
59         client       = aClient;
60         required     = aRequired;
61         server       = aServer;
62         provided     = aProvided;
63
64         if (client == null) {
65             throw new IllegalArgumentException(
66                 "Client component must be specified");
67         }
68     }
69
70     /**
71      * DOCUMENT ME!
72      *
73      * @param aEdge DOCUMENT ME!
74      *
75      * @return DOCUMENT ME!
76      */
77     @Override
78     public boolean isViolated(Edge aEdge) {
79         if (aEdge.getFrom() instanceof RequiredInterfaceNode
80                 && aEdge.getTo() instanceof ProvidedInterfaceNode) {
81             return isViolated((RequiredInterfaceNode) aEdge.getFrom(),
82                 (ProvidedInterfaceNode) aEdge.getTo());
83         }
84
85         return false;
86     }
87
88     /**
89      * DOCUMENT ME!
90      *
91      * @param aFrom DOCUMENT ME!
92      * @param aTo DOCUMENT ME!
93      *
94      * @return DOCUMENT ME!
95      */
96     private boolean isViolated(RequiredInterfaceNode aFrom,
97         ProvidedInterfaceNode aTo) {
98         if (client.equals(aFrom.getComponent().getName())
99                 && ((required == null)
100                 || required.equals(aFrom.getRequired().getName()))) {
101             // From part matches.
102             if (server == null) {
103                 return true; // all connections are eliminated
104             }
105
106             if (server.equals(aTo.getComponent().getName())
107                     && ((provided == null)
108                     || provided.equals(aTo.getProvided().getName()))) {
109                 // to part matches also
110                 return false;
111             } else {
112                 // From matches and to doesn't so edgefilter is violated.
113                 return true;
114             }
115         } else {
116             // From part does not match, restriction does not apply.
117             return false;
118         }
119     }
120 }