Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package socialnetwork.domain;
- import java.util.concurrent.locks.ReentrantLock;
- public class IDList<E> {
- ReentrantLock lock = new ReentrantLock();
- private objectTuple<E> head;
- private int length;
- public IDList() {
- this.head = null;
- this.length = 0;
- }
- public objectTuple<E> getHead() {
- return head;
- }
- public void append(final E object) {
- if (head == null) {
- head = new objectTuple<>(object);
- head.setNext(null);
- } else {
- objectTuple<E> cursor = head;
- while (cursor.getNext() != null) {
- cursor = cursor.next;
- }
- objectTuple<E> appendice = new objectTuple<>(object);
- cursor.setNext(appendice);
- appendice.setNext(null);
- }
- }
- public void add(final E object) {
- if (head == null) {
- head = new objectTuple<>(object);
- head.setNext(null);
- } else {
- objectTuple<E> previous = head;
- head = new objectTuple<>(object);
- head.setNext(previous);
- }
- }
- public void remove(E object) {
- assert contains(object) : "can't remove inexistant object";
- if (head.getObject() == object) {
- head = head.getNext();
- } else {
- objectTuple<E> cursor = head;
- while (object.hashCode() != cursor.getNext().getObject().hashCode()) {
- cursor = cursor.getNext();
- }
- cursor.setNext(cursor.getNext().getNext());
- }
- }
- public boolean contains(E obj) {
- if (head == null) {
- return false;
- } else {
- objectTuple<E> cursor = head;
- for (int i = 0; i < this.ListSize(); i++) {
- if (cursor.getObject() == obj) {
- return true;
- }
- cursor = cursor.getNext();
- }
- return false;
- }
- }
- public E get(int position) {
- if (position == 0) {
- return head.getObject();
- } else {
- objectTuple<E> cursor = head;
- int k = 0;
- while (position > k) {
- cursor = cursor.getNext();
- k++;
- }
- return cursor.getObject();
- }
- }
- public E getLast() {
- objectTuple<E> cursor = head;
- int k = 0;
- while (this.ListSize()-1 > k) {
- cursor = cursor.getNext();
- k++;
- }
- return cursor.getObject();
- }
- public int ListSize() {
- int size = 0;
- objectTuple<E> cursor = head;
- while (cursor != null) {
- cursor = cursor.next;
- size++;
- }
- this.length = size;
- return size;
- }
- private class objectTuple<E> {
- private E object;
- private objectTuple<E> next;
- objectTuple(E newobject) {
- this.object = newobject;
- }
- objectTuple() {
- this.object = null;
- }
- E getObject() {
- return this.object;
- }
- objectTuple<E> getNext() {
- return this.next;
- }
- void setNext(objectTuple<E> newmsg) {
- this.next = newmsg;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement