2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
7 package gov
.nasa
.worldwind
;
13 public class AbsentResourceList
15 // Absent resources: A resource is deemed absent if a specified maximum number of attempts have been made to retrieve it.
16 // Retrieval attempts are governed by a minimum time interval between successive attempts. If an attempt is made
17 // within this interval, the resource is still deemed to be absent until the interval expires.
18 private static final int DEFAULT_MAX_ABSENT_RESOURCE_TRIES
= 2;
19 private static final int DEFAULT_MIN_ABSENT_RESOURCE_CHECK_INTERVAL
= 10000;
21 private int maxTries
= DEFAULT_MAX_ABSENT_RESOURCE_TRIES
;
22 private int minCheckInterval
= DEFAULT_MIN_ABSENT_RESOURCE_CHECK_INTERVAL
;
24 private static class AbsentResoureEntry
26 long timeOfLastMark
; // meant to be the time of the most recent attempt to find the resource
30 private final java
.util
.concurrent
.ConcurrentHashMap
<Long
, AbsentResoureEntry
> possiblyAbsent
=
31 new java
.util
.concurrent
.ConcurrentHashMap
<Long
, AbsentResoureEntry
>();
33 private java
.util
.SortedSet
<Long
> definitelyAbsent
= java
.util
.Collections
.synchronizedSortedSet(
34 new java
.util
.TreeSet
<Long
>());
36 public AbsentResourceList()
40 public AbsentResourceList(int maxTries
, int minCheckInterval
)
42 this.maxTries
= Math
.max(maxTries
, 1);
43 this.minCheckInterval
= Math
.max(minCheckInterval
, 500);
46 public final void markResourceAbsent(long resourceID
)
48 if (this.definitelyAbsent
.contains(resourceID
))
51 AbsentResoureEntry entry
= this.possiblyAbsent
.get(resourceID
);
53 this.possiblyAbsent
.put(resourceID
, entry
= new AbsentResoureEntry());
56 entry
.timeOfLastMark
= System
.currentTimeMillis();
58 if (entry
.numTries
>= this.maxTries
)
60 this.definitelyAbsent
.add(resourceID
);
61 this.possiblyAbsent
.remove(resourceID
);
62 // entry can now be garbage collected
66 public final boolean isResourceAbsent(long resourceID
)
68 if (this.definitelyAbsent
.contains(resourceID
))
71 AbsentResoureEntry entry
= this.possiblyAbsent
.get(resourceID
);
72 //noinspection SimplifiableIfStatement
76 return (System
.currentTimeMillis() - entry
.timeOfLastMark
) < this.minCheckInterval
;
79 public final void unmarkResourceAbsent(long resourceID
)
81 this.definitelyAbsent
.remove(resourceID
);
82 this.possiblyAbsent
.remove(resourceID
);