Debian Codegraph @

bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/update/ConfigurationCache.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.equinox.internal.p2.update;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 /**
22 * TODO ensure thread safety
23 *
24 */
25 public class ConfigurationCache {
26 private static Map<String, CacheEntry> cache = new HashMap<>();
27
28 // class used to represent cache values
29 static class CacheEntry {
30 long timestamp;
31 Configuration config;
32 }
33
34 // helper method to convert the file to a cache key. convert to an absolute
35 // path to ensure equality between relative and absolute comparisons
36 private static String toKey(File file) {
37 try {
38 return file.getCanonicalPath();
39 } catch (IOException e) {
40 // ignore and return the absolute value instead
41 }
42 return file.getAbsolutePath();
43 }
44
45 /*
46 * Return the configuration object in the cache which is represented by the
47 * given file. Do a check on disk to see if the cache is up-to-date. If not,
48 * then treat it as a cache miss.
49 */
50 public static Configuration get(File file) {
51 String key = toKey(file);
52 CacheEntry entry = cache.get(key);
53 if (entry == null)
54 return null;
55 return file.lastModified() == entry.timestamp ? entry.config : null;
56 }
57
58 /*
59 * Store the given configuration in the cache.
60 */
61 public static void put(File file, Configuration config) {
62 String key = toKey(file);
63 if (config == null) {
64 cache.remove(key);
65 return;
66 }
67 CacheEntry entry = new CacheEntry();
68 entry.config = config;
69 entry.timestamp = file.lastModified();
70 cache.put(key, entry);
71 }
72
73 }