View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  package org.esigate.esi;
16  
17  import java.util.Date;
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  final class InlineCache {
22      private static final Map<String, InlineCache> CACHE = new HashMap<>();
23  
24      private final Date outdate;
25      private final boolean fetchable;
26      private final String originalUrl;
27      private final String fragment;
28  
29      public static void storeFragment(String uri, Date outdate, boolean fetchable, String originalUrl, String fragment) {
30          InlineCache ic = new InlineCache(outdate, fetchable, originalUrl, fragment);
31          CACHE.put(uri, ic);
32      }
33  
34      public static InlineCache getFragment(String uri) {
35          return CACHE.get(uri);
36      }
37  
38      private InlineCache(Date outdate, boolean fetchable, String originalUrl, String fragment) {
39          this.outdate = outdate;
40          this.fetchable = fetchable;
41          this.originalUrl = originalUrl;
42          this.fragment = fragment;
43      }
44  
45      public boolean isExpired() {
46          return (outdate != null) && (outdate.getTime() < System.currentTimeMillis());
47      }
48  
49      public Date getOutdate() {
50          return outdate;
51      }
52  
53      public boolean isFetchable() {
54          return fetchable;
55      }
56  
57      public String getOriginalUrl() {
58          return originalUrl;
59      }
60  
61      public String getFragment() {
62          return fragment;
63      }
64  
65  }