Now basing the implementation on a component graph.
[utils] / system / general / src / main / java / org / wamblee / system / container / CompositeInterfaceRestriction.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.container;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21
22 import org.wamblee.system.core.Component;
23 import org.wamblee.system.core.ProvidedInterface;
24 import org.wamblee.system.core.RequiredInterface;
25
26
27 /**
28  * Composite restriction that wraps a number of other restrictions. The restriction is 
29  * violated if one of the contained restrictions is violated. 
30  * 
31  * @author Erik Brakkee
32  *
33  */
34 public class CompositeInterfaceRestriction implements InterfaceRestriction {
35     
36     private List<InterfaceRestriction> _restrictions; 
37     
38     /**
39      * Constructs the restriction. 
40      */
41     public CompositeInterfaceRestriction() { 
42         _restrictions = new ArrayList<InterfaceRestriction>();
43     }
44     
45     /**
46      * Constructs the restriction. 
47      * @param aRestrictions List of contained restrictions. 
48      */
49     public CompositeInterfaceRestriction(Collection<InterfaceRestriction> aRestrictions) { 
50         this(); 
51         _restrictions.addAll(aRestrictions);
52     }
53     
54     /**
55      * Adds a restriction. 
56      * @param aRestriction Restriction. 
57      * @return Reference to this composite restriction to allow call chaining. 
58      */
59     public CompositeInterfaceRestriction add(InterfaceRestriction aRestriction) { 
60         _restrictions.add(aRestriction);
61         return this; 
62     }
63
64     @Override
65     public boolean isViolated(Component aClient, RequiredInterface aRequired,
66             Component aServer, ProvidedInterface aProvided) {
67         for (InterfaceRestriction restriction: _restrictions) { 
68             if ( restriction.isViolated(aClient, aRequired, aServer, aProvided)) { 
69                 return true; 
70             }
71         }
72         return false; 
73     }
74
75 }