Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package container;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- import util.searchable.ISearchFilter;
- import util.searchable.ISearchableByFilter;
- public class Container<E> implements Collection<E>, ISearchableByFilter<E> { //Eldira 11815163
- public IContainerElement<E> head;
- public IContainerElement<E> tail;
- //constructor
- public Container() {
- this.head = null;
- this.tail = null;
- }
- public boolean add(E e) {
- if (e == null) {
- throw new NullPointerException("You cant enter null");
- }
- ContainerElement<E> node = new ContainerElement<>(e);
- if (head == null) {
- head = node;
- tail = node;
- }
- else {
- tail.setNextElement(node);
- tail = node;
- }
- return true;
- }
- public boolean addAll(Collection<? extends E> c) {
- if (this.containsAll(c)) return false;
- for(E e : c) {
- this.add(e);
- }
- return true;
- }
- public void clear() {
- this.head = null;
- this.tail = null;
- }
- public boolean contains(Object o) {
- IContainerElement<E> n = head;
- while(n.hasNextElement()) {
- if(n.getData().equals(o)) return true;
- n=n.getNextElement();
- }
- if(n.getData().equals(o)) return true;
- return false;
- }
- public int size() {
- int counter=0;
- IContainerElement<E> n = head;
- while(n != null) {
- counter += 1;
- n=n.getNextElement();
- }
- return counter;
- }
- public E get(int index) throws IndexOutOfBoundsException{
- if(index<0 || index>=this.size()) throw new IndexOutOfBoundsException ("Index is out of bounds!");
- IContainerElement<E> n = head;
- int counter = 0;
- while(n != null) {
- if(index==counter) {
- return n.getData();
- }
- n = n.getNextElement();
- }
- return null;
- }
- public boolean isEmpty() {
- return (head==null);
- }
- public boolean containsAll(Collection<?> c) {
- for(Object e : c) {
- if (!this.contains(e)) {
- return false;
- }
- }
- return true;
- }
- public boolean remove(Object o) {
- ContainerElement<E> current = (ContainerElement<E>) head;
- ContainerElement<E> previous = null;
- while (current != null){
- if(current.getData().equals(o) && previous != null){
- if (current == tail) {
- tail = previous;
- }
- previous.setNextElement(current.getNextElement());
- return true;
- }else if(current.getData().equals(o) && previous == null){
- head = head.getNextElement();
- return true;
- }
- previous = current;
- current = (ContainerElement<E>) current.getNextElement();
- }
- return false;
- }
- public boolean removeAll(Collection<?> c) {
- //ContainerElement<E> n = (ContainerElement<E>) head;
- int brojac = 0;
- for(Object e : c) {
- if(this.remove(e)) brojac++;
- this.remove(e);
- }
- if(brojac > 0) {
- return true;
- }
- else return false;
- }
- public boolean retainAll(Collection <?> c) throws UnsupportedOperationException{
- throw new UnsupportedOperationException("This is not supported by the container! ");
- }
- public Object[] toArray() throws UnsupportedOperationException {
- throw new UnsupportedOperationException("This is not supported by the container!");
- }
- public <T> T[] toArray(T[] a) throws UnsupportedOperationException {
- throw new UnsupportedOperationException("This is not supported by the container!");
- }
- public Iterator<E> iterator(){
- return new Itr<E>(this.head);
- }
- public Collection<E> searchByFilter(ISearchFilter filter, Object filterObject){
- ArrayList<E> listOfMatches = new ArrayList<>();
- if(head != null){
- ContainerElement<E> current = (ContainerElement<E>) head;
- while (current.hasNextElement()){ //proci kroz cijelu listu
- if(filter.searchFilterFunction(current, filterObject)){ //kad god se nadje element za koji searchFilter vrati true u poredjenju s ovim
- //sto je poslan u paramtru, on se doda u listu i ta lista se vrati
- listOfMatches.add(current.getData());
- }
- current = (ContainerElement<E>) current.getNextElement(); //predji na sljedeci element
- }
- }
- return listOfMatches;
- }
- @Override
- public String toString() {
- return "Container [head=" + head + "]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement