org.protege.editor.owl.model.search.SearchResultSet Maven / Gradle / Ivy
package org.protege.editor.owl.model.search;
import java.util.*;
/**
* Author: Matthew Horridge
* Stanford University
* Bio-Medical Informatics Research Group
* Date: 19/09/2012
*/
public class SearchResultSet {
private List searchResults;
private Map> searchResultsByCategory;
public SearchResultSet(Collection searchResults) {
this.searchResults = new ArrayList<>(searchResults);
buildCatResults();
}
public int getCategoryResultsCount(String cat) {
SearchCategoryGroupKey key = getKeyForCategory(cat);
List catResults = searchResultsByCategory.get(key);
if (catResults == null) {
return 0;
}
else {
return catResults.size();
}
}
public List getCategories() {
List result = new ArrayList<>();
for (SearchCategoryGroupKey searchCategoryGroupKey : new TreeSet<>(searchResultsByCategory.keySet())) {
result.add(searchCategoryGroupKey.groupDescription);
}
return result;
}
public List getCategoryResults(String category) {
SearchCategoryGroupKey key = getKeyForCategory(category);
List catResults = searchResultsByCategory.get(key);
if (catResults == null) {
return Collections.emptyList();
}
return new ArrayList<>(catResults);
}
public List getCategoryResults(String category, int limit) {
List trimmedResult = new ArrayList<>();
SearchCategoryGroupKey key = getKeyForCategory(category);
if (key == null) {
return Collections.emptyList();
}
List catResults = searchResultsByCategory.get(key);
if (catResults == null) {
return Collections.emptyList();
}
List trimmedCatResults;
if (catResults.size() > limit) {
trimmedCatResults = catResults.subList(0, limit);
}
else {
trimmedCatResults = catResults;
}
trimmedResult.addAll(trimmedCatResults);
return trimmedResult;
}
private SearchCategoryGroupKey getKeyForCategory(String cat) {
for (SearchCategoryGroupKey searchCategoryGroupKey : searchResultsByCategory.keySet()) {
if (searchCategoryGroupKey.groupDescription.equals(cat)) {
return searchCategoryGroupKey;
}
}
return null;
}
private void buildCatResults() {
searchResultsByCategory = new HashMap<>();
for (SearchResult searchResult : searchResults) {
String cat = searchResult.getGroupDescription();
SearchCategory category = searchResult.getCategory();
SearchCategoryGroupKey key = new SearchCategoryGroupKey(category, cat);
List catResults = searchResultsByCategory.get(key);
if (catResults == null) {
catResults = new ArrayList<>();
searchResultsByCategory.put(key, catResults);
}
catResults.add(searchResult);
}
}
public List getSearchResults() {
return Collections.unmodifiableList(searchResults);
}
private class SearchCategoryGroupKey implements Comparable {
private SearchCategory category;
private String groupDescription;
private SearchCategoryGroupKey(SearchCategory searchType, String groupDescription) {
this.category = searchType;
this.groupDescription = groupDescription;
}
public int compareTo(SearchCategoryGroupKey o) {
int typeDiff = this.category.ordinal() - o.category.ordinal();
if (typeDiff != 0) {
return typeDiff;
}
return this.groupDescription.compareToIgnoreCase(o.groupDescription);
}
@Override
public int hashCode() {
return SearchCategoryGroupKey.class.getSimpleName().hashCode() + this.category.hashCode() + category.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof SearchCategoryGroupKey)) {
return false;
}
SearchCategoryGroupKey other = (SearchCategoryGroupKey) obj;
return this.category == other.category && this.groupDescription.equals(other.groupDescription);
}
}
}