OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.suggestions; | |
6 | |
7 import org.chromium.chrome.browser.ntp.cards.ActionItem; | |
8 import org.chromium.chrome.browser.ntp.cards.SuggestionsSection; | |
9 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; | |
10 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; | |
11 | |
12 import java.util.LinkedHashMap; | |
13 import java.util.Map; | |
14 | |
15 /** | |
16 * Attributes ranks to suggestions and related elements. | |
17 * | |
18 * Ranks here are 0-based scores attributed based on the position or loading ord er of the | |
19 * elements. See implementation for more details. | |
vitaliii
2017/01/18 08:46:27
Remove "See implementation for more details."?
dgn
2017/01/18 13:54:36
Done.
| |
20 */ | |
21 public class SuggestionsRanker { | |
22 private final Map<Integer, Integer> mSuggestionsAddedPerSection = new Linked HashMap<>(); | |
23 private int mTotalAddedSuggestions; | |
24 | |
25 /** | |
26 * Attributes a per section rank to the provided action item. | |
27 * @see ActionItem#getPerSectionRank() | |
28 */ | |
29 public void rankActionItem(ActionItem actionItem, SuggestionsSection section ) { | |
30 if (actionItem.getPerSectionRank() != -1) return; // Item was already ra nked. | |
31 actionItem.setPerSectionRank(section.getSuggestionsCount()); | |
32 } | |
33 | |
34 /** | |
35 * Attributes global and per section rank to the provided suggestion. | |
36 * @see SnippetArticle#getPerSectionRank() | |
37 * @see SnippetArticle#getGlobalRank() | |
38 */ | |
39 public void rankSuggestion(SnippetArticle suggestion) { | |
40 int globalRank = mTotalAddedSuggestions++; | |
41 int perSectionRank = mSuggestionsAddedPerSection.get(suggestion.mCategor y); | |
42 mSuggestionsAddedPerSection.put(suggestion.mCategory, perSectionRank + 1 ); | |
43 | |
44 suggestion.setRank(perSectionRank, globalRank); | |
45 } | |
46 | |
47 public void registerCategory(@CategoryInt int category) { | |
48 // Check we are not simply resetting an already registered category. | |
49 if (mSuggestionsAddedPerSection.containsKey(category)) return; | |
50 mSuggestionsAddedPerSection.put(category, 0); | |
51 } | |
52 | |
53 public int getCategoryRank(@CategoryInt int category) { | |
54 int rank = 0; | |
55 for (Integer key : mSuggestionsAddedPerSection.keySet()) { | |
56 if (key == category) return rank; | |
57 ++rank; | |
58 } | |
59 return -1; | |
60 } | |
61 } | |
OLD | NEW |