xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/util/FileExtraZipper.java (revision 9368fcecb2b7ea70174f734141303ebf227f2737)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2017, 2020, Chris Fraire <cfraire@me.com>.
22  */
23 package org.opengrok.indexer.util;
24 
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.stream.Collectors;
31 
32 import org.opengrok.indexer.analysis.NullableNumLinesLOC;
33 import org.opengrok.indexer.search.DirectoryEntry;
34 
35 /**
36  * Represents a transformer of lists of files and
37  * {@link NullableNumLinesLOC} instances to zip them into a list of
38  * {@link DirectoryEntry} instances.
39  */
40 public class FileExtraZipper {
41 
42     /**
43      * Merge the specified lists by looking up a possible entry in
44      * {@code extras} for every element in {@code files}.
45      * @param dir the files' directory
46      * @param files the file names
47      * @param extras some OpenGrok-analyzed extra metadata
48      * @return a list of the same size as {@code files}
49      */
zip(File dir, List<String> files, List<NullableNumLinesLOC> extras)50     public List<DirectoryEntry> zip(File dir, List<String> files, List<NullableNumLinesLOC> extras) {
51 
52         if (extras == null) {
53             return files.stream().map(f ->
54                 new DirectoryEntry(new File(dir, f))).collect(
55                 Collectors.toList());
56         }
57 
58         Map<String, NullableNumLinesLOC> byName = indexExtraByName(extras);
59 
60         List<DirectoryEntry> result = new ArrayList<>(files.size());
61         for (String file : files) {
62             File fileobj = new File(dir, file);
63             NullableNumLinesLOC extra = findExtra(byName, fileobj);
64             DirectoryEntry entry = new DirectoryEntry(fileobj, extra);
65             result.add(entry);
66         }
67 
68         return result;
69     }
70 
findExtra(Map<String, NullableNumLinesLOC> byName, File fileobj)71     private NullableNumLinesLOC findExtra(Map<String, NullableNumLinesLOC> byName, File fileobj) {
72         String key = fileobj.getName();
73         return byName.get(key);
74     }
75 
indexExtraByName(List<NullableNumLinesLOC> extras)76     private Map<String, NullableNumLinesLOC> indexExtraByName(List<NullableNumLinesLOC> extras) {
77         Map<String, NullableNumLinesLOC> byPath = new HashMap<>();
78         for (NullableNumLinesLOC extra : extras) {
79             if (extra.getPath() != null) {
80                 File f = new File(extra.getPath());
81                 String filename = f.getName();
82                 byPath.put(filename, extra);
83             }
84         }
85         return byPath;
86     }
87 }
88