[libgee] Hide read-only views and make them accessible through a property



commit 88be7908361b7a279e3e48e04d3570a765d201b0
Author: Julien Peeters <contact julienpeeters fr>
Date:   Wed Sep 9 01:00:20 2009 +0200

    Hide read-only views and make them accessible through a property
    
    Fixes bug 594578.
    
    A new read_only_view property is introduced in the Collection, List, Set and Map
    interfaces. The AbstractSet class is introduced to put the common code for the
    read_only_view property.

 gee/Makefile.am                   |    1 +
 gee/abstractcollection.vala       |   14 +++++++++++
 gee/abstractlist.vala             |   12 ++++++++++
 gee/abstractmap.vala              |   15 ++++++++++++
 gee/abstractset.vala              |   45 +++++++++++++++++++++++++++++++++++++
 gee/collection.vala               |    5 ++++
 gee/hashmap.vala                  |    3 +-
 gee/hashset.vala                  |    2 +-
 gee/list.vala                     |    5 ++++
 gee/map.vala                      |    5 ++++
 gee/readonlycollection.vala       |    7 +++++-
 gee/readonlylist.vala             |    9 ++++++-
 gee/readonlymap.vala              |    7 +++++-
 gee/readonlyset.vala              |    7 +++++-
 gee/set.vala                      |    6 +++++
 gee/treemap.vala                  |    2 +-
 gee/treeset.vala                  |    2 +-
 tests/testreadonlycollection.vala |    7 +++++
 tests/testreadonlylist.vala       |    2 +-
 19 files changed, 147 insertions(+), 9 deletions(-)
---
diff --git a/gee/Makefile.am b/gee/Makefile.am
index 656d710..446b937 100644
--- a/gee/Makefile.am
+++ b/gee/Makefile.am
@@ -16,6 +16,7 @@ libgee_la_VALASOURCES = \
 	abstractcollection.vala \
 	abstractlist.vala \
 	abstractmap.vala \
+	abstractset.vala \
 	arraylist.vala \
 	collection.vala \
 	functions.vala \
diff --git a/gee/abstractcollection.vala b/gee/abstractcollection.vala
index 9ca2988..2acf92c 100644
--- a/gee/abstractcollection.vala
+++ b/gee/abstractcollection.vala
@@ -142,4 +142,18 @@ public abstract class Gee.AbstractCollection<G> : Object, Iterable<G>, Collectio
 	 * @inheritDoc
 	 */
 	public abstract Iterator<G> iterator ();
+
+	protected Collection<G> _read_only_view;
+
+	/**
+	 * @inheritDoc
+	 */
+	public virtual Collection<G> read_only_view {
+		get {
+			if (_read_only_view == null) {
+				_read_only_view = new ReadOnlyCollection<G> (this);
+			}
+			return _read_only_view;
+		}
+	}
 }
diff --git a/gee/abstractlist.vala b/gee/abstractlist.vala
index d122e15..82d57b3 100644
--- a/gee/abstractlist.vala
+++ b/gee/abstractlist.vala
@@ -94,4 +94,16 @@ public abstract class Gee.AbstractList<G> : Gee.AbstractCollection<G>, List<G> {
 		}
 		TimSort<G>.sort (this, compare);
 	}
+
+	/**
+	 * @inheritDoc
+	 */
+	public virtual new List<G> read_only_view {
+		get {
+			if (_read_only_view == null) {
+				_read_only_view = new ReadOnlyList<G> (this);
+			}
+			return _read_only_view as List<G>;
+		}
+	}
 }
diff --git a/gee/abstractmap.vala b/gee/abstractmap.vala
index e276ce4..0666fbe 100644
--- a/gee/abstractmap.vala
+++ b/gee/abstractmap.vala
@@ -110,4 +110,19 @@ public abstract class Gee.AbstractMap<K,V> : Object, Map<K,V> {
 		}
 		return true;
 	}
+
+	protected Map<K,V> _read_only_view;
+
+	/**
+	 * @inheritDoc
+	 */
+	public virtual Map<K,V> read_only_view {
+		get {
+			if (_read_only_view == null) {
+				_read_only_view = new ReadOnlyMap<K,V> (this);
+			}
+			return _read_only_view;
+		}
+	}
+
 }
