diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/.gitignore b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/.gitignore new file mode 100644 index 0000000000..e10e727be5 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/.gitignore @@ -0,0 +1 @@ +/.metadata/ diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.project b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.project new file mode 100644 index 0000000000..47cffea382 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.project @@ -0,0 +1,17 @@ + + + collection-aggregator + + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + + diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.settings/org.eclipse.m2e.core.prefs b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..f897a7f1cb --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/pom.xml b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/pom.xml new file mode 100644 index 0000000000..a09edb357b --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/pom.xml @@ -0,0 +1,13 @@ + + 4.0.0 + com.eulerlcs.collection + collection-aggregator + 0.0.1-SNAPSHOT + pom + + + ../collection-parent + ../collection-lib + + \ No newline at end of file diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/src/site/.gitkeep b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/src/site/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.classpath b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.classpath new file mode 100644 index 0000000000..5131f04311 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.classpath @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.gitignore b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.gitignore new file mode 100644 index 0000000000..b83d22266a --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.project b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.project new file mode 100644 index 0000000000..69766f62bc --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.project @@ -0,0 +1,23 @@ + + + collection-lib + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.jdt.core.prefs b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..714351aec1 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.m2e.core.prefs b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..f897a7f1cb --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/pom.xml b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/pom.xml new file mode 100644 index 0000000000..6d1654e23a --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + + com.eulerlcs.collection + collection-parent + 0.0.1-SNAPSHOT + ../collection-parent/pom.xml + + collection-lib + + + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + + + junit + junit + + + \ No newline at end of file diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/java/com/eulerlcs/collection/ArrayList.java b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/java/com/eulerlcs/collection/ArrayList.java new file mode 100644 index 0000000000..219919719b --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/java/com/eulerlcs/collection/ArrayList.java @@ -0,0 +1,438 @@ +/** + * 90% or more copy from jdk + */ +package com.eulerlcs.collection; + +import java.util.Arrays; +import java.util.Collection; +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.NoSuchElementException; +import java.util.Objects; +import java.util.RandomAccess; +import java.util.function.Consumer; + +public class ArrayList implements List, RandomAccess { + private static final int MAXQARRAYQSIZE = 1 << 10; + private transient Object[] elementData = new Object[0]; + private int size; + private transient int modCount = 0; + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(Object o) { + if (o == null) { + for (Object obi : elementData) { + if (obi == null) { + return true; + } + } + } else { + for (Object obj : elementData) { + if (o.equals(obj)) { + return true; + } + } + } + return false; + } + + @Override + public boolean containsAll(Collection c) { + for (Object e : c) + if (!contains(e)) + return false; + return true; + } + + @Override + public Object[] toArray() { + return Arrays.copyOf(elementData, size, elementData.getClass()); + } + + @SuppressWarnings("unchecked") + @Override + public T[] toArray(T[] a) { + if (a.length < size) { + return (T[]) Arrays.copyOf(elementData, size, a.getClass()); + } else { + System.arraycopy(elementData, 0, a, 0, size); + if (a.length > size) + a[size] = null; + return a; + } + } + + @Override + public boolean add(E e) { + ensureExplicitCapacity(size + 1); // Increments modCount!! + elementData[size] = e; + size++; + return true; + } + + @Override + public void add(int index, E element) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + ensureExplicitCapacity(size + 1); // Increments modCount!! + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = element; + size++; + } + + @Override + public E remove(int index) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + modCount++; + @SuppressWarnings("unchecked") + E oldValue = (E) elementData[index]; + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + elementData[--size] = null;// clear to let GC do its work + + return oldValue; + } + + @Override + public boolean remove(Object o) { + int index = -1; + + if (o == null) { + for (int i = 0; i < size; i++) + if (elementData[i] == null) { + index = i; + break; + } + } else { + for (int i = 0; i < size; i++) + if (o.equals(elementData[i])) { + index = i; + break; + } + } + + if (index > 0) { + modCount++; + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + elementData[--size] = null;// clear to let GC do its work + + return true; + } + + return false; + } + + @Override + public boolean removeAll(Collection c) { + boolean modified = false; + for (Object obj : c) { + modified |= remove(obj); + } + + return modified; + } + + @Override + public boolean addAll(Collection c) { + Object[] a = c.toArray(); + int numNew = a.length; + ensureExplicitCapacity(size + numNew);// Increments modCount + System.arraycopy(a, 0, elementData, size, numNew); + size += numNew; + return numNew != 0; + } + + @Override + public boolean addAll(int index, Collection c) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + Object[] a = c.toArray(); + int numNew = a.length; + ensureExplicitCapacity(size + numNew);// Increments modCount + + int numMoved = size - index; + if (numMoved > 0) + System.arraycopy(elementData, index, elementData, index + numNew, numMoved); + + System.arraycopy(a, 0, elementData, index, numNew); + size += numNew; + return numNew != 0; + } + + @Override + public boolean retainAll(Collection c) { + final Object[] elementData = this.elementData; + int r = 0, w = 0; + boolean modified = false; + for (; r < size; r++) + if (c.contains(elementData[r])) + elementData[w++] = elementData[r]; + + if (w != size) { + // clear to let GC do its work + for (int i = w; i < size; i++) + elementData[i] = null; + modCount += size - w; + size = w; + modified = true; + } + + return modified; + } + + @Override + public void clear() { + modCount++; + for (int i = 0; i < size; i++) + elementData[i] = null; + + size = 0; + } + + @Override + public List subList(int fromIndex, int toIndex) { + throw new UnsupportedOperationException(); + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + return (E) elementData[index]; + } + + @SuppressWarnings("unchecked") + @Override + public E set(int index, E element) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + E oldValue = (E) elementData[index]; + elementData[index] = element; + return oldValue; + } + + @Override + public int indexOf(Object o) { + if (o == null) { + for (int i = 0; i < size; i++) + if (elementData[i] == null) + return i; + } else { + for (int i = 0; i < size; i++) + if (o.equals(elementData[i])) + return i; + } + + return -1; + } + + @Override + public int lastIndexOf(Object o) { + if (o == null) { + for (int i = size - 1; i >= 0; i--) + if (elementData[i] == null) + return i; + } else { + for (int i = size - 1; i >= 0; i--) + if (o.equals(elementData[i])) + return i; + } + + return -1; + } + + private void ensureExplicitCapacity(int minCapacity) { + modCount++; + + if (elementData.length > minCapacity) { + return; + } else if (minCapacity > MAXQARRAYQSIZE) { + throw new OutOfMemoryError(); + } + + int oldCapacity = elementData.length; + + int newCapacity = oldCapacity == 0 ? 10 : (oldCapacity + (oldCapacity >> 1)); + if (newCapacity > MAXQARRAYQSIZE) { + newCapacity = MAXQARRAYQSIZE; + } + + elementData = Arrays.copyOf(elementData, newCapacity); + } + + @Override + public Iterator iterator() { + return new Itr(); + } + + @Override + public ListIterator listIterator() { + return new ListItr(0); + } + + @Override + public ListIterator listIterator(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: " + index); + return new ListItr(index); + } + + /** + * fully copy from jdk ArrayList.Itr + */ + private class Itr implements Iterator { + int cursor; // index of next element to return + int lastRet = -1; // index of last element returned; -1 if no such + int expectedModCount = modCount; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + @SuppressWarnings("unchecked") + public E next() { + checkForComodification(); + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return (E) elementData[lastRet = i]; + } + + @Override + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + ArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + @Override + @SuppressWarnings("unchecked") + public void forEachRemaining(Consumer consumer) { + Objects.requireNonNull(consumer); + final int size = ArrayList.this.size; + int i = cursor; + if (i >= size) { + return; + } + final Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) { + throw new ConcurrentModificationException(); + } + while (i != size && modCount == expectedModCount) { + consumer.accept((E) elementData[i++]); + } + // update once at end of iteration to reduce heap write traffic + cursor = i; + lastRet = i - 1; + checkForComodification(); + } + + final void checkForComodification() { + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } + } + + /** + * fully copy from jdk ArrayList.ListItr + */ + private class ListItr extends Itr implements ListIterator { + ListItr(int index) { + super(); + cursor = index; + } + + @Override + public boolean hasPrevious() { + return cursor != 0; + } + + @Override + public int nextIndex() { + return cursor; + } + + @Override + public int previousIndex() { + return cursor - 1; + } + + @Override + @SuppressWarnings("unchecked") + public E previous() { + checkForComodification(); + int i = cursor - 1; + if (i < 0) + throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i; + return (E) elementData[lastRet = i]; + } + + @Override + public void set(E e) { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + ArrayList.this.set(lastRet, e); + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + @Override + public void add(E e) { + checkForComodification(); + + try { + int i = cursor; + ArrayList.this.add(i, e); + cursor = i + 1; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + } +} diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/resources/.gitkeep b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/resources/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/java/com/eulerlcs/collection/TestArrayList.java b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/java/com/eulerlcs/collection/TestArrayList.java new file mode 100644 index 0000000000..b66dd278e3 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/java/com/eulerlcs/collection/TestArrayList.java @@ -0,0 +1,44 @@ +package com.eulerlcs.collection; + +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestArrayList { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test_foreach() { + List list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + + int sum = 0; + for (Integer item : list) { + sum += item; + } + + Assert.assertEquals(sum, 6); + } +} diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/resources/.gitkeep b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/resources/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.project b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.project new file mode 100644 index 0000000000..3467a254de --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.project @@ -0,0 +1,17 @@ + + + collection-parent + + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + + diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.settings/org.eclipse.m2e.core.prefs b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..f897a7f1cb --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/pom.xml b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/pom.xml new file mode 100644 index 0000000000..6f05e517e2 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/pom.xml @@ -0,0 +1,112 @@ + + 4.0.0 + com.eulerlcs.collection + collection-parent + 0.0.1-SNAPSHOT + pom + + + + 1.7.23 + 4.12 + 2.17 + 1.8 + 1.8 + + + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + + + junit + junit + ${junit.version} + test + + + + + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.1 + + + attach-source + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + ${maven.compiler.source} + ${maven.compiler.target} + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.4 + + true + 1.8 + protected + UTF-8 + UTF-8 + UTF-8 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + org.apache.maven.plugins + maven-surefire-report-plugin + 2.19.1 + + true + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.17 + + + + + + + org.apache.maven.plugins + maven-source-plugin + + + + \ No newline at end of file diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/src/site/.gitkeep b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/src/site/.gitkeep new file mode 100644 index 0000000000..e69de29bb2