Container.java

jmud-0.11/src/Container.java · 3.3 KB

From the archive jmud-0_11.tgz

Download the archive   « Back to the archive contents

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Enumeration;

import util.list.List;
import util.list.Iterator;
import util.nice.NiceQueue;
import util.nice.Searcher;
import util.nice.NiceKey;
import util.nice.NiceQueue;
import util.nice.NiceEnumeration;
import misc.Separators;
import clone.CloneList;
import clone.CloneAlgo;

class Container extends Item implements Owner {

  protected CloneList theItems       = new CloneList();
  //  private List theItems  = new List();
  private int  capacity  = 0;
  private int  available = 0;

  Container(ContainerProto c) {
    super(c);

    theItems  = new CloneList();
    capacity  = c.capacity;
    available = c.capacity;
  }

  protected void finalize() {
    super.finalize();
  }

  int getType() {
    return T_CONTAINER;
  }

  String getFullDescription() {
    String list = getItemList();
    return getDescription() + Separators.NL + "Contém:" + ((list.length() == 0) ? (Separators.NL + "Nada") : list);
  }

  Owner getContainer() {
    return this;
  }

  //////////////////////////
  // Implementação de Owner:

  public int getId() {
    return super.getId();
  }

  public String getOwnerType() {
    return "recipiente";
  }

  private void addLoadOf(Item it) {
    int load = it.getLoad();
    available -= load;
    theLoad += load;
  }

  private void subLoadOf(Item it) {
    int load = it.getLoad();
    available += load;
    theLoad -= load;
  }

  public void insertItem(Item it) {
    CloneAlgo.insert(theItems, it);
    addLoadOf(it);
  }

  public void removeItem(Item anItem) throws NoSuchElementException {
    theItems.remove(anItem);
    subLoadOf(anItem);
  }

  public Item findItemById(int itemId) throws NoSuchElementException {
    return (Item) Searcher.linearSearch(theItems, new NiceKey(itemId));
  }

  public Item findItemByName(String name, int ind) throws NoSuchElementException {
    return (Item) Searcher.linearSearch(theItems, new NiceKey(name), ind);
  }

  public NiceQueue findItemsByName(IndexParser ip) {
    return SearcherAdaptor.findItemsByName(theItems, ip);
  }

  public String getItemList() {
    return CloneAlgo.getList(theItems, null, CloneAlgo.F_NAME);
  }

  public void extractItems() {
    for (NiceEnumeration items = theItems.elements(); items.hasMoreElements(); ) {
      Item curr = (Item) items.nextElement();
      Owner ow = curr.getContainer();
      if (ow != null)
	ow.extractItems();
      theWorld.removeItem(curr);
    }
  }

  public boolean hasFreeLoad(int load) {
    return load <= available;
  }

   public BoardItem findBoard() throws NoSuchElementException {
       for (NiceEnumeration enum = theItems.elements(); enum.hasMoreElements(); ) {
	   Item it = (Item) enum.nextElement();
	    if (it instanceof BoardItem)
		return (BoardItem) it;
       }

	throw new NoSuchElementException();
    }

    public NiceEnumeration getContents() {
	return theItems.elements();
    }

  //
  //////////////////////////

  /////////////
  // Sheetable:

  public String getSheet() {
    return super.getSheet() + Separators.NL +
      "Capacidade: " + capacity + "  Disponível: " + available;
  }

    //
    //////////////


    void save(BufferedWriter writer) throws IOException {
	super.save(writer);
	writer.write(String.valueOf(capacity)); writer.newLine();
    }

}

Site Stats

Snippets: 1791
Codebases: 571