Skip to content

Commit

Permalink
Fixed issue jcabi#14
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Wirz committed Oct 14, 2017
1 parent 1cbd9fb commit 2d6475d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/main/java/com/jcabi/immutable/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,29 @@ public Array(final Iterable<T> list) {
"list of objects can't be NULL"
);
}
if (list instanceof Array) {
this.values = ((Array<T>) list).values;
} else if (list instanceof Collection) {
final Collection<T> col = Collection.class.cast(list);
this.values = (T[]) new Object[col.size()];
col.toArray(this.values);
} else {
final Collection<T> items = new LinkedList<T>();
for (final T item : list) {
items.add(item);
}
this.values = (T[]) new Object[items.size()];
items.toArray(this.values);
final Collection<T> items = new LinkedList<T>();
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<T> array) {
this.values = array.values.clone();
}

/**
* Public ctor, from a generic collection.
* @param collection Collection with items to encapsulate
*/
public Array(final Collection<T> collection) {
this.values = (T[]) new Object[collection.size()];
collection.toArray(this.values);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/jcabi/immutable/ArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ public void encapsulatesIterables() throws Exception {
);
}

/**
* Array can encapsulate another Array instance.
*/
@Test
public void encapsulatesArrays() {
final Array<Integer> array = new Array<Integer>(Tv.TEN, Tv.FIVE);
array.with(Tv.MILLION);
MatcherAssert.assertThat(
new Array<Integer>(array),
Matchers.hasItem(Tv.TEN)
);
}

/**
* Array can find index of an object.
* @throws Exception If some problem inside
Expand Down

0 comments on commit 2d6475d

Please sign in to comment.