diff --git a/gee/abstractset.vala b/gee/abstractset.vala
new file mode 100644
index 0000000..a9177ec
--- /dev/null
+++ b/gee/abstractset.vala
@@ -0,0 +1,45 @@
+/* abstractset.vala
+ *
+ * Copyright (C) 2007  Jürg Billeter
+ * Copyright (C) 2009  Didier Villevalois
+ *
+ * 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:
+ * 	Julien Peeters <contact julienpeeters fr>
+ */
+
+/**
+ * Skeletal implementation of the { link Gee.Set} interface.
+ *
+ * Contains common code shared by all set implementations.
+ *
+ * @see Gee.TreeSet
+ * @see Gee.HashSet
+ */
+public abstract class Gee.AbstractSet<G> : Gee.AbstractCollection<G>, Set<G> {
+
+	/**
+	 * @inheritDoc
+	 */
+	public virtual new Set<G> read_only_view {
+		get {
+			if (_read_only_view == null) {
+				_read_only_view = new ReadOnlySet<G> (this);
+			}
+			return _read_only_view as Set<G>;
+		}
+	}
+}
diff --git a/gee/collection.vala b/gee/collection.vala
index 769ee93..b506e6a 100644
--- a/gee/collection.vala
+++ b/gee/collection.vala
@@ -121,5 +121,10 @@ public interface Gee.Collection<G> : Iterable<G> {
 	 * @return an array containing all of items from this collection
 	 */
 	public abstract G[] to_array();
+
+	/**
+	 * Property giving access to the read-only view of this collection.
+	 */
+	public abstract Collection<G> read_only_view { get; }
 }
 
diff --git a/gee/hashmap.vala b/gee/hashmap.vala
index 9d0591f..40e6964 100644
--- a/gee/hashmap.vala
+++ b/gee/hashmap.vala
@@ -236,7 +236,7 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
 		}
 	}
 
