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 import com.google.common.io.Files;
     23 import com.google.common.io.LineProcessor;
     24 
     25 import java.io.File;
     26 import java.io.IOException;
     27 import java.nio.charset.Charset;
     28 import java.util.HashMap;
     29 import java.util.Set;
     30 import java.util.logging.Level;
     31 import java.util.logging.Logger;
     32 
     33 /**
     34  * Mapping of module names to make files.
     35  */
     36 public class ModuleIndexes {
     37 
     38     private static final Logger logger = Logger.getLogger(ModuleIndexes.class.getName());
     39 
     40     private File indexFile;
     41     private HashMap<String, String> moduleNameToMakeFileMap;
     42     private HashMap<String, Set<String>> makeFileToModuleNamesMap;
     43 
     44     public ModuleIndexes(File indexFile) {
     45         this.indexFile = indexFile;
     46     }
     47 
     48     public void build() throws IOException {
     49 
     50         moduleNameToMakeFileMap = Maps.newHashMap();
     51         makeFileToModuleNamesMap = Maps.newHashMap();
     52         logger.info("Building index from " + indexFile.getCanonicalPath());
     53         Files.readLines(indexFile, Charset.forName("UTF-8"),
     54                 new LineProcessor<Object>() {
     55                     int count = 0;
     56 
     57                     @Override
     58                     public boolean processLine(String line) throws IOException {
     59                         count++;
     60                         String[] arr = line.split(":");
     61                         if (arr.length < 2) {
     62                             logger.log(Level.WARNING,
     63                                     "Ignoring index line " + count + ". Bad format: " + line);
     64                         } else {
     65                             String makeFile = arr[0];
     66                             String moduleName = arr[1];
     67                             moduleNameToMakeFileMap.put(moduleName, makeFile);
     68                             append(makeFile, moduleName);
     69                         }
     70                         return true;
     71                     }
     72 
     73                     @Override
     74                     public Object getResult() {
     75                         return null;
     76                     }
     77                 });
     78     }
     79 
     80     private void append(String makeFile, String moduleName) {
     81         Set<String> moduleNames = makeFileToModuleNamesMap.get(makeFile);
     82         if (moduleNames == null) {
     83             moduleNames = Sets.newHashSet();
     84             makeFileToModuleNamesMap.put(makeFile, moduleNames);
     85         } else {
     86             // Create a aggregate module place holder.
     87             //moduleNameToMakeFileMap.put(getAggregateName(moduleDir), makeFile);
     88         }
     89         moduleNames.add(moduleName);
     90     }
     91 
     92     public String getMakeFile(String moduleName) {
     93         Preconditions.checkState(moduleNameToMakeFileMap != null,
     94                 "Index not built. Call build() first.");
     95 
     96         return moduleNameToMakeFileMap.get(moduleName);
     97     }
     98 }
     99