1 // Copyright 2013 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.
5 #include "sync/notifier/single_object_invalidation_set.h"
7 #include "base/values.h"
8 #include "sync/notifier/invalidation_util.h"
12 SingleObjectInvalidationSet::SingleObjectInvalidationSet() {}
14 SingleObjectInvalidationSet::~SingleObjectInvalidationSet() {}
16 void SingleObjectInvalidationSet::Insert(const Invalidation
& invalidation
) {
17 invalidations_
.insert(invalidation
);
20 void SingleObjectInvalidationSet::InsertAll(
21 const SingleObjectInvalidationSet
& other
) {
22 invalidations_
.insert(other
.begin(), other
.end());
25 void SingleObjectInvalidationSet::Clear() {
26 invalidations_
.clear();
29 bool SingleObjectInvalidationSet::StartsWithUnknownVersion() const {
30 return !invalidations_
.empty() &&
31 invalidations_
.begin()->is_unknown_version();
34 size_t SingleObjectInvalidationSet::GetSize() const {
35 return invalidations_
.size();
38 bool SingleObjectInvalidationSet::IsEmpty() const {
39 return invalidations_
.empty();
44 struct InvalidationComparator
{
45 bool operator()(const Invalidation
& inv1
, const Invalidation
& inv2
) {
46 return inv1
.Equals(inv2
);
52 bool SingleObjectInvalidationSet::operator==(
53 const SingleObjectInvalidationSet
& other
) const {
54 return std::equal(invalidations_
.begin(),
56 other
.invalidations_
.begin(),
57 InvalidationComparator());
60 SingleObjectInvalidationSet::const_iterator
61 SingleObjectInvalidationSet::begin() const {
62 return invalidations_
.begin();
65 SingleObjectInvalidationSet::const_iterator
66 SingleObjectInvalidationSet::end() const {
67 return invalidations_
.end();
70 SingleObjectInvalidationSet::const_reverse_iterator
71 SingleObjectInvalidationSet::rbegin() const {
72 return invalidations_
.rbegin();
75 SingleObjectInvalidationSet::const_reverse_iterator
76 SingleObjectInvalidationSet::rend() const {
77 return invalidations_
.rend();
80 const Invalidation
& SingleObjectInvalidationSet::back() const {
81 return *invalidations_
.rbegin();
84 scoped_ptr
<base::ListValue
> SingleObjectInvalidationSet::ToValue() const {
85 scoped_ptr
<base::ListValue
> value(new ListValue
);
86 for (InvalidationsSet::const_iterator it
= invalidations_
.begin();
87 it
!= invalidations_
.end(); ++it
) {
88 value
->Append(it
->ToValue().release());
93 bool SingleObjectInvalidationSet::ResetFromValue(
94 const base::ListValue
& list
) {
95 for (size_t i
= 0; i
< list
.GetSize(); ++i
) {
96 const base::DictionaryValue
* dict
;
97 if (!list
.GetDictionary(i
, &dict
)) {
98 DLOG(WARNING
) << "Could not find invalidation at index " << i
;
101 scoped_ptr
<Invalidation
> invalidation
= Invalidation::InitFromValue(*dict
);
103 DLOG(WARNING
) << "Failed to parse invalidation at index " << i
;
106 invalidations_
.insert(*invalidation
);
111 } // namespace syncer