Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. package socialnetwork.domain;
  2.  
  3. import java.util.concurrent.locks.ReentrantLock;
  4.  
  5. public class IDList<E> {
  6.     ReentrantLock lock = new ReentrantLock();
  7.     private objectTuple<E> head;
  8.     private int length;
  9.  
  10.     public IDList() {
  11.         this.head = null;
  12.         this.length = 0;
  13.     }
  14.  
  15.     public objectTuple<E> getHead() {
  16.         return head;
  17.     }
  18.  
  19.     public void append(final E object) {
  20.         if (head == null) {
  21.             head = new objectTuple<>(object);
  22.             head.setNext(null);
  23.         } else {
  24.             objectTuple<E> cursor = head;
  25.             while (cursor.getNext() != null) {
  26.                 cursor = cursor.next;
  27.             }
  28.             objectTuple<E> appendice = new objectTuple<>(object);
  29.             cursor.setNext(appendice);
  30.             appendice.setNext(null);
  31.         }
  32.  
  33.     }
  34.  
  35.     public void add(final E object) {
  36.  
  37.         if (head == null) {
  38.             head = new objectTuple<>(object);
  39.             head.setNext(null);
  40.         } else {
  41.             objectTuple<E> previous = head;
  42.             head = new objectTuple<>(object);
  43.             head.setNext(previous);
  44.         }
  45.  
  46.     }
  47.  
  48.     public void remove(E object) {
  49.         assert contains(object) : "can't remove inexistant object";
  50.         if (head.getObject() == object) {
  51.             head = head.getNext();
  52.         } else {
  53.             objectTuple<E> cursor = head;
  54.             while (object.hashCode() != cursor.getNext().getObject().hashCode()) {
  55.                 cursor = cursor.getNext();
  56.             }
  57.             cursor.setNext(cursor.getNext().getNext());
  58.         }
  59.     }
  60.  
  61.     public boolean contains(E obj) {
  62.         if (head == null) {
  63.             return false;
  64.         } else {
  65.             objectTuple<E> cursor = head;
  66.             for (int i = 0; i < this.ListSize(); i++) {
  67.                 if (cursor.getObject() == obj) {
  68.                     return true;
  69.                 }
  70.                 cursor = cursor.getNext();
  71.             }
  72.             return false;
  73.         }
  74.  
  75.     }
  76.  
  77.  
  78.     public E get(int position) {
  79.         if (position == 0) {
  80.             return head.getObject();
  81.         } else {
  82.             objectTuple<E> cursor = head;
  83.             int k = 0;
  84.             while (position > k) {
  85.                 cursor = cursor.getNext();
  86.                 k++;
  87.             }
  88.             return cursor.getObject();
  89.         }
  90.  
  91.     }
  92.  
  93.  
  94.     public E getLast() {
  95.         objectTuple<E> cursor = head;
  96.         int k = 0;
  97.         while (this.ListSize()-1 > k) {
  98.             cursor = cursor.getNext();
  99.             k++;
  100.         }
  101.         return cursor.getObject();
  102.     }
  103.  
  104.     public int ListSize() {
  105.  
  106.         int size = 0;
  107.         objectTuple<E> cursor = head;
  108.         while (cursor != null) {
  109.             cursor = cursor.next;
  110.             size++;
  111.         }
  112.         this.length = size;
  113.  
  114.         return size;
  115.     }
  116.  
  117.     private class objectTuple<E> {
  118.         private E object;
  119.         private objectTuple<E> next;
  120.  
  121.         objectTuple(E newobject) {
  122.             this.object = newobject;
  123.         }
  124.  
  125.         objectTuple() {
  126.             this.object = null;
  127.         }
  128.  
  129.         E getObject() {
  130.             return this.object;
  131.         }
  132.  
  133.         objectTuple<E> getNext() {
  134.             return this.next;
  135.         }
  136.  
  137.         void setNext(objectTuple<E> newmsg) {
  138.  
  139.             this.next = newmsg;
  140.  
  141.         }
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement