src/share/classes/com/sun/tools/javac/file/ZipFileIndexCache.java

Thu, 02 Aug 2012 18:24:01 +0100

author
mcimadamore
date
Thu, 02 Aug 2012 18:24:01 +0100
changeset 1298
2d75e7c952b8
parent 0
959103a6100f
permissions
-rw-r--r--

7187104: Inference cleanup: remove redundant exception classes in Infer.java
Summary: Remove unused exception classes in Infer.java
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.javac.file;
    28 import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
    29 import com.sun.tools.javac.util.Context;
    30 import java.io.File;
    31 import java.io.IOException;
    32 import java.util.ArrayList;
    33 import java.util.HashMap;
    34 import java.util.Iterator;
    35 import java.util.List;
    36 import java.util.Map;
    39 /** A cache for ZipFileIndex objects. */
    40 public class ZipFileIndexCache {
    42     private final Map<File, ZipFileIndex> map =
    43             new HashMap<File, ZipFileIndex>();
    45     /** Get a shared instance of the cache. */
    46     private static ZipFileIndexCache sharedInstance;
    47     public synchronized static ZipFileIndexCache getSharedInstance() {
    48         if (sharedInstance == null)
    49             sharedInstance = new ZipFileIndexCache();
    50         return sharedInstance;
    51     }
    53     /** Get a context-specific instance of a cache. */
    54     public static ZipFileIndexCache instance(Context context) {
    55         ZipFileIndexCache instance = context.get(ZipFileIndexCache.class);
    56         if (instance == null)
    57             context.put(ZipFileIndexCache.class, instance = new ZipFileIndexCache());
    58         return instance;
    59     }
    61     /**
    62      * Returns a list of all ZipFileIndex entries
    63      *
    64      * @return A list of ZipFileIndex entries, or an empty list
    65      */
    66     public List<ZipFileIndex> getZipFileIndexes() {
    67         return getZipFileIndexes(false);
    68     }
    70     /**
    71      * Returns a list of all ZipFileIndex entries
    72      *
    73      * @param openedOnly If true it returns a list of only opened ZipFileIndex entries, otherwise
    74      *                   all ZipFileEntry(s) are included into the list.
    75      * @return A list of ZipFileIndex entries, or an empty list
    76      */
    77     public synchronized List<ZipFileIndex> getZipFileIndexes(boolean openedOnly) {
    78         List<ZipFileIndex> zipFileIndexes = new ArrayList<ZipFileIndex>();
    80         zipFileIndexes.addAll(map.values());
    82         if (openedOnly) {
    83             for(ZipFileIndex elem : zipFileIndexes) {
    84                 if (!elem.isOpen()) {
    85                     zipFileIndexes.remove(elem);
    86                 }
    87             }
    88         }
    90         return zipFileIndexes;
    91     }
    93     public synchronized ZipFileIndex getZipFileIndex(File zipFile,
    94             RelativeDirectory symbolFilePrefix,
    95             boolean useCache, String cacheLocation,
    96             boolean writeIndex) throws IOException {
    97         ZipFileIndex zi = getExistingZipIndex(zipFile);
    99         if (zi == null || (zi != null && zipFile.lastModified() != zi.zipFileLastModified)) {
   100             zi = new ZipFileIndex(zipFile, symbolFilePrefix, writeIndex,
   101                     useCache, cacheLocation);
   102             map.put(zipFile, zi);
   103         }
   104         return zi;
   105     }
   107     public synchronized ZipFileIndex getExistingZipIndex(File zipFile) {
   108         return map.get(zipFile);
   109     }
   111     public synchronized void clearCache() {
   112         map.clear();
   113     }
   115     public synchronized void clearCache(long timeNotUsed) {
   116         Iterator<File> cachedFileIterator = map.keySet().iterator();
   117         while (cachedFileIterator.hasNext()) {
   118             File cachedFile = cachedFileIterator.next();
   119             ZipFileIndex cachedZipIndex = map.get(cachedFile);
   120             if (cachedZipIndex != null) {
   121                 long timeToTest = cachedZipIndex.lastReferenceTimeStamp + timeNotUsed;
   122                 if (timeToTest < cachedZipIndex.lastReferenceTimeStamp || // Overflow...
   123                         System.currentTimeMillis() > timeToTest) {
   124                     map.remove(cachedFile);
   125                 }
   126             }
   127         }
   128     }
   130     public synchronized void removeFromCache(File file) {
   131         map.remove(file);
   132     }
   134     /** Sets already opened list of ZipFileIndexes from an outside client
   135       * of the compiler. This functionality should be used in a non-batch clients of the compiler.
   136       */
   137     public synchronized void setOpenedIndexes(List<ZipFileIndex>indexes) throws IllegalStateException {
   138         if (map.isEmpty()) {
   139             String msg =
   140                     "Setting opened indexes should be called only when the ZipFileCache is empty. "
   141                     + "Call JavacFileManager.flush() before calling this method.";
   142             throw new IllegalStateException(msg);
   143         }
   145         for (ZipFileIndex zfi : indexes) {
   146             map.put(zfi.zipFile, zfi);
   147         }
   148     }
   149 }

mercurial