Author: julien(a)jboss.com
Date: 2007-11-11 17:31:47 -0500 (Sun, 11 Nov 2007)
New Revision: 8869
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionBuilder.java
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapBuilder.java
Log:
refactored and generifed CollectionBuilder and MapBuilder
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionBuilder.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionBuilder.java 2007-11-11
22:20:21 UTC (rev 8868)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/CollectionBuilder.java 2007-11-11
22:31:47 UTC (rev 8869)
@@ -33,37 +33,44 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7234 $
*/
-public class CollectionBuilder
+public class CollectionBuilder<V>
{
/** . */
- private ArrayList collection;
+ private Collection<V> collection;
- public CollectionBuilder()
+ private CollectionBuilder(Collection<V> collection)
{
- this.collection = new ArrayList();
+ this.collection = collection;
}
- /**
- * Factory method to create a collection builder using a singleton.
- *
- * @param o the object to add
- * @return the builder created
- */
- public static CollectionBuilder singleton(Object o)
+ public static <V> CollectionBuilder<V> create(Collection<V>
collection)
{
- CollectionBuilder builder = new CollectionBuilder();
- builder.collection.add(o);
- return builder;
+ return new CollectionBuilder<V>(collection);
}
+ public static <V> CollectionBuilder<V> arrayList()
+ {
+ return new CollectionBuilder<V>(new ArrayList<V>());
+ }
+
+ public static <V> CollectionBuilder<V> linkedList()
+ {
+ return new CollectionBuilder<V>(new LinkedList<V>());
+ }
+
+ public static <V> CollectionBuilder<V> hashSet()
+ {
+ return new CollectionBuilder<V>(new HashSet<V>());
+ }
+
/**
* Add the object to the collection.
*
* @param o the object to add
* @return the builder
*/
- public CollectionBuilder add(Object o)
+ public CollectionBuilder<V> add(V o)
{
collection.add(o);
return this;
@@ -75,39 +82,14 @@
* @param all the objects to add
* @return the builder
*/
- public CollectionBuilder addAll(Collection all)
+ public CollectionBuilder<V> addAll(Collection<V> all)
{
collection.addAll(all);
return this;
}
- /**
- * Return a set build from the collection.
- *
- * @return a set
- */
- public HashSet toHashSet()
+ public Collection<V> get()
{
- return new HashSet(collection);
+ return collection;
}
-
- /**
- * Return a list build from the collection.
- *
- * @return a list
- */
- public ArrayList toArrayList()
- {
- return new ArrayList(collection);
- }
-
- /**
- * Return a list build from the collection.
- *
- * @return a list
- */
- public LinkedList toLinkedList()
- {
- return new LinkedList(collection);
- }
}
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapBuilder.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapBuilder.java 2007-11-11
22:20:21 UTC (rev 8868)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapBuilder.java 2007-11-11
22:31:47 UTC (rev 8869)
@@ -27,18 +27,18 @@
import java.util.Map;
/**
- * An helper to build collection of object in a simple manner.
+ * An helper to build map in a simple manner.
*
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7228 $
*/
-public class MapBuilder
+public class MapBuilder<K, V>
{
/** . */
- private final Map map;
+ private final Map<K, V> map;
- private MapBuilder(Map map)
+ private MapBuilder(Map<K, V> map)
{
if (map == null)
{
@@ -52,9 +52,9 @@
*
* @return a new instance
*/
- public static MapBuilder hashMap()
+ public static <K, V> MapBuilder<K, V> hashMap()
{
- return new MapBuilder(new HashMap());
+ return new MapBuilder<K, V>(new HashMap<K, V>());
}
/**
@@ -62,14 +62,14 @@
*
* @return a new instance
*/
- public static MapBuilder linkedHashMap()
+ public static <K, V> MapBuilder<K, V> linkedHashMap()
{
- return new MapBuilder(new LinkedHashMap());
+ return new MapBuilder<K, V>(new LinkedHashMap<K, V>());
}
- public static MapBuilder create(Map map)
+ public static <K, V> MapBuilder<K, V> create(Map<K, V> map)
{
- return new MapBuilder(map);
+ return new MapBuilder<K, V>(map);
}
/**
@@ -79,7 +79,7 @@
* @param value the value
* @return the builder
*/
- public MapBuilder put(Object key, Object value)
+ public MapBuilder<K, V> put(K key, V value)
{
map.put(key, value);
return this;
@@ -91,13 +91,13 @@
* @param all the entries to add
* @return the builder
*/
- public MapBuilder putAll(Map all)
+ public MapBuilder<K, V> putAll(Map<K, V> all)
{
map.putAll(all);
return this;
}
- public Map get()
+ public Map<K, V> get()
{
return map;
}
Show replies by date