[folks] e-d-s: move Set comparison method into it's own class



commit 525679227287ca21270c996762864be1f4962323
Author: Raul Gutierrez Segales <rgs collabora co uk>
Date:   Wed Sep 7 22:59:41 2011 +0100

    e-d-s: move Set comparison method into it's own class
    
    This refactoring is so we can reuse the method with other
    types.

 backends/eds/lib/Makefile.am             |    1 +
 backends/eds/lib/edsf-persona-store.vala |   36 +--------------------------
 backends/eds/lib/edsf-persona.vala       |   25 ++++++++++---------
 backends/eds/lib/edsf-util.vala          |   38 ++++++++++++++++++++++++++++++
 4 files changed, 54 insertions(+), 46 deletions(-)
---
diff --git a/backends/eds/lib/Makefile.am b/backends/eds/lib/Makefile.am
index 2e52c43..5aa784a 100644
--- a/backends/eds/lib/Makefile.am
+++ b/backends/eds/lib/Makefile.am
@@ -29,6 +29,7 @@ libfolks_eds_la_vala.stamp:
 folks_eds_valasources = \
 	edsf-persona.vala \
 	edsf-persona-store.vala \
+	edsf-util.vala \
 	memory-icon.vala \
 	$(NULL)
 
diff --git a/backends/eds/lib/edsf-persona-store.vala b/backends/eds/lib/edsf-persona-store.vala
index 9c28f66..d696cc9 100644
--- a/backends/eds/lib/edsf-persona-store.vala
+++ b/backends/eds/lib/edsf-persona-store.vala
@@ -1387,7 +1387,8 @@ public class Edsf.PersonaStore : Folks.PersonaStore
   internal async void _set_roles (Edsf.Persona persona,
       Set<RoleFieldDetails> roles) throws PropertyError
     {
-      if (Edsf.PersonaStore.equal_sets (roles, persona.roles))
+      var comp = new Edsf.SetComparator<RoleFieldDetails> ();
+      if (comp.equal (roles, persona.roles))
         return;
 
       yield this._set_contact_roles (persona.contact, roles);
@@ -1760,37 +1761,4 @@ public class Edsf.PersonaStore : Folks.PersonaStore
           _("Unknown error setting property â%sâ: %s"), property_name,
           error_in.message);
     }
-
-  internal static bool equal_sets (Set<RoleFieldDetails> a,
-      Set<RoleFieldDetails> b)
-    {
-      /* For the love of Dijkstra, there should be a more
-       * succint way of comparing Sets */
-
-      if (a.size == 0 &&
-          b.size == 0)
-        return true;
-
-      if (a.size != b.size)
-        return false;
-
-      bool same = false;
-      foreach (var new_role in a)
-        {
-          same = false;
-          foreach (var cur_role in b)
-            {
-              if (cur_role.equal (new_role))
-                {
-                  same = true;
-                  break;
-                }
-            }
-
-          if (!same)
-            return false;
-        }
-
-      return true;
-    }
 }
diff --git a/backends/eds/lib/edsf-persona.vala b/backends/eds/lib/edsf-persona.vala
index 209e927..8a980dc 100644
--- a/backends/eds/lib/edsf-persona.vala
+++ b/backends/eds/lib/edsf-persona.vala
@@ -899,18 +899,19 @@ public class Edsf.Persona : Folks.Persona,
             new_roles.add (role_fd);
         }
 
-        if (new_roles.size > 0 &&
-            !Edsf.PersonaStore.equal_sets (new_roles, this._roles))
-          {
-            this._roles = new_roles;
-            this._roles_ro = new_roles.read_only_view;
-            this.notify_property ("roles");
-          }
-        else if (new_roles.size == 0)
-          {
-            this._roles.clear ();
-            this.notify_property ("roles");
-          }
+      var comp = new Edsf.SetComparator<RoleFieldDetails> ();
+      if (new_roles.size > 0 &&
+          !comp.equal (new_roles, this._roles))
+        {
+          this._roles = new_roles;
+          this._roles_ro = new_roles.read_only_view;
+          this.notify_property ("roles");
+        }
+      else if (new_roles.size == 0)
+        {
+          this._roles.clear ();
+          this.notify_property ("roles");
+        }
     }
 
   private RoleFieldDetails? _get_default_role ()
diff --git a/backends/eds/lib/edsf-util.vala b/backends/eds/lib/edsf-util.vala
new file mode 100644
index 0000000..d517d89
--- /dev/null
+++ b/backends/eds/lib/edsf-util.vala
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2011 Collabora Ltd.
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *       Raul Gutierrez Segales <raul gutierrez segales collabora co uk>
+ */
+
+using Gee;
+
+internal class Edsf.SetComparator<G> : GLib.Object {
+  internal bool equal (Set<G> a,
+      Set<G> b)
+    {
+      if (a.size != b.size)
+        return false;
+
+      foreach (var a_elem in a)
+        {
+          if (!b.contains (a_elem))
+            return false;
+        }
+
+      return true;
+    }
+}



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