]> git.mxchange.org Git - jshop-core.git/blobdiff - src/org/mxchange/jshopcore/model/basket/BaseBasket.java
Continued:
[jshop-core.git] / src / org / mxchange / jshopcore / model / basket / BaseBasket.java
index 54ac27660a0e4b13d05c82669272742b92709a34..3e7f2781b8c9f93383fcbd35812cc76b47d094bf 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Roland Haeder
+ * Copyright (C) 2016 Roland Häder
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  */
 package org.mxchange.jshopcore.model.basket;
 
-import java.util.Map;
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException;
 
 /**
  * A general basket class. This class does not store any properties, it only
  * contains logic for handling the items (T).
- *
- * @author Roland Haeder
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
  * @param <T> Any instance that implements AddableBasketItem
  */
 public abstract class BaseBasket<T extends AddableBasketItem> implements Basket<T> {
+
        /**
         * Serial number
         */
        private static final long serialVersionUID = 782_396_762_230_845_717L;
 
+       /**
+        * Ordered item list
+        */
+       private final Deque<T> deque;
+
        /**
         * Protected constructor with session instance
         */
        protected BaseBasket () {
+               // Init queue
+               this.deque = new LinkedList<>();
        }
 
        @Override
-       public void init () {
-       }
-
-       @Override
-       public void addItem (final T item) {
+       public void addItem (final T item) throws BasketItemAlreadyAddedException {
                // item must not be null
                if (null == item) {
                        // Then abort here
                        throw new NullPointerException("item is null"); //NOI18N
                } else if (this.isAdded(item)) {
                        // Already been added
-                       throw new IllegalArgumentException("item has already been added. Did you miss to call isAdded()?"); //NOI18N
+                       throw new BasketItemAlreadyAddedException(item); //NOI18N
                }
 
-               // Add item to database
-               // TODO: ((BasketFrontend) this.getFrontend()).addItem(item, this.getSessionId());
+               // Add it here
+               this.deque.add(item);
        }
 
        @Override
-       public boolean isEmpty () {
-               // Deligate call to frontend
-               // TODO: return ((BasketFrontend) this.getFrontend()).isEmpty();
-               throw new UnsupportedOperationException("Not yet implmeneted.");
+       public void clear () {
+               // Deligate to deque
+               this.deque.clear();
        }
 
        @Override
-       public Map<Long, T> getAll () {
+       public List<T> getAll () {
                // Init map
-               // TODO: Map<Long, T> map = ((BasketFrontend) this.getFrontend()).getAll();
-               Map<Long, T> map = null;
+               List<T> list = new LinkedList<>();
+
+               // Iterate over full item list
+               for (final T item : this.deque) {
+                       // item should not be null
+                       if (null == item) {
+                               // Abort here
+                               throw new NullPointerException("item is null"); //NOI18N
+                       }
+
+                       // Add to map, use the item's id as key
+                       list.add(item);
+               }
 
                // Return it
-               return map;
+               return list;
        }
 
        @Override
        public T getLast () {
-               // Deligate to frontend
-               // TODO: return ((BasketFrontend) this.getFrontend()).getLast();
-               throw new UnsupportedOperationException("Not yet implmeneted.");
-       }
-
-       @Override
-       public int getLastNumRows () {
-               // Deligate to frontend
-               // TODO: return this.getFrontend().getLastNumRows();
-               throw new UnsupportedOperationException("Not yet implmeneted.");
+               // Deligate to list
+               return this.deque.getLast();
        }
 
        @Override
@@ -95,11 +104,38 @@ public abstract class BaseBasket<T extends AddableBasketItem> implements Basket<
                        throw new NullPointerException("item is null"); //NOI18N
                }
 
-               // Call map's method
-               // TODO: boolean isAdded = ((BasketFrontend) this.getFrontend()).isAdded(item, this.getSessionId());
-               boolean isAdded = true;
+               // Get all items
+               List<T> list = this.getAll();
 
+               // Default is not found
+               boolean isAdded = false;
+
+               // Loop through list
+               for (final T i : list) {
+                       // Compare id
+                       if (i.equals(item)) {
+                               // Okay, found it
+                               isAdded = true;
+                               break;
+                       }
+               }
                // Return it
                return isAdded;
        }
+
+       @Override
+       public boolean isEmpty () {
+               // Deligate call to frontend
+               return this.deque.isEmpty();
+       }
+
+       @Override
+       public int size () {
+               // Is the list empty?
+               assert this.isEmpty() : "deque is empty"; //NOI18N
+
+               // It is size-1
+               return this.deque.size();
+       }
+
 }