[geary/wip/713891-traversable: 7/12] Adding a count method



commit 279871bfe7294d25b8691fa55ff1d474a57ac216
Author: Charles Lindsay <chaz yorba org>
Date:   Wed Dec 11 17:30:55 2013 -0800

    Adding a count method

 src/engine/imap-db/imap-db-folder.vala |   10 +++-------
 src/engine/util/util-traversable.vala  |    9 +++++++++
 2 files changed, 12 insertions(+), 7 deletions(-)
---
diff --git a/src/engine/imap-db/imap-db-folder.vala b/src/engine/imap-db/imap-db-folder.vala
index d9b6bc8..9bbc11f 100644
--- a/src/engine/imap-db/imap-db-folder.vala
+++ b/src/engine/imap-db/imap-db-folder.vala
@@ -2070,19 +2070,15 @@ private class Geary.ImapDB.Folder : BaseObject, Geary.ReferenceSemantics {
     
     private int do_get_unread_count_for_ids(Db.Connection cx,
         Gee.Collection<ImapDB.EmailIdentifier> ids, Cancellable? cancellable) throws Error {
-        int unread_count = 0;
-        
         // Fetch flags for each email and update this folder's unread count.
         // (Note that this only flags for emails which have NOT been marked for removal
         // are included.)
         Gee.Map<ImapDB.EmailIdentifier, Geary.EmailFlags>? flag_map = do_get_email_flags(cx,
             ids, cancellable);
-        if (flag_map != null) {
-            foreach (Geary.EmailFlags flags in flag_map.values)
-                unread_count += flags.is_unread() ? 1 : 0;
-        }
+        if (flag_map != null)
+            return Geary.traverse<Geary.EmailFlags>(flag_map.values).count_matching(f => f.is_unread());
         
-        return unread_count;
+        return 0;
     }
 }
 
diff --git a/src/engine/util/util-traversable.vala b/src/engine/util/util-traversable.vala
index 0dd65c4..9d80cbc 100644
--- a/src/engine/util/util-traversable.vala
+++ b/src/engine/util/util-traversable.vala
@@ -90,6 +90,15 @@ public class Iterable<G> : BaseObject {
         return true;
     }
     
+    public int count_matching(owned Gee.Predicate<G> f) {
+        int count = 0;
+        foreach (G g in this) {
+            if (f(g))
+                count++;
+        }
+        return count;
+    }
+    
     public Gee.Collection<G> add_all_to(Gee.Collection<G> c) {
         while (i.next())
             c.add(i  get());


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]