Home | History | Annotate | Download | only in idegen
      1 /*
      2  * Copyright (C) 2012 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  */
     16 
     17 package com.android.idegen;
     18 
     19 import com.google.common.base.Preconditions;
     20 import com.google.common.collect.Maps;
     21 import com.google.common.collect.Sets;
     22 
     23 import java.io.File;
     24 import java.io.IOException;
     25 import java.util.HashMap;
     26 import java.util.Set;
     27 import java.util.logging.Logger;
     28 
     29 /**
     30  * Cache to hold built modules.
     31  */
     32 public class ModuleCache {
     33 
     34     private static final Logger logger = Logger.getLogger(ModuleCache.class.getName());
     35 
     36     private static ModuleCache cache = new ModuleCache();
     37 
     38     ModuleIndexes indexes;
     39 
     40     HashMap<String, Module> modulesByName = Maps.newHashMap();
     41 
     42     private ModuleCache() {
     43     }
     44 
     45     public static ModuleCache getInstance() {
     46         return cache;
     47     }
     48 
     49     public void init(File indexFile) throws IOException {
     50         indexes = new ModuleIndexes(indexFile);
     51         indexes.build();
     52     }
     53 
     54     public Module getAndCache(String moduleName) throws IOException {
     55         Preconditions.checkState(indexes != null, "You must call init() first.");
     56 
     57         Module module = modulesByName.get(moduleName);
     58         if (module == null) {
     59             String makeFile = indexes.getMakeFile(moduleName);
     60             if (makeFile == null) {
     61                 logger.warning("Unable to find make file for module: " + moduleName);
     62             } else {
     63                 module = new StandardModule(moduleName, makeFile);
     64                 module.build();
     65                 modulesByName.put(moduleName, module);
     66             }
     67         }
     68         return module;
     69     }
     70 
     71     public void buildAndCacheAggregatedModule(String moduleName) throws IOException {
     72         if (indexes.isPartOfAggregatedModule(moduleName)) {
     73             Set<String> moduleNames = indexes.getAggregatedModules(moduleName);
     74             Set<Module> modules = Sets.newHashSet();
     75             for (String name : moduleNames) {
     76                 Module m = modulesByName.get(name);
     77                 if (m != null) {
     78                     modules.add(m);
     79                 }
     80             }
     81             String aggregatedName = indexes.getAggregateName(moduleName);
     82             AggregatedModule module = new AggregatedModule(aggregatedName, modules);
     83             module.build();
     84             modulesByName.put(aggregatedName, module);
     85         }
     86     }
     87 
     88     public Iterable<Module> getModules() {
     89         return modulesByName.values();
     90     }
     91 
     92     public String getMakeFile(String moduleName) {
     93         return indexes.getMakeFile(moduleName);
     94     }
     95 
     96     public void put(StandardModule module) {
     97         Preconditions.checkNotNull(module);
     98         modulesByName.put(module.getName(), module);
     99     }
    100 
    101     public String getAggregateReplacementName(String moduleName) {
    102         if (indexes.isPartOfAggregatedModule(moduleName)) {
    103             return indexes.getAggregateName(moduleName);
    104         }
    105         return null;
    106     }
    107 }
    108