17
17
import java .util .List ;
18
18
import java .util .concurrent .CompletableFuture ;
19
19
import java .util .concurrent .Executor ;
20
+ import java .util .function .Supplier ;
20
21
21
22
public class SuggestionsProvider {
22
23
23
24
private static final String LOGTAG = SuggestionsProvider .class .getSimpleName ();
24
25
25
- public class DefaultSuggestionsComparator implements Comparator {
26
+ public static class DefaultSuggestionsComparator implements Comparator < SuggestionItem > {
26
27
27
- public int compare (Object obj1 , Object obj2 ) {
28
- SuggestionItem suggestion1 = (SuggestionItem )obj1 ;
29
- SuggestionItem suggestion2 = (SuggestionItem )obj2 ;
30
- if (suggestion1 .type == Type .SUGGESTION && suggestion2 .type == Type .SUGGESTION ) {
28
+ public int compare (SuggestionItem obj1 , SuggestionItem obj2 ) {
29
+ if (obj1 .type == Type .SUGGESTION && obj2 .type == Type .SUGGESTION ) {
31
30
return 0 ;
32
31
33
- } else if (suggestion1 .type == suggestion2 .type ) {
34
- if (suggestion1 .type == Type .HISTORY ) {
35
- if (suggestion1 .score != suggestion2 .score ) {
36
- return suggestion1 .score - suggestion2 .score ;
32
+ } else if (obj1 .type == obj2 .type ) {
33
+ if (obj1 .type == Type .HISTORY ) {
34
+ if (obj1 .score != obj2 .score ) {
35
+ return obj1 .score - obj2 .score ;
37
36
}
38
37
}
39
38
40
- return suggestion1 .url .compareTo (suggestion2 .url );
39
+ return obj1 .url .compareTo (obj2 .url );
41
40
42
41
} else {
43
- return suggestion1 .type .ordinal () - suggestion2 .type .ordinal ();
42
+ return obj1 .type .ordinal () - obj2 .type .ordinal ();
44
43
}
45
44
}
46
45
}
47
46
48
47
private SearchEngineWrapper mSearchEngineWrapper ;
49
48
private String mText ;
50
49
private String mFilterText ;
51
- private Comparator mComparator ;
50
+ private Comparator < SuggestionItem > mComparator ;
52
51
private Executor mUIThreadExecutor ;
53
52
54
53
public SuggestionsProvider (Context context ) {
@@ -74,15 +73,15 @@ public void setFilterText(String text) {
74
73
75
74
public void setText (String text ) { mText = text ; }
76
75
77
- public void setComparator (Comparator comparator ) {
76
+ public void setComparator (Comparator < SuggestionItem > comparator ) {
78
77
mComparator = comparator ;
79
78
}
80
79
81
80
private CompletableFuture <List <SuggestionItem >> getBookmarkSuggestions (@ NonNull List <SuggestionItem > items ) {
82
- CompletableFuture future = new CompletableFuture ();
81
+ CompletableFuture < List < SuggestionItem >> future = new CompletableFuture <> ();
83
82
SessionStore .get ().getBookmarkStore ().searchBookmarks (mFilterText , 100 ).thenAcceptAsync ((bookmarks ) -> {
84
83
bookmarks .stream ()
85
- .filter ((b ) -> !b .getUrl ().startsWith ("place:" ) &&
84
+ .filter ((b ) -> b . getUrl () != null && !b .getUrl ().startsWith ("place:" ) &&
86
85
!b .getUrl ().startsWith ("about:reader" ))
87
86
.forEach (b -> items .add (SuggestionItem .create (
88
87
b .getTitle (),
@@ -107,7 +106,7 @@ private CompletableFuture<List<SuggestionItem>> getBookmarkSuggestions(@NonNull
107
106
}
108
107
109
108
private CompletableFuture <List <SuggestionItem >> getHistorySuggestions (@ NonNull final List <SuggestionItem > items ) {
110
- CompletableFuture future = new CompletableFuture ();
109
+ CompletableFuture < List < SuggestionItem >> future = new CompletableFuture <> ();
111
110
SessionStore .get ().getHistoryStore ().getSuggestions (mFilterText , 100 ).thenAcceptAsync ((history ) -> {
112
111
history .forEach (h -> items .add (SuggestionItem .create (
113
112
h .getTitle (),
@@ -132,7 +131,7 @@ private CompletableFuture<List<SuggestionItem>> getHistorySuggestions(@NonNull f
132
131
}
133
132
134
133
private CompletableFuture <List <SuggestionItem >> getSearchEngineSuggestions (@ NonNull final List <SuggestionItem > items ) {
135
- CompletableFuture future = new CompletableFuture ();
134
+ CompletableFuture < List < SuggestionItem >> future = new CompletableFuture <> ();
136
135
137
136
// Completion from browser-domains
138
137
if (!mText .equals (mFilterText )) {
@@ -182,7 +181,7 @@ private CompletableFuture<List<SuggestionItem>> getSearchEngineSuggestions(@NonN
182
181
}
183
182
184
183
public CompletableFuture <List <SuggestionItem >> getSuggestions () {
185
- return CompletableFuture .supplyAsync (() -> new ArrayList <SuggestionItem >() )
184
+ return CompletableFuture .supplyAsync ((Supplier < ArrayList <SuggestionItem >>) ArrayList :: new )
186
185
.thenComposeAsync (this ::getSearchEngineSuggestions )
187
186
.thenComposeAsync (this ::getBookmarkSuggestions )
188
187
.thenComposeAsync (this ::getHistorySuggestions );
0 commit comments