-	private class KeySet<K,V> : AbstractCollection<K>, Set<K> {
+	private class KeySet<K,V> : AbstractSet<K> {
 		public HashMap<K,V> map { private set; get; }
 
 		public KeySet (HashMap map) {
@@ -278,6 +278,7 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
 		public override bool retain_all (Collection<K> collection) {
 			assert_not_reached ();
 		}
+
 	}
 
 	private class KeyIterator<K,V> : Object, Iterator<K> {
diff --git a/gee/hashset.vala b/gee/hashset.vala
index d680c45..8956791 100644
--- a/gee/hashset.vala
+++ b/gee/hashset.vala
@@ -33,7 +33,7 @@ using GLib;
  *
  * @see Gee.TreeSet
  */
-public class Gee.HashSet<G> : AbstractCollection<G>, Set<G> {
+public class Gee.HashSet<G> : AbstractSet<G> {
 	/**
 	 * @inheritDoc
 	 */
diff --git a/gee/list.vala b/gee/list.vala
index 7461905..d7a22e7 100644
--- a/gee/list.vala
+++ b/gee/list.vala
@@ -103,5 +103,10 @@ public interface Gee.List<G> : Collection<G> {
 	 * @param compare_func compare function to use to compare items
 	 */
 	public abstract void sort (CompareFunc? compare_func = null);
+
+	/**
+	 * Property giving access to the read-only view of this list.
+	 */
+	public abstract new List<G> read_only_view { get; }
 }
 
diff --git a/gee/map.vala b/gee/map.vala
index b3f713a..1e53198 100644
--- a/gee/map.vala
+++ b/gee/map.vala
@@ -112,5 +112,10 @@ public interface Gee.Map<K,V> : GLib.Object {
 	 * @param map the map which items will be compared with this map.
 	 */
 	public abstract bool contains_all (Map<K,V> map);
+
+	/**
+	 * Property giving access to the read-only view this map.
+	 */
+	public abstract Map<K,V> read_only_view { get; }
 }
 
diff --git a/gee/readonlycollection.vala b/gee/readonlycollection.vala
index c4ad4d8..11c3bb1 100644
--- a/gee/readonlycollection.vala
+++ b/gee/readonlycollection.vala
@@ -31,7 +31,7 @@ using GLib;
  *
  * @see Gee.Collection
  */
-public class Gee.ReadOnlyCollection<G> : Object, Iterable<G>, Collection<G> {
+internal class Gee.ReadOnlyCollection<G> : Object, Iterable<G>, Collection<G> {
 
 	/**
 	 * @inheritDoc
@@ -145,5 +145,10 @@ public class Gee.ReadOnlyCollection<G> : Object, Iterable<G>, Collection<G> {
 			return null;
 		}
 	}
+
+	public virtual Collection<G> read_only_view {
+		get { return this; }
+	}
+
 }
 
diff --git a/gee/readonlylist.vala b/gee/readonlylist.vala
index 89fd8a6..354afa7 100644
--- a/gee/readonlylist.vala
+++ b/gee/readonlylist.vala
@@ -31,7 +31,7 @@ using GLib;
  *
  * @see Gee.List
  */
-public class Gee.ReadOnlyList<G> : Gee.ReadOnlyCollection<G>, List<G> {
+internal class Gee.ReadOnlyList<G> : Gee.ReadOnlyCollection<G>, List<G> {
 
 	/**
 	 * Constructs a read-only list that mirrors the content of the specified
@@ -112,5 +112,12 @@ public class Gee.ReadOnlyList<G> : Gee.ReadOnlyCollection<G>, List<G> {
 	public void sort (CompareFunc? compare = null) {
 		assert_not_reached ();
 	}
+
+	/**
+	 * @inheritDoc
+	 */
+	public virtual new List<G> read_only_view {
+		get { return this; }
+	}
 }
 
diff --git a/gee/readonlymap.vala b/gee/readonlymap.vala
index 3166104..1034cd5 100644
--- a/gee/readonlymap.vala
+++ b/gee/readonlymap.vala
@@ -31,7 +31,7 @@ using GLib;
  *
  * @see Gee.Map
  */
-public class Gee.ReadOnlyMap<K,V> : Object, Map<K,V> {
+internal class Gee.ReadOnlyMap<K,V> : Object, Map<K,V> {
 
 	/**
 	 * @inheritDoc
@@ -127,5 +127,10 @@ public class Gee.ReadOnlyMap<K,V> : Object, Map<K,V> {
 	public bool contains_all (Map<K,V> map) {
 		return _map.contains_all (map);
 	}
+
+	public virtual Map<K,V> read_only_view {
+		get { return this; }
+	}
+
 }
 
diff --git a/gee/readonlyset.vala b/gee/readonlyset.vala
index 3997409..0430d79 100644
--- a/gee/readonlyset.vala
+++ b/gee/readonlyset.vala
@@ -31,7 +31,7 @@ using GLib;
  *
  * @see Gee.Set
  */
-public class Gee.ReadOnlySet<G> : Gee.ReadOnlyCollection<G>, Set<G> {
+internal class Gee.ReadOnlySet<G> : Gee.ReadOnlyCollection<G>, Set<G> {
 
 	/**
 	 * Constructs a read-only set that mirrors the content of the specified set.
@@ -41,5 +41,10 @@ public class Gee.ReadOnlySet<G> : Gee.ReadOnlyCollection<G>, Set<G> {
 	public ReadOnlySet (Set<G> set) {
 		base(set);
 	}
+
+	public virtual new Set<G> read_only_view {
+		get { return this; }
+	}
+
 }
 
diff --git a/gee/set.vala b/gee/set.vala
index 765db0b..35a83d9 100644
--- a/gee/set.vala
+++ b/gee/set.vala
@@ -24,5 +24,11 @@
  * A collection without duplicate elements.
  */
 public interface Gee.Set<G> : Collection<G> {
+
+	/**
+	 * Property giving access to the read-only view of this set.
+	 */
+	public abstract new Set<G> read_only_view { get; }
+
 }
 
diff --git a/gee/treemap.vala b/gee/treemap.vala
index 6bbea61..3a20a64 100644
--- a/gee/treemap.vala
+++ b/gee/treemap.vala
@@ -353,7 +353,7 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V> {
 	private weak Node<K, V>? first;
 	private int stamp = 0;
 
-	private class KeySet<K,V> : AbstractCollection<K>, Set<K> {
+	private class KeySet<K,V> : AbstractSet<K> {
 		public TreeMap<K,V> map { private set; get; }
 
 		public KeySet (TreeMap<K,V> map) {
diff --git a/gee/treeset.vala b/gee/treeset.vala
index 5af2462..a325705 100644
--- a/gee/treeset.vala
+++ b/gee/treeset.vala
@@ -32,7 +32,7 @@ using GLib;
  *
  * @see Gee.HashSet
  */
-public class Gee.TreeSet<G> : AbstractCollection<G>, Set<G> {
+public class Gee.TreeSet<G> : AbstractSet<G> {
 	/**
 	 * @inheritDoc
 	 */
diff --git a/tests/testreadonlycollection.vala b/tests/testreadonlycollection.vala
index a78d801..eaa4af7 100644
--- a/tests/testreadonlycollection.vala
+++ b/tests/testreadonlycollection.vala
@@ -28,6 +28,8 @@ public abstract class ReadOnlyCollectionTests : Gee.TestCase {
 
 	public ReadOnlyCollectionTests (string name) {
 		base (name);
+		add_test ("[ReadOnlyCollection] unique read-only view instance",
+		          test_unique_read_only_view_instance);
 		add_test ("[ReadOnlyCollection] add", test_add);
 		add_test ("[ReadOnlyCollection] clear", test_clear);
 		add_test ("[ReadOnlyCollection] remove", test_remove);
@@ -38,6 +40,11 @@ public abstract class ReadOnlyCollectionTests : Gee.TestCase {
 	protected Collection<string> test_collection;
 	protected Collection<string> ro_collection;
 
+	public void test_unique_read_only_view_instance () {
+		var another_ro_collection = test_collection.read_only_view;
+		assert (ro_collection == another_ro_collection);
+	}
+
 	public void test_add () {
 		assert (test_collection.add ("one"));
 
diff --git a/tests/testreadonlylist.vala b/tests/testreadonlylist.vala
index 07d6fc1..41482c5 100644
--- a/tests/testreadonlylist.vala
+++ b/tests/testreadonlylist.vala
@@ -32,7 +32,7 @@ public class ReadOnlyListTests : ReadOnlyCollectionTests {
 
 	public override void set_up () {
 		test_collection = new ArrayList<string> ();
-		ro_collection = new ReadOnlyCollection<string> (test_collection);
+		ro_collection = test_collection.read_only_view;
 	}
 
 	public override void tear_down () {



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