Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package core;
- import interfaces.Buffer;
- import interfaces.Entity;
- import model.BaseEntity;
- import java.util.ArrayList;
- import java.util.EnumSet;
- import java.util.Iterator;
- import java.util.List;
- public class Loader implements Buffer {
- private Node head;
- private Node tail;
- private int size;
- private class Node {
- private Entity value;
- private Node next;
- private Node prev;
- private Node(Entity entity){
- this.value = entity;
- }
- }
- public Loader(){
- this.size = 0;
- }
- @Override
- public void add(Entity entity) {
- Node node = new Node(entity);
- if(this.size == 0){
- this.head = node;
- this.tail = node;
- } else {
- this.tail.next = node;
- node.prev = this.tail;
- this.tail = node;
- }
- this.size++;
- }
- @Override
- public Entity extract(int id) {
- if (isEmpty()) return null;
- Node current = this.head;
- while (current != null) {
- if(current.value.getId() == id) {
- Node previous = current.prev;
- Node next = current.next;
- previous.next = next;
- next.prev = previous;
- current.next = null;
- current.prev = null;
- this.size--;
- return current.value;
- }
- current = current.next;
- }
- return null;
- }
- @Override
- public Entity find(Entity entity) {
- if (isEmpty()) return null;
- return findEntity(entity);
- }
- @Override
- public boolean contains(Entity entity) {
- if (isEmpty()) return false;
- return findEntity(entity) != null;
- }
- @Override
- public int entitiesCount() {
- return this.size;
- }
- @Override
- public void replace(Entity oldEntity, Entity newEntity) {
- if (isEmpty()) return;
- Node current = this.head;
- while (current != null) {
- if (current.value.getId() == oldEntity.getId()) {
- break;
- }
- current = current.next;
- }
- if(current == null) { entityNotFound(); }
- current.value.setId(newEntity.getId());
- }
- private void entityNotFound(){
- throw new IllegalStateException("Entity not found!");
- }
- @Override
- public void swap(Entity first, Entity second) {
- if (isEmpty()) return;
- int firstId = first.getId();
- int secondId = second.getId();
- Node current = this.head;
- Node firstFound = null;
- Node secondFound = null;
- while (current != null) {
- if (current.value.getId() == first.getId() && firstFound == null) {
- firstFound = current;
- current.value.setId(secondId);
- }else if (current.value.getId() == second.getId() && secondFound == null) {
- secondFound = current;
- current.value.setId(firstId);
- }
- if(firstFound != null && secondFound != null) break;
- current = current.next;
- }
- if(current == null || firstFound == null || secondFound == null) {
- throw new IllegalStateException("Entities not found!");
- }
- }
- @Override
- public void clear() {
- this.head = null;
- this.tail = null;
- this.size = 0;
- }
- @Override
- public Entity[] toArray() {
- Entity[] result = new Entity[this.size];
- if (isEmpty()) return result;
- Node current = this.head;
- int i = 0;
- while (current != null) {
- result[i] = current.value;
- current = current.next;
- i++;
- }
- return result;
- }
- @Override
- public List<Entity> retainAllFromTo(BaseEntity.Status lowerBound, BaseEntity.Status upperBound) {
- List<Entity> result = new ArrayList<>();
- if (isEmpty()) return result;
- Node current = this.head;
- while (current != null) {
- if(current.value.getStatus().ordinal() >= lowerBound.ordinal() &&
- current.value.getStatus().ordinal() <= upperBound.ordinal()) {
- result.add(current.value);
- }
- current = current.next;
- }
- return result;
- }
- @Override
- public List<Entity> getAll() {
- List<Entity> result = new ArrayList<>();
- if (isEmpty()) return result;
- Node current = this.head;
- while (current != null) {
- result.add(current.value);
- current = current.next;
- }
- return result;
- }
- @Override
- public void updateAll(BaseEntity.Status oldStatus, BaseEntity.Status newStatus) {
- Node current = this.head;
- while (current != null) {
- if (current.value.getStatus().ordinal() == oldStatus.ordinal()) {
- current.value.setStatus(newStatus);
- }
- current = current.next;
- }
- }
- @Override
- public void removeSold() {
- Node current = this.head;
- while (current != null) {
- Node next = null;
- Node prev = null;
- if (current.value.getStatus().equals(BaseEntity.Status.SOLD)) {
- if (current.next == null){
- this.tail = this.tail.prev;
- break;
- }
- if(current.prev != null) {
- prev = current.prev;
- next.prev = prev;
- prev.next = next;
- }
- current.prev = null;
- current.next = null;
- this.size--;
- }
- if(next!=null){
- current = next;
- }else {
- current = current.next;
- }
- }
- }
- @Override
- public Iterator<Entity> iterator() {
- return new Iterator<Entity>() {
- Node current = head;
- @Override
- public boolean hasNext() {
- return current != null;
- }
- @Override
- public Entity next() {
- Node result = current;
- current = current.next;
- return result.value;
- }
- };
- }
- private Entity findEntity(Entity entity) {
- Node current = this.head;
- while (current != null) {
- if (current.value.getId() == entity.getId()) {
- return current.value;
- }
- current = current.next;
- }
- return null;
- }
- private boolean isEmpty() {
- if (this.size == 0) return true;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement