Revision: 56
http://mud4j.svn.sourceforge.net/mud4j/?rev=56&view=rev
Author: mpurland
Date: 2006-12-24 10:36:06 -0800 (Sun, 24 Dec 2006)
Log Message:
-----------
Add container item type.
Added Paths:
-----------
trunk/mud4j-core/src/java/net/sf/mud4j/world/item/ContainerItemType.java
Added: trunk/mud4j-core/src/java/net/sf/mud4j/world/item/ContainerItemType.java
===================================================================
--- trunk/mud4j-core/src/java/net/sf/mud4j/world/item/ContainerItemType.java (rev 0)
+++ trunk/mud4j-core/src/java/net/sf/mud4j/world/item/ContainerItemType.java 2006-12-24 18:36:06 UTC (rev 56)
@@ -0,0 +1,99 @@
+/**
+ * Copyright 2006 Matthew Purland (m.p...@gm...)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.sf.mud4j.world.item;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Container item type for managing a collection of items within
+ * it.
+ *
+ * @author Matthew Purland
+ */
+public class ContainerItemType extends AbstractItemType {
+
+ // Capacity for the item container
+ private int initialCapacity;
+ private List<Item> itemList;
+
+ /**
+ * Create an container item type with an initial capacity.
+ *
+ * @param initialCapacity Initial capacity of the item list.
+ */
+ public ContainerItemType(int initialCapacity) {
+ this.initialCapacity = initialCapacity;
+ itemList = new ArrayList<Item>(initialCapacity);
+ }
+
+ /**
+ * Add an item to the container.
+ *
+ * @param item Item to add to the container.
+ */
+ public void addItem(Item item) {
+ itemList.add(item);
+ }
+
+ /**
+ * Remove an item from the container.
+ *
+ * @param item Item to remove from the container.
+ */
+ public void removeItem(Item item) {
+ itemList.remove(item);
+ }
+
+ private int getInitialCapacity() {
+ return initialCapacity;
+ }
+
+ private int getSize() {
+ return itemList.size();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int compareTo(Object o) {
+ if (o instanceof ContainerItemType) {
+ ContainerItemType itemType = (ContainerItemType) o;
+
+ // First compare by initial capacity
+ if (getInitialCapacity() > itemType.getInitialCapacity()) {
+ return 1;
+ }
+ else if (getInitialCapacity() < itemType.getInitialCapacity()) {
+ return -1;
+ }
+ // If they are of equal initial capacity, then compare by
+ // their current sizes
+ else {
+ if (getSize() > itemType.getSize()) {
+ return 1;
+ }
+ else if (getSize() < itemType.getSize()) {
+ return -1;
+ }
+ }
+ }
+
+ return 0;
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|