1 /*
2  * Copyright (c) 2008-2019 Emmanuel Dupuy.
3  * This project is distributed under the GPLv3 license.
4  * This is a Copyleft license that gives the user the right to use,
5  * copy and modify the code freely for non-commercial purposes.
6  */
7 
8 package org.jd.gui.util.index;
9 
10 import org.jd.gui.api.model.Container;
11 import org.jd.gui.api.model.Indexes;
12 import org.jd.gui.util.exception.ExceptionUtil;
13 
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.concurrent.Future;
19 
20 public class IndexesUtil {
containsInternalTypeName(Collection<Future<Indexes>> collectionOfFutureIndexes, String internalTypeName)21     public static boolean containsInternalTypeName(Collection<Future<Indexes>> collectionOfFutureIndexes, String internalTypeName) {
22         return contains(collectionOfFutureIndexes, "typeDeclarations", internalTypeName);
23     }
24 
25     @SuppressWarnings("unchecked")
findInternalTypeName(Collection<Future<Indexes>> collectionOfFutureIndexes, String internalTypeName)26     public static List<Container.Entry> findInternalTypeName(Collection<Future<Indexes>> collectionOfFutureIndexes, String internalTypeName) {
27         return find(collectionOfFutureIndexes, "typeDeclarations", internalTypeName);
28     }
29 
contains(Collection<Future<Indexes>> collectionOfFutureIndexes, String indexName, String key)30     public static boolean contains(Collection<Future<Indexes>> collectionOfFutureIndexes, String indexName, String key) {
31         try {
32             for (Future<Indexes> futureIndexes : collectionOfFutureIndexes) {
33                 if (futureIndexes.isDone()) {
34                     Map<String, Collection> index = futureIndexes.get().getIndex(indexName);
35                     if ((index != null) && (index.get(key) != null)) {
36                         return true;
37                     }
38                 }
39             }
40         } catch (Exception e) {
41             assert ExceptionUtil.printStackTrace(e);
42         }
43 
44         return false;
45     }
46 
47     @SuppressWarnings("unchecked")
find(Collection<Future<Indexes>> collectionOfFutureIndexes, String indexName, String key)48     public static List<Container.Entry> find(Collection<Future<Indexes>> collectionOfFutureIndexes, String indexName, String key) {
49         ArrayList<Container.Entry> entries = new ArrayList<>();
50 
51         try {
52             for (Future<Indexes> futureIndexes : collectionOfFutureIndexes) {
53                 if (futureIndexes.isDone()) {
54                     Map<String, Collection> index = futureIndexes.get().getIndex(indexName);
55                     if (index != null) {
56                         Collection<Container.Entry> collection = index.get(key);
57                         if (collection != null) {
58                             entries.addAll(collection);
59                         }
60                     }
61                 }
62             }
63         } catch (Exception e) {
64             assert ExceptionUtil.printStackTrace(e);
65         }
66 
67         return entries;
68     }
69 }
70