Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: components/ntp_snippets/category.cc

Issue 2568033005: [NTP::SectionOrder] Replace CategoryFactory with a category ranker. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/category.h" 5 #include "components/ntp_snippets/category.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 8
9 namespace ntp_snippets { 9 namespace ntp_snippets {
10 10
11 Category::Category(int id) : id_(id) {} 11 Category::Category(int id) : id_(id) {}
12 12
13 bool Category::IsKnownCategory(KnownCategories known_category) const { 13 bool Category::IsKnownCategory(KnownCategories known_category) const {
14 DCHECK_NE(known_category, KnownCategories::LOCAL_CATEGORIES_COUNT); 14 DCHECK_NE(known_category, KnownCategories::LOCAL_CATEGORIES_COUNT);
15 DCHECK_NE(known_category, KnownCategories::REMOTE_CATEGORIES_OFFSET); 15 DCHECK_NE(known_category, KnownCategories::REMOTE_CATEGORIES_OFFSET);
16 return id_ == static_cast<int>(known_category); 16 return id_ == static_cast<int>(known_category);
17 } 17 }
18 18
19 // static
20 Category Category::FromKnownCategory(KnownCategories known_category) {
21 const int id = static_cast<int>(known_category);
Marc Treib 2016/12/13 12:22:42 nit: just inline into the return
vitaliii 2016/12/14 08:59:38 Done.
22 return FromIDValue(id);
23 }
24
25 // static
26 Category Category::FromRemoteCategory(int remote_category) {
27 DCHECK_GT(remote_category, 0);
28 return Category(static_cast<int>(KnownCategories::REMOTE_CATEGORIES_OFFSET) +
29 remote_category);
30 }
31
32 // static
33 Category Category::FromIDValue(int id) {
34 DCHECK_GE(id, 0);
35 DCHECK(id < static_cast<int>(KnownCategories::LOCAL_CATEGORIES_COUNT) ||
36 id > static_cast<int>(KnownCategories::REMOTE_CATEGORIES_OFFSET));
37 return Category(id);
38 }
39
19 bool operator==(const Category& left, const Category& right) { 40 bool operator==(const Category& left, const Category& right) {
20 return left.id() == right.id(); 41 return left.id() == right.id();
21 } 42 }
22 43
23 bool operator!=(const Category& left, const Category& right) { 44 bool operator!=(const Category& left, const Category& right) {
24 return !(left == right); 45 return !(left == right);
25 } 46 }
26 47
27 bool Category::CompareByID::operator()(const Category& left, 48 bool Category::CompareByID::operator()(const Category& left,
28 const Category& right) const { 49 const Category& right) const {
29 return left.id() < right.id(); 50 return left.id() < right.id();
30 } 51 }
31 52
32 std::ostream& operator<<(std::ostream& os, const Category& obj) { 53 std::ostream& operator<<(std::ostream& os, const Category& obj) {
33 os << obj.id(); 54 os << obj.id();
34 return os; 55 return os;
35 } 56 }
36 57
37 } // namespace ntp_snippets 58 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698