Advertisement
EldiraSesto

Container

May 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.17 KB | None | 0 0
  1. package container;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Iterator;
  6.  
  7. import util.searchable.ISearchFilter;
  8. import util.searchable.ISearchableByFilter;
  9.  
  10. public class Container<E> implements Collection<E>, ISearchableByFilter<E> { //Eldira 11815163
  11.     public IContainerElement<E> head;
  12.     public IContainerElement<E> tail;
  13.  
  14.     //constructor
  15.     public Container() {
  16.         this.head = null;
  17.         this.tail = null;
  18.     }
  19.  
  20.     public boolean add(E e) {
  21.         if (e == null) {
  22.             throw new NullPointerException("You cant enter null");
  23.         }
  24.  
  25.         ContainerElement<E> node = new ContainerElement<>(e);
  26.         if (head == null) {
  27.             head = node;
  28.             tail = node;
  29.         }
  30.         else {
  31.             tail.setNextElement(node);
  32.             tail = node;
  33.         }
  34.  
  35.         return true;
  36.     }
  37.  
  38.  
  39.    
  40.     public boolean addAll(Collection<? extends E> c) {
  41.         if (this.containsAll(c)) return false;
  42.         for(E e : c) {
  43.             this.add(e);
  44.         }
  45.        
  46.         return true;
  47.     }
  48.    
  49.     public void clear() {
  50.         this.head = null;
  51.         this.tail = null;
  52.     }
  53.    
  54.     public boolean contains(Object o) {
  55.         IContainerElement<E> n = head;
  56.         while(n.hasNextElement()) {
  57.             if(n.getData().equals(o)) return true;
  58.             n=n.getNextElement();
  59.         }
  60.         if(n.getData().equals(o)) return true;
  61.        
  62.         return false;
  63.     }
  64.    
  65.     public int size() {
  66.         int counter=0;
  67.         IContainerElement<E> n = head;
  68.         while(n != null) {
  69.             counter += 1;
  70.             n=n.getNextElement();
  71.         }
  72.        
  73.         return counter;
  74.     }
  75.    
  76.     public E get(int index) throws IndexOutOfBoundsException{
  77.         if(index<0 || index>=this.size()) throw new IndexOutOfBoundsException ("Index is out of bounds!");
  78.  
  79.         IContainerElement<E> n = head;
  80.         int counter = 0;
  81.         while(n != null) {
  82.             if(index==counter) {
  83.                 return n.getData();
  84.             }
  85.  
  86.             n = n.getNextElement();
  87.         }
  88.  
  89.         return null;
  90.     }
  91.    
  92.     public boolean isEmpty() {
  93.         return (head==null);
  94.     }
  95.    
  96.     public boolean containsAll(Collection<?> c) {
  97.         for(Object e : c) {
  98.             if (!this.contains(e)) {
  99.                 return false;
  100.             }
  101.         }
  102.  
  103.         return true;
  104.     }
  105.    
  106.     public boolean remove(Object o) {
  107.         ContainerElement<E> current = (ContainerElement<E>) head;
  108.         ContainerElement<E> previous = null;
  109.         while (current != null){
  110.             if(current.getData().equals(o) && previous != null){
  111.                 if (current == tail) {
  112.                     tail = previous;
  113.                 }
  114.  
  115.                 previous.setNextElement(current.getNextElement());
  116.                 return true;
  117.             }else if(current.getData().equals(o) && previous == null){
  118.                 head = head.getNextElement();
  119.                 return true;
  120.             }
  121.             previous = current;
  122.             current = (ContainerElement<E>) current.getNextElement();
  123.         }
  124.    
  125.         return false;
  126.     }
  127.    
  128.    
  129.    
  130.    
  131.    
  132.    
  133.     public boolean removeAll(Collection<?> c) {
  134.         //ContainerElement<E> n = (ContainerElement<E>) head;
  135.         int brojac = 0;
  136.         for(Object e : c) {
  137.             if(this.remove(e)) brojac++;
  138.             this.remove(e);
  139.         }
  140.         if(brojac > 0) {
  141.             return true;
  142.         }
  143.         else return false;
  144.     }
  145.    
  146.    
  147.     public boolean retainAll(Collection <?> c) throws  UnsupportedOperationException{
  148.         throw new  UnsupportedOperationException("This is not supported by the container! ");
  149.     }
  150.    
  151.     public Object[] toArray() throws UnsupportedOperationException {
  152.         throw new UnsupportedOperationException("This is not supported by the container!");
  153.        
  154.         }
  155.    
  156.     public <T> T[] toArray(T[] a) throws UnsupportedOperationException {
  157.         throw new UnsupportedOperationException("This is not supported by the container!");
  158.        
  159.     }
  160.  
  161.    
  162.     public Iterator<E> iterator(){
  163.         return new Itr<E>(this.head);
  164.     }
  165.    
  166.    
  167.    
  168.     public Collection<E> searchByFilter(ISearchFilter filter, Object filterObject){
  169.  
  170.         ArrayList<E> listOfMatches = new ArrayList<>();
  171.         if(head != null){
  172.             ContainerElement<E> current = (ContainerElement<E>) head;
  173.             while (current.hasNextElement()){ //proci kroz cijelu listu
  174.                 if(filter.searchFilterFunction(current, filterObject)){ //kad god se nadje element za koji searchFilter vrati true u poredjenju s ovim
  175.                     //sto je poslan u paramtru, on se doda u listu i ta lista se vrati
  176.                     listOfMatches.add(current.getData());
  177.                 }
  178.                 current = (ContainerElement<E>) current.getNextElement(); //predji na sljedeci element
  179.             }
  180.         }
  181.  
  182.         return listOfMatches;
  183.     }
  184.    
  185.  
  186.     @Override
  187.     public String toString() {
  188.         return "Container [head=" + head + "]";
  189.     }
  190.    
  191.      
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement