Can this code be improved from an OOP paradigm perspective?
package JavaCollections;
import java.util.Iterator;
/**
* Dlist using sentinel node
*
* @author mohet01
*
* @param <T>
*/
public class DblyLinkList<T> implements Iterable<T>{
/*
* Representation - starts
*
*
*/
/**
* DListNode is a node in a DblyLinkList
*
* @author mohet01
*
*/
private class DListNode<T> {
/**
* item references the item stored in the current node. prev references the
* previous node in the DList. next references the next node in the DList.
*
*
*/
T item;
DListNode<T> prev;
DListNode<T> next;
/**
* DListNode() constructor.
*/
DListNode() {
this.item = null;
this.prev = null;
this.next = null;
}
DListNode(T item) {
this.item = item;
this.prev = null;
this.next = null;
}
}
/**
* head references the sentinel node.
*
* Being sentinel as part of implementation detail, will avoid null checks,
* while performing mutable operations on list.
*
* DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
*
*/
private DListNode<T> head;
private long size;
private class Itr implements Iterator<T>{
private DListNode<T> currentPosition;
public Itr(){
currentPosition = head.next;
}
@Override
public boolean hasNext() {
return currentPosition.next != currentPosition;
}
@Override
public T next() {
currentPosition = currentPosition.next;
return currentPosition.item;
}
}
/*
* Representation - end
*
* DblyLinkList invariants:
* 1) head != null.
* 2) For any DListNode x in a DblyLinkList, x.next != null.
* 3) For any DListNode x in a DblyLinkList, x.prev != null.
* 4) For any DListNode x in a DblyLinkList, if x.next == y, then y.prev == x.
* 5) For any DListNode x in a DblyLinkList, if x.prev == y, then y.next == x.
* 6) size is the number of DListNode's, NOT COUNTING the sentinel (referenced
* by "head"), that can be accessed from the sentinel by a sequence of "next" references.
*/
/*
* Interface - starts
*/
/**
* DblyLinkList() constructor for an empty DblyLinkList.
*/
public DblyLinkList() {
this.head = new DListNode<T>();
this.head.item = null;
this.head.next = this.head;
this.head.prev = this.head;
this.size = 0;
}
/**
* DblyLinkList() constructor for a one-node DblyLinkList.
*/
public DblyLinkList(T item) {
this.head = new DListNode<T>();
this.head.item = null;
this.head.next = new DListNode<T>();
this.head.next.item = item;
this.head.prev = this.head.next;
this.head.next.prev = this.head;
this.head.prev.next = this.head;
this.size = 1;
}
/**
* Inserts a non-sentinel node at front of the list.
*
* @param item
*/
public void insertFront(T item){
DListNode<T> node = new DListNode<>(item);
node.next = head.next;
node.prev = head.next.prev;
node.next.prev = node;
head.next = node;
this.size++;
}
/**
* Remove first non-sentinel node from the list.
* Do not require size check before remove operation
*
*/
public void removeFront(){
head.next.item = null;
head.next.next.prev = head;
head.next = head.next.next;
if(this.size > 0){
this.size--;
}
}
public T get(int index){ // index 1 is first element
if (index <= size){
DListNode<T> node = head; //sentinel node
while(index > 0){
node = node.next; //linear search
index--;
}
return node.item;
}else{
return null;
}
}
@Override
public Iterator<T> iterator() {
return new Itr();
}
}
DblyLinkList
is a data abstraction which is nothing but a barrier between representation and usage (interface as mentioned).