KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > validity > DelayedValidity


1 /*
2  * Copyright 2005 The Apache Software Foundation.
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.apache.cocoon.components.source.impl.validity;
17
18 import org.apache.excalibur.source.SourceValidity;
19
20 /**
21  * Delays validity check for a specified interval.
22  *
23  * <p>
24  * This is wrapper validity which can be used to reduce count of
25  * filesystem (or network) accesses just to check the source
26  * validity.
27  *
28  * @since 2.1.8
29  * @version $Id: DelayedValidity.java 312968 2005-10-11 22:28:46Z vgritsenko $
30  */

31 public class DelayedValidity implements SourceValidity {
32
33     private long delay;
34     private long expires;
35
36     private SourceValidity delegate;
37
38
39     public DelayedValidity(long delay, SourceValidity validity) {
40         this.delay = delay;
41         this.expires = System.currentTimeMillis() + delay;
42         this.delegate = validity;
43     }
44
45     public int isValid() {
46         final long currentTime = System.currentTimeMillis();
47         if (currentTime <= this.expires) {
48             // The delay has not passed yet - assuming source is valid.
49
return SourceValidity.VALID;
50         }
51
52         // The delay has passed, prepare for the next interval.
53
this.expires = currentTime + this.delay;
54
55         return this.delegate.isValid();
56     }
57
58     public int isValid(SourceValidity newValidity) {
59         // Always delegate
60
return this.delegate.isValid(newValidity);
61     }
62 }
63
Popular Tags