*/
package org.mxchange.jshopcore.model.basket;
-import java.io.IOException;
-import java.io.Serializable;
-import java.lang.reflect.InvocationTargetException;
-import java.sql.SQLException;
-import java.text.MessageFormat;
-import java.util.Map;
-import org.mxchange.jcore.BaseFrameworkSystem;
-import org.mxchange.jshopcore.model.item.AddableBasketItem;
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException;
/**
- * A general basket class
+ * A general basket class. This class does not store any properties, it only
+ * contains logic for handling the items (T).
*
- * @author Roland Haeder
+ * @author Roland Haeder<roland@mxchange.org>
* @param <T> Any instance that implements AddableBasketItem
*/
-public abstract class BaseBasket<T extends AddableBasketItem> extends BaseFrameworkSystem implements Basket<T>, Serializable {
+public abstract class BaseBasket<T extends AddableBasketItem> implements Basket<T> {
+
/**
* Serial number
*/
- private static final long serialVersionUID = 784_396_762_230_845_717L;
+ private static final long serialVersionUID = 782_396_762_230_845_717L;
/**
- * Protected constructor with session instance
- *
- * @throws java.sql.SQLException If an SQL error occurs
+ * Ordered item list
*/
- protected BaseBasket () throws SQLException {
- // Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
- }
+ private final Deque<T> deque;
- @Override
- public void init () throws SQLException {
- // Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
-
- // Is the bundle initialized?
- if (!BaseFrameworkSystem.isBundledInitialized()) {
- // Temporary initialize default bundle
- // TODO The enum Gender uses this
- this.initBundle();
- }
+ /**
+ * Protected constructor with session instance
+ */
+ protected BaseBasket () {
+ // Init queue
+ this.deque = new LinkedList<>();
}
@Override
- @SuppressWarnings ("unchecked")
- public void addItem (final T item) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
- // Trace call
- this.getLogger().trace(MessageFormat.format("item={0} - CALLED!", item)); //NOI18N
-
+ 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());
-
- // Trace call
- this.getLogger().trace("EXIT!"); //NOI18N
+ // Add it here
+ this.deque.add(item);
}
@Override
- public boolean isEmpty () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
- // 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
- @SuppressWarnings("unchecked")
- public Map<Long, T> getAll () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
- // Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
-
+ public List<T> getAll () {
// Init map
- // TODO: Map<Long, T> map = ((BasketFrontend) this.getFrontend()).getAll();
- Map<Long, T> map = null;
-
- // Trace message
- this.getLogger().trace("map=" + map); //NOI18N
+ 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 AddableBasketItem getLast () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
- // Deligate to frontend
- // TODO: return ((BasketFrontend) this.getFrontend()).getLast();
- throw new UnsupportedOperationException("Not yet implmeneted.");
+ public T getLast () {
+ // Deligate to list
+ return this.deque.getLast();
}
@Override
public int getLastNumRows () {
- // Deligate to frontend
- // TODO: return this.getFrontend().getLastNumRows();
- throw new UnsupportedOperationException("Not yet implmeneted.");
+ // Is the list empty?
+ assert this.isEmpty() : "deque is empty"; //NOI18N
+
+ // It is size-1
+ return (this.deque.size() - 1);
}
@Override
- public boolean isAdded (final T item) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
- // Trace call
- this.getLogger().trace(MessageFormat.format("item={0} - CALLED!", item)); //NOI18N
-
+ public boolean isAdded (final T item) {
// item must not be null
if (null == item) {
// Then abort here
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();
- // Trace message
- this.getLogger().trace(MessageFormat.format("isAdded={0} - EXIT!", isAdded)); //NOI18N
+ // 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();
+ }
}