Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
42   209   21   5.25
32   80   0.67   8
8     3.5  
1    
 
 
  IntersectionValidityPeriod       Line # 43 42 21 50% 0.5
 
  (1)
 
1    /*
2    * Copyright (c) 2000-2005, University of Salford
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10    *
11    * Redistributions in binary form must reproduce the above copyright notice,
12    * this list of conditions and the following disclaimer in the documentation
13    * and/or other materials provided with the distribution.
14    *
15    * Neither the name of the University of Salford nor the names of its
16    * contributors may be used to endorse or promote products derived from this
17    * software without specific prior written permission.
18    *
19    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29    * POSSIBILITY OF SUCH DAMAGE.
30    */
31   
32    package issrg.pba.rbac;
33   
34    import java.util.Date;
35   
36    /**
37    * This class is an intersection of ValidityPeriods. It is used when the
38    * intersection cannot be precomputed, e.g. when a AdjustedValidityPeriod is
39    * intersected with another ValidityPeriod, or when RelativeValidityPeriods are
40    * intersected - the results of both intersections will depend on the current
41    * time.
42    */
 
43    public class IntersectionValidityPeriod extends DefaultValidityPeriodBehaviour {
44    ValidityPeriod vp1;
45    ValidityPeriod vp2;
46   
 
47  0 toggle protected IntersectionValidityPeriod() {
48    }
49   
50    /**
51    * This constructor builds an intersection of two ValidityPeriods. They are
52    * treated equally, and any of them can be null. If either of them is null,
53    * then the result of getNotBefore and getNotAfter is provided by the
54    * non-null ValidityPeriod.
55    *
56    * @param v1 - one ValidityPeriod
57    * @param v2 - another ValidityPeriod
58    */
 
59  4068 toggle public IntersectionValidityPeriod(ValidityPeriod v1, ValidityPeriod v2){
60  4068 vp1=v1;
61  4068 vp2=v2;
62   
63    // the following check allows to simplify the operations
64    // so that it is sufficient to test whether v1 is an AdjustedValidityPeriod,
65    // and then it is not necessary to test if v2 is, see the other methods.
66  4068 if (v2 instanceof AdjustedValidityPeriod){ // we will check for vp1 being adjustedvalidityperiod only
67    // if they both are adjustedvalidityperiod, the result will be "now or never"
68    // (the adjusted validity period would return now for both NA and NB dates.
69    // the other adjustedvalidityperiod can only change it to "never")
70  0 vp1=v2;
71  0 vp2=v1;
72    }
73    }
74   
75    /**
76    * Generally, this method returns the latest of the notBefore times of the
77    * ValidityPeriods.
78    *
79    * <p>If either of the ValidityPeriods is a
80    * AdjustedValidityPeriod, the result is the notBefore time of the periods
81    * adjusted against each other. If neither of the ValidityPeriods is an
82    * AdjustedValidityPeriod, then the latest notBefore time of the two is
83    * returned.
84    *
85    * @return the Date specifying the latest notBefore time of the two periods,
86    * or null, if it is not constrained ("valid from time immemorial")
87    */
 
88  4036 toggle public java.util.Date getNotBefore(){
89  4036 Date notBefore=null;
90  4036 if (vp1!=null){
91  4036 if (vp1 instanceof AdjustedValidityPeriod){
92  0 return ((AdjustedValidityPeriod)vp1).adjust(vp2).getNotBefore();
93    }
94   
95  4036 notBefore = vp1.getNotBefore();
96    }
97   
98  4036 if (vp2!=null){
99  4036 Date d2=vp2.getNotBefore();
100  4036 if (notBefore==null || (d2!=null && d2.after(notBefore))){ // choose the latest
101    // if d2 = null, then notBefore is the latest (or if it is also null, null will be reassigned)
102  2339 notBefore=d2;
103    }
104    }
105   
106  4036 return notBefore;
107    }
108   
109    /**
110    * Generally this method returns the earliest notAfter time of the two
111    * ValidityPeriods.
112    *
113    * <p>If either of the ValidityPeriods is a AdjustedValidityPeriod, then the
114    * notAfter time of the result of adjusting one period against the other is
115    * returned. If neither of them is an AdjustedValidityPeriod, then the
116    * earliest notAfter time of the two ValidityPeriods is returned.
117    *
118    * @return the Date specifying the earliest notAfter time of the two periods,
119    * or null, if it is not constrained ("valid forever")
120    */
 
121  4516 toggle public java.util.Date getNotAfter(){
122  4516 Date notAfter=null;
123  4516 if (vp1!=null){
124  4516 if (vp1 instanceof AdjustedValidityPeriod){
125  0 return ((AdjustedValidityPeriod)vp1).adjust(vp2).getNotAfter();
126    }
127   
128  4516 notAfter=vp1.getNotAfter();
129    }
130   
131  4516 if (vp2!=null){
132  4516 Date d2=vp2.getNotAfter();
133   
134  4516 if (notAfter==null || (d2!=null && d2.before(notAfter))){ // choose the earliest
135  2935 notAfter=d2;
136    }
137    }
138   
139  4516 return notAfter;
140    }
141   
142    /**
143    * Generally, this method tests that both ValidityPeriods contain the given
144    * ValidityPeriod (the condition that the intersection of sets contains the
145    * given value).
146    *
147    * <p>This method overrides the inherited behaviour of the contains method to
148    * treat the AdjustedValidityPeriods differently. If neither of the Validity
149    * Periods in the intersection is an AdjustedValidityPeriod, or the given
150    * Credentials is not a ValidityPeriod, the inherited method determines
151    * whether the given Credentials is contained in this ValidityPeriod.
152    *
153    * <p>If either of the ValidityPeriods in the intersection is an
154    * AdjustedValidityPeriod, such
155    * validity period is adjusted against the given ValidityPeriod. The result
156    * of adjustment must still contain the given ValidityPeriod.
157    *
158    * @param c - the Credentials that must be contained in the intersection of
159    * the ValidityPeriods (e.g. another ValidityPeriod or a
160    * SetOfSubsetsCredentials containing multiple ValidityPeriods)
161    *
162    * @return true, if the intersection of the validity periods contains the
163    * given Credentials
164    */
 
165  1555 toggle public boolean contains(issrg.pba.Credentials c){
166  1555 if (!(vp1 instanceof AdjustedValidityPeriod || vp2 instanceof AdjustedValidityPeriod) || !(c instanceof ValidityPeriod)){
167  1555 return super.contains(c);
168    }
169   
170  0 ValidityPeriod vp = (ValidityPeriod)c;
171  0 if (vp1 instanceof AdjustedValidityPeriod){
172  0 vp=((AdjustedValidityPeriod)vp1).adjust(vp);
173    }else{
174  0 if (!vp1.contains(vp)){
175  0 return false;
176    }
177    }
178   
179  0 if (vp2 instanceof AdjustedValidityPeriod){
180  0 vp=((AdjustedValidityPeriod)vp2).adjust(vp);
181    }else{
182  0 if (!vp2.contains(vp)){
183  0 return false;
184    }
185    }
186   
187  0 return vp.contains(c);
188    }
189   
190    /**
191    * This method tells the caller if this intersection of validity periods is
192    * null or not.
193    */
 
194  0 toggle public boolean isNull(){
195  0 Date d=getNotBefore();
196  0 Date d2=getNotAfter();
197   
198  0 return d!=null && d2!=null && d.after(d2);
199    }
200   
 
201  0 toggle public Object clone(){
202  0 return new IntersectionValidityPeriod(vp1==null?null:(ValidityPeriod)(vp1.clone()), vp2==null?null:(ValidityPeriod)(vp2.clone()));
203    }
204   
 
205  1486 toggle public String toString(){
206  1486 return "intersection of "+vp1+" "+vp2;
207    }
208    }
209