From 2d6475d6ad67d32dc228fbdebf1685c9b096e016 Mon Sep 17 00:00:00 2001 From: Alexander Wirz Date: Sat, 14 Oct 2017 14:08:55 +0200 Subject: [PATCH] Fixed issue #14 --- src/main/java/com/jcabi/immutable/Array.java | 35 ++++++++++++------- .../java/com/jcabi/immutable/ArrayTest.java | 13 +++++++ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/jcabi/immutable/Array.java b/src/main/java/com/jcabi/immutable/Array.java index ac74d31..0b691b3 100644 --- a/src/main/java/com/jcabi/immutable/Array.java +++ b/src/main/java/com/jcabi/immutable/Array.java @@ -90,20 +90,29 @@ public Array(final Iterable list) { "list of objects can't be NULL" ); } - if (list instanceof Array) { - this.values = ((Array) list).values; - } else if (list instanceof Collection) { - final Collection col = Collection.class.cast(list); - this.values = (T[]) new Object[col.size()]; - col.toArray(this.values); - } else { - final Collection items = new LinkedList(); - for (final T item : list) { - items.add(item); - } - this.values = (T[]) new Object[items.size()]; - items.toArray(this.values); + final Collection items = new LinkedList(); + for (final T item : list) { + items.add(item); } + this.values = (T[]) new Object[items.size()]; + items.toArray(this.values); + } + + /** + * Public ctor, from a generic array. + * @param array Array with items to encapsulate + */ + public Array(final Array array) { + this.values = array.values.clone(); + } + + /** + * Public ctor, from a generic collection. + * @param collection Collection with items to encapsulate + */ + public Array(final Collection collection) { + this.values = (T[]) new Object[collection.size()]; + collection.toArray(this.values); } /** diff --git a/src/test/java/com/jcabi/immutable/ArrayTest.java b/src/test/java/com/jcabi/immutable/ArrayTest.java index 139adb7..7ee4503 100644 --- a/src/test/java/com/jcabi/immutable/ArrayTest.java +++ b/src/test/java/com/jcabi/immutable/ArrayTest.java @@ -131,6 +131,19 @@ public void encapsulatesIterables() throws Exception { ); } + /** + * Array can encapsulate another Array instance. + */ + @Test + public void encapsulatesArrays() { + final Array array = new Array(Tv.TEN, Tv.FIVE); + array.with(Tv.MILLION); + MatcherAssert.assertThat( + new Array(array), + Matchers.hasItem(Tv.TEN) + ); + } + /** * Array can find index of an object. * @throws Exception If some problem inside