Add JSONArray.toList()
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/ed524613 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/ed524613 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/ed524613 Branch: refs/heads/5.4-js-rewrite Commit: ed524613821ec28dd23ac28e8ca1cd76f2a1637f Parents: 68bcdca Author: Howard M. Lewis Ship <hls...@apache.org> Authored: Sun Jul 8 15:10:10 2012 -0700 Committer: Howard M. Lewis Ship <hls...@apache.org> Committed: Sun Jul 8 15:10:10 2012 -0700 ---------------------------------------------------------------------- .../java/org/apache/tapestry5/json/JSONArray.java | 17 +++++++- .../java/org/apache/tapestry5/json/JSONObject.java | 2 +- .../test/groovy/json/specs/JSONArraySpec.groovy | 31 +++++++++++++++ .../test/groovy/json/specs/JSONObjectSpec.groovy | 2 +- 4 files changed, 48 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ed524613/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java ---------------------------------------------------------------------- diff --git a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java index 3afb401..eaa268e 100644 --- a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java +++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java @@ -1,4 +1,4 @@ -// Copyright 2007, 2008, 2010, 2011 The Apache Software Foundation +// Copyright 2007, 2008, 2010, 2011, 2012 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -35,6 +35,7 @@ package org.apache.tapestry5.json; */ import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -466,7 +467,7 @@ public final class JSONArray extends JSONCollection implements Iterable<Object> * @return this JSONArray * @since 5.4 */ - JSONArray putAll(Iterable<Object> collection) + public JSONArray putAll(Iterable<Object> collection) { if (collection != null) { @@ -478,4 +479,16 @@ public final class JSONArray extends JSONCollection implements Iterable<Object> return this; } + + /** + * Returns an unmodifiable list of the contents of the array. This is a wrapper around the list's internal + * storage and is live (changes to the JSONArray affect the returned List). + * + * @return unmodifiable list of array contents + * @since 5.4 + */ + public List<Object> toList() + { + return Collections.unmodifiableList(list); + } } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ed524613/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java ---------------------------------------------------------------------- diff --git a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java index 33fd585..d4f8b6d 100644 --- a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java +++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java @@ -1,4 +1,4 @@ -// Copyright 2007, 2010, 2011 The Apache Software Foundation +// Copyright 2007, 2010, 2011, 2012 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ed524613/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy ---------------------------------------------------------------------- diff --git a/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy b/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy index 44ccc91..3008cf0 100644 --- a/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy +++ b/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy @@ -298,7 +298,38 @@ class JSONArraySpec extends Specification { then: array.toCompactString() == /[100,200]/ + } + + def "list returned by toList() is unmodifiable"() { + def array = new JSONArray(100, 200) + def list = array.toList() + + when: + + list.clear() + + then: + + thrown(UnsupportedOperationException) + } + + def "list from toList() is live"() { + def array = new JSONArray(100, 200) + when: + + def list = array.toList() + + then: + + list == [100, 200] + + when: + + array.remove(0) + + then: + list == [200] } } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ed524613/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy ---------------------------------------------------------------------- diff --git a/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy b/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy index 1741ce7..db99cb9 100644 --- a/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy +++ b/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy @@ -328,7 +328,7 @@ class JSONObjectSpec extends Specification { names instanceof JSONArray names.length() == 2 - names.toArray().sort() == ["barney", "fred"] + new ArrayList(names.toList()).sort() == ["barney", "fred"] } def "names() with no properties returns null"() {