com.davidthomasbernal.stardict.dictionary.DictionaryIndex Maven / Gradle / Ivy
The newest version!
package com.davidthomasbernal.stardict.dictionary;
import java.util.*;
import java.util.logging.Logger;
public class DictionaryIndex {
private final List indexFileEntries;
private Logger logger = Logger.getLogger(this.getClass().getName());
// Updated also with indexFileEntries frm the synonyms file, if present.
private final Map> entryMap;
public DictionaryIndex(List indexFileEntries) {
this.indexFileEntries = new ArrayList(indexFileEntries);
entryMap = new HashMap>(this.indexFileEntries.size());
addToIndex(indexFileEntries);
}
public Set getWords() {
return entryMap.keySet();
}
public List getIndexFileEntries() {
return Collections.unmodifiableList(indexFileEntries);
}
public IndexEntry getIndexFileEntry(int index) {
return indexFileEntries.get(index);
}
public boolean containsWord(String searchWord) {
return entryMap.containsKey(searchWord);
}
public Set getIndexFileEntries(String word) {
if (entryMap.containsKey(word)) {
return entryMap.get(word);
} else {
return Collections.emptySet();
}
}
public void addToIndex(Collection entries) {
for (IndexEntry entry : entries) {
for (String word: entry.words) {
String indexWord = word.toLowerCase();
entryMap.putIfAbsent(indexWord, new LinkedHashSet<>());
entryMap.get(indexWord).add(entry);
}
}
}
}