[libgee] Add read_only_view to Gee.MultiSet



commit 5fcd51c38814506901150121daeaa7236f0227b4
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Sat Feb 16 12:46:46 2013 +0000

    Add read_only_view to Gee.MultiSet

 gee/Makefile.am           |    1 +
 gee/abstractmultiset.vala |   14 ++++++++++-
 gee/multiset.vala         |   19 +++++++++++++++
 gee/readonlymultiset.vala |   57 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 90 insertions(+), 1 deletions(-)
---
diff --git a/gee/Makefile.am b/gee/Makefile.am
index aeaffcd..a129b1f 100644
--- a/gee/Makefile.am
+++ b/gee/Makefile.am
@@ -56,6 +56,7 @@ libgee_0_8_la_SOURCES = \
        readonlycollection.vala \
        readonlylist.vala \
        readonlymap.vala \
+       readonlymultiset.vala \
        readonlyset.vala \
        readonlysortedmap.vala \
        readonlysortedset.vala \
diff --git a/gee/abstractmultiset.vala b/gee/abstractmultiset.vala
index 1be4fd4..3104c55 100644
--- a/gee/abstractmultiset.vala
+++ b/gee/abstractmultiset.vala
@@ -92,6 +92,19 @@ public abstract class Gee.AbstractMultiSet<G> : AbstractCollection<G>, MultiSet<
                _nitems = 0;
        }
 
+       private weak MultiSet<G> _read_only_view;
+       public virtual new MultiSet<G> read_only_view {
+               owned get {
+                       MultiSet<G> instance = _read_only_view;
+                       if (_read_only_view == null) {
+                               instance = new ReadOnlyMultiSet<G> (this);
+                               _read_only_view = instance;
+                               instance.add_weak_pointer ((void**) (&_read_only_view));
+                       }
+                       return instance;
+               }
+       }
+
        // Future-proofing
        internal new virtual void reserved0() {}
        internal new virtual void reserved1() {}
@@ -102,7 +115,6 @@ public abstract class Gee.AbstractMultiSet<G> : AbstractCollection<G>, MultiSet<
        internal new virtual void reserved6() {}
        internal new virtual void reserved7() {}
        internal new virtual void reserved8() {}
-       internal new virtual void reserved9() {}
 
        private class Iterator<G> : Object, Traversable<G>, Gee.Iterator<G> {
                private AbstractMultiSet<G> _set;
diff --git a/gee/multiset.vala b/gee/multiset.vala
index 66ab950..0236cea 100644
--- a/gee/multiset.vala
+++ b/gee/multiset.vala
@@ -33,4 +33,23 @@ public interface Gee.MultiSet<G> : Collection<G> {
         * @return     the number of occurences of the item in this multiset.
         */
        public abstract int count (G item);
+
+       /**
+        * The read-only view of this set.
+        */
+       public virtual new MultiSet<G> read_only_view {
+               owned get {
+                       return new ReadOnlyMultiSet<G> (this);
+               }
+       }
+
+       /**
+        * Returns an immutable empty set.
+        *
+        * @return an immutable empty set
+        */
+       public static Set<G> empty<G> () {
+               return new HashSet<G> ().read_only_view;
+       }
 }
+
diff --git a/gee/readonlymultiset.vala b/gee/readonlymultiset.vala
new file mode 100644
index 0000000..b6404f1
--- /dev/null
+++ b/gee/readonlymultiset.vala
@@ -0,0 +1,57 @@
+/* readonlymultiset.vala
+ *
+ * Copyright (C) 2013  Maciej Piechotka
+ *
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Maciej Piechotka <uzytkownik2 gmail com>
+ */
+
+using GLib;
+
+/**
+ * Read-only view for { link MultiSet} collections.
+ *
+ * This class decorates any class which implements the { link Collection}
+ * interface by making it read only. Any method which normally modify data will
+ * throw an error.
+ *
+ * @see MultiSet
+ */
+internal class Gee.ReadOnlyMultiSet<G> : Gee.ReadOnlyCollection<G>, MultiSet<G> {
+       /**
+        * Constructs a read-only multi-set that mirrors the content of the specified
+        * list.
+        *
+        * @param multiset the multi-set to decorate.
+        */
+       public ReadOnlyMultiSet (MultiSet<G> multiset) {
+               base (multiset);
+       }
+
+       /**
+        * { inheritDoc}
+        */
+       public int count (G item) {
+               return ((Gee.MultiSet<G>) _collection).count (item);
+       }
+
+       /**
+        * { inheritDoc}
+        */
+       public virtual new MultiSet<G> read_only_view { owned get { return this; } }
+}
+


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