Permalink
Browse files

GROOVY-4731: sample code in javadoc of Map#spreadMap() doesn't work

git-svn-id: http://svn.codehaus.org/groovy/branches/GROOVY_1_8_RC_X@22000 a5544e8c-8a19-0410-ba12-f9af4593a198
  • Loading branch information...
1 parent ca64c2a commit e3a842fcbac512c8fa782c40bec875a31bdc4a77 @paulk-asert paulk-asert committed Apr 13, 2011
Showing with 47 additions and 20 deletions.
  1. +11 −6 src/main/groovy/lang/SpreadMap.java
  2. +36 −14 src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2007 the original author or authors.
+ * Copyright 2003-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package groovy.lang;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Iterator;
@@ -25,7 +26,6 @@
* Represents a spreadable map which extends java.util.HashMap.
*
* @author Pilho Kim
- * @version $Revision$
*/
public class SpreadMap extends HashMap {
@@ -44,6 +44,14 @@ public SpreadMap(Map map) {
this.mapData = map;
}
+ /**
+ * @since 1.8.0
+ * @param list the list to make spreadable
+ */
+ public SpreadMap(List list) {
+ this(list.toArray());
+ }
+
public Object get(Object obj) {
return mapData.get(obj);
}
@@ -78,11 +86,10 @@ public boolean equals(SpreadMap that) {
if (that == null) return false;
if (size() == that.size()) {
- SpreadMap other = (SpreadMap) that;
Iterator iter = mapData.keySet().iterator();
for (; iter.hasNext(); ) {
Object key = iter.next();
- if (! DefaultTypeTransformation.compareEqual(get(key), other.get(key)) ) {
+ if (! DefaultTypeTransformation.compareEqual(get(key), that.get(key)) ) {
return false;
}
}
@@ -105,8 +112,6 @@ public int hashCode() {
}
/**
- * Returns the string expression of <code>this</code>.
- *
* @return the string expression of <code>this</code>
*/
public String toString() {
@@ -5805,7 +5805,7 @@ public static List getAt(Collection coll, String property) {
/**
* Synonym for {@link #toSpreadMap(java.util.Map)}.
* @param self a map
- * @return a newly created Spreadmap
+ * @return a newly created SpreadMap
* @since 1.0
*/
public static SpreadMap spread(Map self) {
@@ -5815,19 +5815,21 @@ public static SpreadMap spread(Map self) {
/**
* Returns a new <code>SpreadMap</code> from this map.
* <p/>
- * For examples, if there is defined a function like as
- * <blockquote><pre>
- * def fn(Map m) { return m.a + m.b + m.c + m.d }
- * </pre></blockquote>, then all of the following three have the same meaning.
- * <blockquote><pre>
- * println fn(a:1, [b:2, c:3].toSpreadMap(), d:4)
- * println fn(a:1, *:[b:2, c:3], d:4)
- * println fn(a:1, b:2, c:3, d:4)
- * </pre></blockquote>
- * <p/>
+ * The example below shows the various possible use cases:
+ * <pre class="groovyTestCase">
+ * def fn(Map m) { return m.a + m.b + m.c + m.d }
+ *
+ * assert fn(a:1, b:2, c:3, d:4) == 10
+ * assert fn(a:1, *:[b:2, c:3], d:4) == 10
+ * assert fn([a:1, b:2, c:3, d:4].toSpreadMap()) == 10
+ * assert fn((['a', 1, 'b', 2, 'c', 3, 'd', 4] as Object[]).toSpreadMap()) == 10
+ * assert fn(['a', 1, 'b', 2, 'c', 3, 'd', 4].toSpreadMap()) == 10
+ * assert fn(['abcd'.toList(), 1..4].transpose().flatten().toSpreadMap()) == 10
+ * </pre>
+ * Note that toSpreadMap() is not normally used explicitly but under the covers by Groovy.
*
- * @param self a list to be converted into a spreadmap
- * @return a newly created Spreadmap if this list is not null and its size is positive.
+ * @param self a map to be converted into a SpreadMap
+ * @return a newly created SpreadMap if this map is not null and its size is positive.
* @see groovy.lang.SpreadMap#SpreadMap(java.util.Map)
* @since 1.0
*/
@@ -5840,9 +5842,11 @@ public static SpreadMap toSpreadMap(Map self) {
/**
* Creates a spreadable map from this array.
+ * <p/>
* @param self an object array
- * @return a newly created Spreadmap
+ * @return a newly created SpreadMap
* @see groovy.lang.SpreadMap#SpreadMap(java.lang.Object[])
+ * @see #toSpreadMap(java.util.Map)
* @since 1.0
*/
public static SpreadMap toSpreadMap(Object[] self) {
@@ -5855,6 +5859,24 @@ else if (self.length % 2 != 0)
}
/**
+ * Creates a spreadable map from this list.
+ * <p/>
+ * @param self a list
+ * @return a newly created SpreadMap
+ * @see groovy.lang.SpreadMap#SpreadMap(java.util.List)
+ * @see #toSpreadMap(java.util.Map)
+ * @since 1.8.0
+ */
+ public static SpreadMap toSpreadMap(List self) {
+ if (self == null)
+ throw new GroovyRuntimeException("Fail to convert List to SpreadMap, because it is null.");
+ else if (self.size() % 2 != 0)
+ throw new GroovyRuntimeException("Fail to convert List to SpreadMap, because it's size is not even.");
+ else
+ return new SpreadMap(self);
+ }
+
+ /**
* Wraps a map using the delegate pattern with a wrapper that intercepts all calls
* to <code>get(key)</code>. If an unknown key is found, a default value will be
* stored into the Map before being returned. The default value stored will be the

0 comments on commit e3a842f

Please sign in to comment.