package com.levelup.java.collections;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.Test;

import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;

/**
 * This java example will demonstrate converting an array
 * to list.
 * 
 * @author Justin Musgrove
 * @see <a href='http://www.leveluplunch.com/java/'></a>
 * 
 */
public class ConvertArrayToList {

	@Test
	public void convert_string_array_to_list_with_java () {

		String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
				"Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };
		
		List<String> planetsAsArrayList = Arrays.asList(planetsAsStringArray);

		assertEquals(9, planetsAsArrayList.size());
	}

	@Test
	public void convert_string_array_to_list_with_java_using_collections () {

		String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
				"Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };
		
		List<String> planetsAsArrayList = new ArrayList<String>();
		Collections.addAll(planetsAsArrayList, planetsAsStringArray);
		
		assertEquals(9, planetsAsArrayList.size());
	}

	@Test
	public void convert_primitive_array_to_list_with_java () {

		int[] someNotSoRandomAscii = {98, 101, 101, 114, 66, 69, 69, 82};
		
		List<Integer> someNotSoRandomAsciiAsList = new ArrayList<Integer>(someNotSoRandomAscii.length);
		for (int x = 0; x < someNotSoRandomAscii.length; x++) {
			someNotSoRandomAsciiAsList.add(someNotSoRandomAscii[x]);
		}
		assertEquals(8, someNotSoRandomAsciiAsList.size());
	}
	
	@Test
	public void convert_string_array_to_list_with_guava () {
		
		String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
				"Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

		List<String> planetsAsArrayList = Lists
				.newArrayList(planetsAsStringArray);

		assertEquals(9, planetsAsArrayList.size());
	}

	@Test
	public void convert_primitive_array_to_list_with_guava () {
		int[] someNotSoRandomAscii = {98, 101, 101, 114, 66, 69, 69, 82};
		
		List<Integer> someNotSoRandomAsciiAsList = Ints.asList(someNotSoRandomAscii);

		assertEquals(8, someNotSoRandomAsciiAsList.size());
	}

	@Test
	public void convert_string_array_to_list_with_apachecommons () {

		String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
				"Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

		List<String> planetsAsArrayList = new ArrayList<String>();
		CollectionUtils.addAll(planetsAsArrayList, planetsAsStringArray);
		
		assertEquals(9, planetsAsArrayList.size());
	}

	@Test
	public void convert_primitive_array_to_list_with_apachecommons () {

		int[] someNotSoRandomAscii = {98, 101, 101, 114, 66, 69, 69, 82};

		List<Integer> someNotSoRandomAsciiAsList = Arrays.asList(ArrayUtils.toObject(someNotSoRandomAscii));

		assertEquals(8, someNotSoRandomAsciiAsList.size());
	}
	
	@Test
	public void convert_string_array_to_list_with_spring () {

		String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
				"Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

		@SuppressWarnings("unchecked")
		List<String> planetsAsArrayList = org.springframework.util.CollectionUtils.arrayToList(planetsAsStringArray);
		
		assertEquals(9, planetsAsArrayList.size());
		
	}

	@Test
	public void convert_primitive_array_to_list_with_spring () {

		int[] someNotSoRandomAscii = {98, 101, 101, 114, 66, 69, 69, 82};
		
		@SuppressWarnings("unchecked")
		List<Integer> someNotSoRandomAsciiAsList = org.springframework.util.CollectionUtils.arrayToList(someNotSoRandomAscii);

		assertEquals(8, someNotSoRandomAsciiAsList.size());

	}
	
}

@@ -550,10 +550,62 @@
 ava/
-'%3E
+examples/convert-array-to-list/'%3EConvert array to list
 %3C/a%3E
@@ -2975,24 +2975,29 @@
 ciiAsList = 
+%0A%09%09%09%09
 Arrays.asLis
@@ -3387,32 +3387,37 @@
 ngframework.util
+%0A%09%09%09%09
 .CollectionUtils
@@ -3500,18 +3500,16 @@
 size());
-%0A%09
 %09%0A%09%7D%0A%0A%09@
@@ -3745,24 +3745,29 @@
 amework.util
+%0A%09%09%09%09
 .CollectionU
@@ -3861,16 +3861,15 @@
 ize());%0A
-%0A
 %09%7D%0A%09%0A%7D%0A

format add url