blob: 20c908a772db2f23e30ff533059e0f6e88800f61 [file] [log] [blame]
Brett Chabote8062c12011-01-22 13:30:17 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
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 */
Brett Chabotbeaebb02011-02-07 16:16:06 -080016package com.android.tradefed.build;
Brett Chabote8062c12011-01-22 13:30:17 -080017
18import java.io.File;
19import java.util.Collections;
20import java.util.HashMap;
21import java.util.Map;
22
23/**
24 * A factory for creating {@link FileDownloadCache}
25 */
26public class FileDownloadCacheFactory {
27
28 // use the "singleton inner class" pattern
29 // http://en.wikipedia.org/wiki/Singleton_pattern#The_solution_of_Bill_Pugh
30 private static class SingletonHolder {
31 public static final FileDownloadCacheFactory INSTANCE = new FileDownloadCacheFactory();
32 }
33
34 private Map<String, FileDownloadCache> mCacheObjectMap = Collections.synchronizedMap(
35 new HashMap<String, FileDownloadCache>());
36
37 /**
38 * Get the singleton instance of FileDownloadCacheFactory
Brett Chabote8062c12011-01-22 13:30:17 -080039 */
40 public static FileDownloadCacheFactory getInstance() {
41 return SingletonHolder.INSTANCE;
42 }
43
44 /**
45 * Retrieve the {@link FileDownloadCache} with the given cache directory, creating if necessary.
46 * <p/>
47 * Note that the cache assumes that this process has exclusive access to the <var>cacheDir</var>
48 * directory. If multiple TF processes will be run on the same machine, they MUST each use
49 * unique cache directories.
50 *
51 * @param cacheDir the local filesystem directory to use as a cache
52 * @return the {@link FileDownloadCache} for given cacheDir
53 */
Brett Chabot0b9e62c2011-02-01 20:28:22 -080054 public synchronized FileDownloadCache getCache(File cacheDir) {
Brett Chabote8062c12011-01-22 13:30:17 -080055 FileDownloadCache cache = mCacheObjectMap.get(cacheDir.getAbsolutePath());
56 if (cache == null) {
57 cache = new FileDownloadCache(cacheDir);
Brett Chabot6b70f872011-02-01 23:08:17 -080058 mCacheObjectMap.put(cacheDir.getAbsolutePath(), cache);
Brett Chabote8062c12011-01-22 13:30:17 -080059 }
60 return cache;
61 }
62}