Skip to content

Commit

Permalink
CustomList methods dev
Browse files Browse the repository at this point in the history
  • Loading branch information
F1orin committed Jul 20, 2014
1 parent a5e2153 commit bff0bfc
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 39 deletions.
144 changes: 117 additions & 27 deletions src/main/java/ua/com/florin/customlist/CustomList.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,24 @@ public CustomList() {

@Override
public List subList(int fromIndex, int toIndex) {
return null;
// throw exception if index is out of range
if (fromIndex < 0 || toIndex > size() || toIndex < fromIndex) {
throw new IndexOutOfBoundsException();
}
List list = new CustomList();
Node n = node(fromIndex);

// if indexes are equal then return empty list
if (fromIndex == toIndex) {
return list;
}

// copy elements to new list
for (int i = fromIndex; i < toIndex; i++) {
list.add(n.getData());
n = n.getNext();
}
return list;
}

@Override
Expand All @@ -37,7 +54,7 @@ public boolean isEmpty() {

@Override
public boolean contains(Object o) {
return false;
return indexOf(o) != -1;
}

@Override
Expand All @@ -47,7 +64,15 @@ public Iterator iterator() {

@Override
public Object[] toArray() {
return new Object[0];
// create new array
Object[] objects = new Object[size()];
// init iterator
int i = 0;
// fill the array with data from the list
for (Node n = header.getNext(); n != header; n = n.getNext(), i++) {
objects[i] = n.getData();
}
return objects;
}

@Override
Expand All @@ -72,6 +97,39 @@ public boolean add(Object o) {

@Override
public boolean remove(Object o) {
if (o == null) {
for (Node n = header.getNext(); n != header; n = n.getNext()) {
if (n.getData() == null) {
// set "next" link at previous element
n.getPrevious().setNext(n.getNext());
// set "previous" link at next element
n.getNext().setPrevious(n.getPrevious());
// remove links to other elements
n.setNext(null);
n.setPrevious(null);
// decrement size
size--;
return true;
}
}
} else {
for (Node n = header.getNext(); n != header; n = n.getNext()) {
if (o.equals(n.getData())) {
// set "next" link at previous element
n.getPrevious().setNext(n.getNext());
// set "previous" link at next element
n.getNext().setPrevious(n.getPrevious());
// remove links to other elements
n.setNext(null);
n.setPrevious(null);
// remove data
n.setData(null);
// decrement size
size--;
return true;
}
}
}
return false;
}

Expand Down Expand Up @@ -102,40 +160,47 @@ public boolean retainAll(Collection c) {

@Override
public void clear() {

for (Node n = header.getNext(); n != header;) {
// remember the link to next node
// because it will be cleared during the iteration
Node next = n.getNext();
// clear data and links
n.setData(null);
n.setPrevious(null);
n.setNext(null);
// set target node to previously remembered link
n = next;
}
// set header links
header.setNext(header);
header.setPrevious(header);
// set size
size = 0;
}

@Override
public Object get(int index) {
return null;
// throw exception if index is out of range
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException();
}
// use already written method to find node by index
return node(index).getData();
}

@Override
public Object set(int index, Object element) {
return null;
}

/**
* Returns the node at specified index
*/
private Node node(int index) {
if (index < 0 || index > size) {
// throw exception if index is out of range
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException();
}

Node node = header;
if (index < (size >> 1)) {
// if index is less than half size then search from the beginning
for (int i = 0; i <= index; i++) {
node = node.getNext();
}
} else {
// search from the end
for (int i = size; i > index; i--) {
node = node.getPrevious();
}
}
return node;
// use already written method to find node by index
Node node = node(index);
// remember previous data to return it later
Object previousValue = node.getData();
// set new data value
node.setData(element);
return previousValue;
}

@Override
Expand Down Expand Up @@ -176,6 +241,7 @@ public Object remove(int index) {
@Override
public int indexOf(Object o) {
int index = 0;
// iterate through list from start to end using "next" link of each node
if (o == null) {
for (Node n = header.getNext(); n != header; n = n.getNext()) {
if (n.getData() == null) {
Expand All @@ -197,6 +263,7 @@ public int indexOf(Object o) {
@Override
public int lastIndexOf(Object o) {
int index = size();
// iterate through list from end to start using "previous" link of each node
if (o == null) {
for (Node n = header.getPrevious(); n != header; n = n.getPrevious()) {
index--;
Expand Down Expand Up @@ -225,6 +292,29 @@ public ListIterator listIterator(int index) {
return null;
}

/**
* Returns the node at specified index
*/
private Node node(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}

Node node = header;
if (index < (size >> 1)) {
// if index is less than half size then search from the beginning
for (int i = 0; i <= index; i++) {
node = node.getNext();
}
} else {
// search from the end
for (int i = size; i > index; i--) {
node = node.getPrevious();
}
}
return node;
}

private static class Node {

// reference to the next node
Expand Down
52 changes: 40 additions & 12 deletions src/test/java/CustomListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import main.java.ua.com.florin.customlist.CustomList;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.List;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -49,16 +53,21 @@ public void tearDown() throws Exception {
oneItemCustomList = null;
fiveItemsCustomList = null;
}
@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public void testSubList() throws Exception {

oneItemCustomList.add(null);
assertEquals(oneItemCustomList.get(0), fiveItemsCustomList.subList(0, 1).get(0));
assertEquals(oneItemCustomList.get(1), fiveItemsCustomList.subList(0, 2).get(1));
assertEquals(oneItemCustomList.size(), fiveItemsCustomList.subList(0, 2).size());
}

@Test
public void testSize() throws Exception {
assertEquals(0,emptyCustomList.size());
assertEquals(1,oneItemCustomList.size());
assertEquals(0, emptyCustomList.size());
assertEquals(1, oneItemCustomList.size());
}

@Test
Expand All @@ -69,7 +78,8 @@ public void testIsEmpty() throws Exception {

@Test
public void testContains() throws Exception {

assertTrue(fiveItemsCustomList.contains(firstValue));
assertFalse(emptyCustomList.contains(firstValue));
}

@Test
Expand All @@ -79,7 +89,11 @@ public void testIterator() throws Exception {

@Test
public void testToArray() throws Exception {

Object[] objects = new Object[1];
objects[0] = firstValue;
Object[] emptyObjects = new Object[0];
assertEquals(objects, oneItemCustomList.toArray());
assertEquals(emptyObjects, emptyCustomList.toArray());
}

@Test
Expand All @@ -97,9 +111,12 @@ public void testAdd() throws Exception {

@Test
public void testRemove() throws Exception {
assertFalse(oneItemCustomList.isEmpty());
assertEquals(firstValue, oneItemCustomList.remove(0));
assertTrue(oneItemCustomList.isEmpty());
assertTrue(oneItemCustomList.remove(firstValue));
assertEquals(0, oneItemCustomList.size());
customList.add(null);
customList.add(null);
assertTrue(customList.remove(null));
assertEquals(null, customList.get(0));
}

@Test
Expand Down Expand Up @@ -129,17 +146,26 @@ public void testRetainAll() throws Exception {

@Test
public void testClear() throws Exception {

emptyCustomList.add(firstValue);
assertFalse(emptyCustomList.isEmpty());
emptyCustomList.clear();
assertTrue(emptyCustomList.isEmpty());
assertEquals(-1, emptyCustomList.indexOf(firstValue));
}

@Test
public void testGet() throws Exception {

assertEquals(firstValue, fiveItemsCustomList.get(0));
assertEquals(fifthValue, fiveItemsCustomList.get(4));
expectedException.expect(IndexOutOfBoundsException.class);
emptyCustomList.get(2);
}

@Test
public void testSet() throws Exception {

customList.add(firstValue);
assertEquals(firstValue, customList.set(0, thirdValue));
assertEquals(thirdValue, customList.get(0));
}

@Test
Expand All @@ -149,7 +175,9 @@ public void testAdd1() throws Exception {

@Test
public void testRemove1() throws Exception {

assertFalse(oneItemCustomList.isEmpty());
assertEquals(firstValue, oneItemCustomList.remove(0));
assertTrue(oneItemCustomList.isEmpty());
}

@Test
Expand Down

0 comments on commit bff0bfc

Please sign in to comment.