MWStack.java :  » Game » magicwars » mw » server » Java Open Source

Java Open Source » Game » magicwars 
magicwars » mw » server » MWStack.java
package mw.server;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

import mw.server.model.Card;
import mw.server.model.MagicWarsModel;
import mw.server.model.SpellAbility;

public class MWStack implements Serializable, Iterable<SpellAbility> {

    private LinkedList<SpellAbility> stack = new LinkedList<SpellAbility>();

    public void clear() {
        stack.clear();
    }

    public void add(SpellAbility sp) {
      sp.addAspect(MagicWarsModel.ASPECT_NEW_SPELL_ABILITY);
        push(sp);
    }
    
    public int size() {
        return stack.size();
    }

    public void push(SpellAbility sp) {
        stack.addLast(sp);
    }

    public SpellAbility pop() {
      if (stack.isEmpty()) {
        return null;
      }
        return stack.removeLast();
    }

    /**
     * index = 0 is the top, index = 1 is the next to top, etc...
     */
    public SpellAbility peek(int index) {
      if (size() == 0) {
        return null;
      }
        return (SpellAbility) stack.get(index);
    }

    /**
     * index = 0 is the top, index = 1 is the next to top, etc...
     */
    public SpellAbility pop(int index) {
        return (SpellAbility) stack.remove(index);
    }
    
    public SpellAbility peek() {
      if (stack.isEmpty()) {
        return null;
      }
        return stack.getLast();
    }

    /**
     * Get Card objects from the stack.
     * 
     * @return list of cards in the stack
     */
    public ArrayList<Card> getSourceCards() {
        ArrayList<Card> cards = new ArrayList<Card>();
        for (SpellAbility s : stack) {
            cards.add(s.getSourceCard());
        }
        
        return cards;
    }
    
    public Iterator<SpellAbility> iterator() {
      return stack.iterator();
    }
    
    public boolean contains(SpellAbility spell) {
      for (SpellAbility sa : stack) {
        if (sa.equals(spell)) {
          return true;
        }
      }
      return false;
    }
    
    public boolean remove(SpellAbility spell) {
      Iterator<SpellAbility> i = stack.iterator();
      while (i.hasNext()) {
        SpellAbility sa = i.next();
        if (sa.equals(spell)) {
          i.remove();
        }
      }
      return false;
    }
    
    /**
     * Generated UID.
     */
    private static final long serialVersionUID = 7408768445419598098L;
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.