1 /*
2  * Copyright (C) 2019 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 android.processor.compat.unsupportedappusage;
18 
19 import com.google.common.base.Splitter;
20 
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 
30 public class CsvReader {
31 
32     private final Splitter mSplitter;
33     private final List<String> mColumns;
34     private final List<Map<String, String>> mContents;
35 
CsvReader(InputStream in)36     public CsvReader(InputStream in) throws IOException {
37         mSplitter = Splitter.on(",");
38         BufferedReader br = new BufferedReader(new InputStreamReader(in));
39         mColumns = mSplitter.splitToList(br.readLine());
40         mContents = new ArrayList<>();
41         String line = br.readLine();
42         while (line != null) {
43             List<String> contents = mSplitter.splitToList(line);
44             Map<String, String> contentMap = new HashMap<>();
45             for (int i = 0; i < Math.min(contents.size(), mColumns.size()); ++i) {
46                 contentMap.put(mColumns.get(i), contents.get(i));
47             }
48             mContents.add(contentMap);
49             line = br.readLine();
50         }
51         br.close();
52     }
53 
getColumns()54     public List<String> getColumns() {
55         return mColumns;
56     }
57 
getContents()58     public List<Map<String, String>> getContents() {
59         return mContents;
60     }
61 }
62