ece7b289374139664c096c806db5f6e72a26805d
[utils] / system / general / src / main / java / org / wamblee / system / core / DefaultInterfaceRestriction.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.core;
17
18 /**
19  * An interface restriction on the required interface of a component. 
20  * 
21  * @author Erik Brakkee
22  *
23  */
24 public class DefaultInterfaceRestriction implements InterfaceRestriction {
25
26     private String _client;
27     private String _required;
28     private String _server;
29     private String _provided;
30
31     /**
32      * Constructs the restriction. If the client name and required interface
33      * name match, then the server name and interface name must also match. 
34      * Otherwise the restriction is violated. 
35      * 
36      * This can be used to explicitly connect required and provided interfaces
37      * of components. 
38      * 
39      * @param aClient
40      *            Client or null if no restriction on the client.
41      * @param aRequired
42      *            Required interface name or null if no restriction on the
43      *            required interface.
44      * @param aServer
45      *            Server or null if no restriction on the server.
46      * @param aProvided
47      *            Provided interface name or null if no restriction on the
48      *            provided interface.
49      */
50     public DefaultInterfaceRestriction(String aClient, String aRequired,
51             String aServer, String aProvided) {
52         
53         if ( aClient == null && aRequired == null ) { 
54             throw new IllegalArgumentException("No restriction on the required interface");
55         }
56         if ( aServer == null && aProvided == null ) { 
57             throw new IllegalArgumentException("No restriction on the provided interface");
58         }
59         _client = aClient;
60         _required = aRequired;
61         _server = aServer;
62         _provided = aProvided;
63     }
64
65     @Override
66     public boolean isViolated(Component aClient, RequiredInterface aRequired,
67             Component aServer, ProvidedInterface aProvided) {
68         
69         if ( _client != null && !aClient.getName().equals(_client)) { 
70             return false; 
71         }
72         if ( _required != null && !aRequired.getName().equals(_required)) { 
73             return false; 
74         }
75         
76         // The required interface matches
77         
78         if ( _server != null && !aServer.getName().equals(_server)) {
79             // Server was specified and does not match.
80             return true; 
81         }
82         if ( _provided != null && !aProvided.getName().equals(_provided)) { 
83             // provided interface was specified and doe not match. 
84             return true; 
85         }
86         
87         // The required and provided interfaces match so this is not a violation.
88         
89         return false; 
90     }
91
92 }