| OLD | NEW |
| 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_factory.h" | 5 #include "components/ntp_snippets/category_factory.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 | 49 |
| 50 Category CategoryFactory::FromIDValue(int id) { | 50 Category CategoryFactory::FromIDValue(int id) { |
| 51 DCHECK_GE(id, 0); | 51 DCHECK_GE(id, 0); |
| 52 DCHECK(id < static_cast<int>(KnownCategories::LOCAL_CATEGORIES_COUNT) || | 52 DCHECK(id < static_cast<int>(KnownCategories::LOCAL_CATEGORIES_COUNT) || |
| 53 id > static_cast<int>(KnownCategories::REMOTE_CATEGORIES_OFFSET)); | 53 id > static_cast<int>(KnownCategories::REMOTE_CATEGORIES_OFFSET)); |
| 54 return InternalFromID(id); | 54 return InternalFromID(id); |
| 55 } | 55 } |
| 56 | 56 |
| 57 bool CategoryFactory::CompareCategories(const Category& left, | 57 bool CategoryFactory::CompareCategories(const Category& left, |
| 58 const Category& right) const { | 58 const Category& right) const { |
| 59 if (left == right) | 59 if (left == right) { |
| 60 return false; | 60 return false; |
| 61 } |
| 61 return std::find(ordered_categories_.begin(), ordered_categories_.end(), | 62 return std::find(ordered_categories_.begin(), ordered_categories_.end(), |
| 62 left) < std::find(ordered_categories_.begin(), | 63 left) < std::find(ordered_categories_.begin(), |
| 63 ordered_categories_.end(), right); | 64 ordered_categories_.end(), right); |
| 64 } | 65 } |
| 65 | 66 |
| 66 //////////////////////////////////////////////////////////////////////////////// | 67 //////////////////////////////////////////////////////////////////////////////// |
| 67 // Private methods | 68 // Private methods |
| 68 | 69 |
| 69 bool CategoryFactory::CategoryExists(int id) { | 70 bool CategoryFactory::CategoryExists(int id) { |
| 70 return std::find(ordered_categories_.begin(), ordered_categories_.end(), | 71 return std::find(ordered_categories_.begin(), ordered_categories_.end(), |
| 71 Category(id)) != ordered_categories_.end(); | 72 Category(id)) != ordered_categories_.end(); |
| 72 } | 73 } |
| 73 | 74 |
| 74 void CategoryFactory::AddKnownCategory(KnownCategories known_category) { | 75 void CategoryFactory::AddKnownCategory(KnownCategories known_category) { |
| 75 InternalFromID(static_cast<int>(known_category)); | 76 InternalFromID(static_cast<int>(known_category)); |
| 76 } | 77 } |
| 77 | 78 |
| 78 Category CategoryFactory::InternalFromID(int id) { | 79 Category CategoryFactory::InternalFromID(int id) { |
| 79 auto it = std::find(ordered_categories_.begin(), ordered_categories_.end(), | 80 auto it = std::find(ordered_categories_.begin(), ordered_categories_.end(), |
| 80 Category(id)); | 81 Category(id)); |
| 81 if (it != ordered_categories_.end()) | 82 if (it != ordered_categories_.end()) { |
| 82 return *it; | 83 return *it; |
| 84 } |
| 83 | 85 |
| 84 Category category(id); | 86 Category category(id); |
| 85 ordered_categories_.push_back(category); | 87 ordered_categories_.push_back(category); |
| 86 return category; | 88 return category; |
| 87 } | 89 } |
| 88 | 90 |
| 89 } // namespace ntp_snippets | 91 } // namespace ntp_snippets |
| OLD | NEW |