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.SuggestionsSection; | |
8 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; | |
9 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; | |
10 | |
11 import java.util.Map; | |
12 import java.util.NoSuchElementException; | |
13 | |
14 /** | |
15 * Exposes UMA related methods. | |
16 */ | |
17 public class SuggestionsUma { | |
18 private final Map<Integer, SuggestionsSection> mSections; | |
19 | |
20 public SuggestionsUma(Map<Integer, SuggestionsSection> sections) { | |
21 mSections = sections; | |
22 } | |
23 | |
24 /** A suggestion's rank is the number of suggestions preceding it in its sec tion. */ | |
25 public int getSuggestionRank(SnippetArticle suggestion) { | |
26 return mSections.get(suggestion.mCategory).getSuggestionRank(suggestion) ; | |
27 } | |
28 | |
29 /** | |
30 * A suggestion's global rank is the total number of suggestions preceding i t on the current | |
31 * page. | |
32 * @param suggestion the suggestion to compute the global rank for. | |
33 * @param localRank the rank of the suggestion in its section. | |
34 */ | |
35 public int getSuggestionGlobalRank(SnippetArticle suggestion, int localRank) { | |
36 return getSectionStartingRank(suggestion.mCategory) + localRank; | |
Bernhard Bauer
2017/01/11 13:53:38
Would it be simpler to just return an android.util
dgn
2017/01/12 14:29:58
Done.
| |
37 } | |
38 | |
39 /** @return the global rank that the first suggestion of the section would h ave. */ | |
40 private int getSectionStartingRank(@CategoryInt int category) { | |
41 int index = 0; | |
42 for (Map.Entry<Integer, SuggestionsSection> kvPair : mSections.entrySet( )) { | |
43 if (kvPair.getKey() == category) return index; | |
44 index += kvPair.getValue().getSuggestionsCount(); | |
45 } | |
46 throw new NoSuchElementException(); // Or return -Inf? | |
47 } | |
48 } | |
OLD | NEW |