Advertisement
SPDG57

Loader - DS Softuni

Mar 30th, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.86 KB | None | 0 0
  1. package core;
  2.  
  3. import interfaces.Buffer;
  4. import interfaces.Entity;
  5. import model.BaseEntity;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.EnumSet;
  9. import java.util.Iterator;
  10. import java.util.List;
  11.  
  12. public class Loader implements Buffer {
  13.  
  14.     private Node head;
  15.     private Node tail;
  16.     private int size;
  17.  
  18.     private class Node {
  19.         private Entity value;
  20.         private Node next;
  21.         private Node prev;
  22.  
  23.         private Node(Entity entity){
  24.             this.value = entity;
  25.         }
  26.     }
  27.  
  28.     public Loader(){
  29.         this.size = 0;
  30.     }
  31.  
  32.     @Override
  33.     public void add(Entity entity) {
  34.         Node node = new Node(entity);
  35.  
  36.         if(this.size == 0){
  37.             this.head = node;
  38.             this.tail = node;
  39.         } else {
  40.             this.tail.next = node;
  41.             node.prev = this.tail;
  42.             this.tail = node;
  43.         }
  44.         this.size++;
  45.     }
  46.  
  47.     @Override
  48.     public Entity extract(int id) {
  49.         if (isEmpty()) return null;
  50.  
  51.         Node current = this.head;
  52.  
  53.         while (current != null) {
  54.             if(current.value.getId() == id) {
  55.                 Node previous = current.prev;
  56.                 Node next     = current.next;
  57.  
  58.                 previous.next = next;
  59.                 next.prev     = previous;
  60.  
  61.                 current.next = null;
  62.                 current.prev = null;
  63.                 this.size--;
  64.  
  65.                 return current.value;
  66.             }
  67.             current = current.next;
  68.         }
  69.         return null;
  70.     }
  71.  
  72.     @Override
  73.     public Entity find(Entity entity) {
  74.         if (isEmpty()) return null;
  75.         return findEntity(entity);
  76.     }
  77.  
  78.     @Override
  79.     public boolean contains(Entity entity) {
  80.         if (isEmpty()) return false;
  81.         return findEntity(entity) != null;
  82.     }
  83.  
  84.     @Override
  85.     public int entitiesCount() {
  86.         return this.size;
  87.     }
  88.  
  89.     @Override
  90.     public void replace(Entity oldEntity, Entity newEntity) {
  91.         if (isEmpty()) return;
  92.         Node current = this.head;
  93.  
  94.         while (current != null) {
  95.             if (current.value.getId() == oldEntity.getId()) {
  96.                 break;
  97.             }
  98.             current = current.next;
  99.         }
  100.         if(current == null) { entityNotFound(); }
  101.  
  102.         current.value.setId(newEntity.getId());
  103.     }
  104.  
  105.     private void entityNotFound(){
  106.         throw new IllegalStateException("Entity not found!");
  107.     }
  108.  
  109.     @Override
  110.     public void swap(Entity first, Entity second) {
  111.         if (isEmpty()) return;
  112.  
  113.         int firstId = first.getId();
  114.         int secondId = second.getId();
  115.  
  116.         Node current = this.head;
  117.  
  118.         Node firstFound = null;
  119.         Node secondFound = null;
  120.  
  121.         while (current != null) {
  122.             if (current.value.getId() == first.getId() && firstFound == null) {
  123.                 firstFound = current;
  124.                 current.value.setId(secondId);
  125.             }else if (current.value.getId() == second.getId() && secondFound == null) {
  126.                 secondFound = current;
  127.                 current.value.setId(firstId);
  128.             }
  129.             if(firstFound != null && secondFound != null) break;
  130.  
  131.             current = current.next;
  132.         }
  133.  
  134.         if(current == null || firstFound == null || secondFound == null) {
  135.             throw new IllegalStateException("Entities not found!");
  136.         }
  137.     }
  138.  
  139.     @Override
  140.     public void clear() {
  141.         this.head = null;
  142.         this.tail = null;
  143.         this.size = 0;
  144.     }
  145.  
  146.     @Override
  147.     public Entity[] toArray() {
  148.         Entity[] result = new Entity[this.size];
  149.  
  150.         if (isEmpty()) return result;
  151.  
  152.         Node current = this.head;
  153.         int i = 0;
  154.         while (current != null) {
  155.             result[i] = current.value;
  156.             current = current.next;
  157.             i++;
  158.         }
  159.         return result;
  160.     }
  161.  
  162.     @Override
  163.     public List<Entity> retainAllFromTo(BaseEntity.Status lowerBound, BaseEntity.Status upperBound) {
  164.         List<Entity> result = new ArrayList<>();
  165.  
  166.         if (isEmpty()) return result;
  167.  
  168.  
  169.         Node current = this.head;
  170.         while (current != null) {
  171.             if(current.value.getStatus().ordinal() >= lowerBound.ordinal() &&
  172.                     current.value.getStatus().ordinal() <= upperBound.ordinal()) {
  173.                 result.add(current.value);
  174.             }
  175.                 current = current.next;
  176.         }
  177.  
  178.         return result;
  179.     }
  180.  
  181.     @Override
  182.     public List<Entity> getAll() {
  183.         List<Entity> result = new ArrayList<>();
  184.  
  185.         if (isEmpty()) return result;
  186.  
  187.         Node current = this.head;
  188.         while (current != null) {
  189.             result.add(current.value);
  190.             current = current.next;
  191.         }
  192.  
  193.         return result;
  194.     }
  195.  
  196.     @Override
  197.     public void updateAll(BaseEntity.Status oldStatus, BaseEntity.Status newStatus) {
  198.         Node current = this.head;
  199.         while (current != null) {
  200.             if (current.value.getStatus().ordinal() == oldStatus.ordinal()) {
  201.                 current.value.setStatus(newStatus);
  202.             }
  203.             current = current.next;
  204.         }
  205.     }
  206.  
  207.     @Override
  208.     public void removeSold() {
  209.         Node current = this.head;
  210.         while (current != null) {
  211.             Node next = null;
  212.             Node prev = null;
  213.  
  214.             if (current.value.getStatus().equals(BaseEntity.Status.SOLD)) {
  215.                 if (current.next == null){
  216.                     this.tail = this.tail.prev;
  217.                     break;
  218.                 }
  219.  
  220.                 if(current.prev != null) {
  221.                     prev = current.prev;
  222.                     next.prev = prev;
  223.                     prev.next = next;
  224.                 }
  225.  
  226.                 current.prev = null;
  227.                 current.next = null;
  228.                 this.size--;
  229.             }
  230.             if(next!=null){
  231.                 current = next;
  232.             }else {
  233.                 current = current.next;
  234.             }
  235.  
  236.         }
  237.     }
  238.  
  239.     @Override
  240.     public Iterator<Entity> iterator() {
  241.  
  242.         return new Iterator<Entity>() {
  243.             Node current = head;
  244.  
  245.             @Override
  246.             public boolean hasNext() {
  247.                 return current != null;
  248.             }
  249.  
  250.             @Override
  251.             public Entity next() {
  252.                 Node result = current;
  253.                 current = current.next;
  254.                 return result.value;
  255.             }
  256.         };
  257.     }
  258.  
  259.     private Entity findEntity(Entity entity) {
  260.         Node current = this.head;
  261.  
  262.         while (current != null) {
  263.             if (current.value.getId() == entity.getId()) {
  264.  
  265.                 return current.value;
  266.             }
  267.             current = current.next;
  268.         }
  269.         return null;
  270.     }
  271.  
  272.     private boolean isEmpty() {
  273.         if (this.size == 0) return true;
  274.         return false;
  275.     }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement