RelativeValidityPeriod | Line # 47 | 41 | 7 | 61% |
0.6101695
|
No Tests | |||
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 | import java.util.GregorianCalendar; | |
36 | ||
37 | /** | |
38 | * This is a class representing a ValidityPeriod relative to the current time. | |
39 | * Its notBefore and notAfter time depend on the time of evaluation and are | |
40 | * specified as an offset from the current time. | |
41 | * | |
42 | * <p>A positive offset to the notBefore end of the period means it is in the | |
43 | * past from now (and a negative means it is in the future from now). A | |
44 | * positive offset to the notAfter end of the period means it is in the future | |
45 | * from now (and a negative means it is in the past from now). | |
46 | */ | |
47 | public class RelativeValidityPeriod extends DefaultValidityPeriodBehaviour { | |
48 | ||
49 | /** | |
50 | * This is the actual clock used for calculating relative periods. At construction time it is | |
51 | * copied from the CustomisePERMIS.systemClock variable. | |
52 | */ | |
53 | protected Clock theClock = CustomisePERMIS.getSystemClock(); | |
54 | ||
55 | private GregorianCalendar gc = new GregorianCalendar(); | |
56 | ||
57 | protected boolean unlimited=false; // i.e. only one side of the period is defined (see constructors with notAfter boolean) | |
58 | protected boolean notAfter; // in that case this variable decides the limited edge | |
59 | protected RelativeDate nb; | |
60 | protected RelativeDate na; | |
61 | ||
62 | 0 | protected RelativeValidityPeriod() { |
63 | } | |
64 | ||
65 | /** | |
66 | * The integers specify how many years, months, etc to add (you can use | |
67 | * negative values as well) to | |
68 | * Now to get notBefore or notAfter date. If notAfter flag is true, the | |
69 | * integers will specify a notAfter; | |
70 | * otherwise it specifies notBefore. The other end of the period is infinite. | |
71 | * | |
72 | * @param years - the offset in years | |
73 | * @param months - the offset in months | |
74 | * @param days - the offset in days | |
75 | * @param hours - the offset in hours | |
76 | * @param minutes - the offset in minutes | |
77 | * @param seconds - the offset in seconds | |
78 | * @param notAfter - the flag specifying whether this is the offset for | |
79 | * notAfter or notBefore; if true, the specified offset is for notAfter, | |
80 | * otherwise for notBefore; the other end of the period is infinite. | |
81 | */ | |
82 | 0 | public RelativeValidityPeriod(int years, int months, int days, int hours, int minutes, int seconds, boolean notAfter){ |
83 | 0 | unlimited=true; |
84 | 0 | this.notAfter=notAfter; |
85 | ||
86 | // note that the values are assigned to both notBefore and notAfter, but they will be used only | |
87 | // as notAfter flag specifies. It is done this way to avoid redundant ifs | |
88 | 0 | nb = na = new RelativeDate(years, months, days, hours, minutes, seconds); |
89 | } | |
90 | ||
91 | /** | |
92 | * This constructor builds the object using a reference to a ready to use | |
93 | * RelativeDate object. | |
94 | * The meaning of the notAfter flag is the same, as in | |
95 | * RelativeValidityPeriod(int,int,int,int,int,int,boolean) | |
96 | * constructor. | |
97 | * | |
98 | * @param rd - the RelativeDate | |
99 | * @param notAfter - the flag telling if it is the RelativeDate of the | |
100 | * notAfter or notBefore | |
101 | */ | |
102 | 0 | public RelativeValidityPeriod(RelativeDate rd, boolean notAfter){ |
103 | 0 | unlimited=true; |
104 | 0 | this.notAfter=notAfter; |
105 | ||
106 | 0 | nb=na=rd; |
107 | } | |
108 | ||
109 | /** | |
110 | * This constructor lets build a RelativeValidityPeriod by specifying offsets | |
111 | * for both the notBefore and notAfter ends explicitly. All nb* parameters | |
112 | * specify the notBefore offset, all na* parameters specify the notAfter | |
113 | * offset. | |
114 | * | |
115 | */ | |
116 | 35 | public RelativeValidityPeriod(int nbYears, int nbMonths, int nbDays, int nbHours, int nbMinutes, int nbSeconds, |
117 | int naYears, int naMonths, int naDays, int naHours, int naMinutes, int naSeconds){ | |
118 | 35 | nb = new RelativeDate(nbYears, nbMonths, nbDays, nbHours, nbMinutes, nbSeconds); |
119 | 35 | na = new RelativeDate(naYears, naMonths, naDays, naHours, naMinutes, naSeconds); |
120 | } | |
121 | ||
122 | /** | |
123 | * This constructor specifies a RelativeValidityPeriod with the given | |
124 | * RelativeDates of the notBefore and notAfter ends. | |
125 | * | |
126 | * @param notBefore - the RelativeDate of the notBefore end of the period | |
127 | * @param notAfter - the RelativeDate of the notAfter end of the period | |
128 | */ | |
129 | 0 | public RelativeValidityPeriod(RelativeDate notBefore, RelativeDate notAfter){ |
130 | 0 | nb=notBefore; |
131 | 0 | na=notAfter; |
132 | } | |
133 | ||
134 | /** | |
135 | * This method returns the notBefore time offset from current time (as | |
136 | * provided by the Clock). The offset is subtracted from now, i.e. the | |
137 | * positive offset moves the end into the past. | |
138 | * | |
139 | * @return the absolute Date computed from now, or null, if the validity | |
140 | * period is unlimited on this end (lasts since the time immemorial) | |
141 | */ | |
142 | 1600 | public Date getNotBefore(){ |
143 | 1600 | if (unlimited && notAfter){ |
144 | 0 | return null; |
145 | } | |
146 | ||
147 | 1600 | synchronized(gc){ |
148 | 1600 | gc.setTime(getNow()); |
149 | 1600 | gc.add(gc.YEAR, -nb.years); |
150 | 1600 | gc.add(gc.MONTH, -nb.months); |
151 | 1600 | gc.add(gc.DATE, -nb.days); |
152 | 1600 | gc.add(gc.HOUR, -nb.hours); |
153 | 1600 | gc.add(gc.MINUTE, -nb.minutes); |
154 | 1600 | gc.add(gc.SECOND, -nb.seconds); |
155 | ||
156 | 1600 | return gc.getTime(); |
157 | } | |
158 | } | |
159 | ||
160 | /** | |
161 | * This method returns the notAfter time offset from current time (as | |
162 | * provided by the Clock). The offset is added to now, i.e. the | |
163 | * positive offset moves the end into the future. | |
164 | * | |
165 | * @return the absolute Date computed from now, or null, if the validity | |
166 | * period is unlimited on this end (lasts forever) | |
167 | */ | |
168 | 1840 | public Date getNotAfter(){ |
169 | 1840 | if (unlimited && !notAfter){ |
170 | 0 | return null; |
171 | } | |
172 | ||
173 | 1840 | synchronized(gc){ |
174 | 1840 | gc.setTime(getNow()); |
175 | 1840 | gc.add(gc.YEAR, na.years); |
176 | 1840 | gc.add(gc.MONTH, na.months); |
177 | 1840 | gc.add(gc.DATE, na.days); |
178 | 1840 | gc.add(gc.HOUR, na.hours); |
179 | 1840 | gc.add(gc.MINUTE, na.minutes); |
180 | 1840 | gc.add(gc.SECOND, na.seconds); |
181 | ||
182 | 1840 | return gc.getTime(); |
183 | } | |
184 | } | |
185 | ||
186 | /** | |
187 | * This is a utility method that returns the Date corresponding to the | |
188 | * current time returned by the Clock. | |
189 | */ | |
190 | 3440 | private java.util.Date getNow(){ |
191 | 3440 | java.util.Date tm = theClock.getTime(); |
192 | 3440 | long l = tm.getTime(); |
193 | 3440 | tm.setTime(l - (l % 1000)); // removing milliseconds; we are not that precise |
194 | 3440 | return tm; |
195 | } | |
196 | ||
197 | 0 | public Object clone(){ |
198 | 0 | RelativeValidityPeriod rp = new RelativeValidityPeriod(nb, na); |
199 | 0 | rp.unlimited=unlimited; |
200 | 0 | rp.notAfter=notAfter; |
201 | ||
202 | 0 | return rp; |
203 | } | |
204 | ||
205 | 4628 | public String toString(){ |
206 | 4628 | return "relative (" |
207 | 4628 | +((unlimited && notAfter)?"-oo":nb.toString()) |
208 | +"; " | |
209 | 4628 | +((unlimited && !notAfter)?"+oo":na.toString()) |
210 | +")"; | |
211 | } | |
212 | } |
|