Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TimedReloadContext |
|
| 2.0;2 |
1 | /* | |
2 | * Copyright (C) 1998-2001 Semiotek Inc. All Rights Reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted under the terms of either of the following | |
6 | * Open Source licenses: | |
7 | * | |
8 | * The GNU General Public License, version 2, or any later version, as | |
9 | * published by the Free Software Foundation | |
10 | * (http://www.fsf.org/copyleft/gpl.html); | |
11 | * | |
12 | * or | |
13 | * | |
14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
15 | * | |
16 | * This software is provided "as is", with NO WARRANTY, not even the | |
17 | * implied warranties of fitness to purpose, or merchantability. You | |
18 | * assume all risks and liabilities associated with its use. | |
19 | * | |
20 | * See www.webmacro.org for more information on the WebMacro project. | |
21 | */ | |
22 | ||
23 | ||
24 | package org.webmacro.resource; | |
25 | ||
26 | import org.webmacro.util.Clock; | |
27 | ||
28 | /** | |
29 | * TimedReloadContext acts as an Decorator for Reload context to support | |
30 | * cache resources that are expensive to check for change. An example is a | |
31 | * resource fetch via the network, but could also be a file. | |
32 | * It is constructed with a reference to another CacheReloadContext and an interval. | |
33 | * Its shouldReload() method will pass calls to the referenced CacheReloadContext, but | |
34 | * will take sure, that this is done only once in the specified interval. In all other | |
35 | * cases it will return "false", to indicate, that the resource should not be reloaded. | |
36 | * @since 0.96 | |
37 | * @author skanthak@muehlheim.de | |
38 | **/ | |
39 | ||
40 | public class TimedReloadContext extends CacheReloadContext | |
41 | { | |
42 | ||
43 | private CacheReloadContext reloadContext; | |
44 | private long nextCheck; | |
45 | private long checkInterval; | |
46 | ||
47 | /** | |
48 | * Construct a new TimedReloadContext decorator. | |
49 | * This is just a wrapper object for another CacheReloadContext, ensuring | |
50 | * that the shouldReload() method of the refrenced reload context is only | |
51 | * called once per checkInterval milliseconds. | |
52 | * @param reloadContext reload context to wrap around | |
53 | * @param checkInterval interval to check for reload at most in milliseconds | |
54 | **/ | |
55 | public TimedReloadContext (CacheReloadContext reloadContext, long checkInterval) | |
56 | { | |
57 | 0 | super(); |
58 | 0 | this.reloadContext = reloadContext; |
59 | 0 | this.checkInterval = checkInterval; |
60 | 0 | this.nextCheck = Clock.TIME + checkInterval; |
61 | 0 | } |
62 | ||
63 | /** | |
64 | * Check, whether the underlying resource should be reloaded. | |
65 | * This method will simply call the shouldReload() method of the | |
66 | * referenced reload context, except when this method was called | |
67 | * again in the last checkInterval milliseconds. In this case, | |
68 | * this method will simply return false. | |
69 | * @return whether resource should be reloaded. | |
70 | **/ | |
71 | public boolean shouldReload () | |
72 | { | |
73 | //long time = System.currentTimeMillis(); | |
74 | 0 | if (Clock.TIME >= nextCheck) |
75 | { | |
76 | 0 | nextCheck = Clock.TIME + checkInterval; |
77 | 0 | return reloadContext.shouldReload(); |
78 | } | |
79 | else | |
80 | { | |
81 | 0 | return false; |
82 | } | |
83 | } | |
84 | } |