001: /*
002: * OperandStack.java Copyright (c) 2006,07 Swaroop Belur
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008:
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013:
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
017: *
018: */
019:
020: package net.sf.jdec.core;
021:
022: import java.util.EmptyStackException;
023: import java.util.Stack;
024:
025: import net.sf.jdec.exceptions.StackOperationException;
026: import net.sf.jdec.util.AllExceptionHandler;
027:
028: public class OperandStack extends Stack {
029:
030: public OperandStack() {
031: super ();
032: }
033:
034: /***
035: * Below are methods which may be useful
036: * while doing some Error checking
037: * Actually Not Needed directly as the variable
038: * can be accessed....BUT Added Them
039: * Since it is extending Stack Class and not
040: * doing anything else
041: * @return
042: */
043:
044: public int getEntryCount() {
045: return this .elementCount;
046: }
047:
048: public Operand getTopOfStack() {
049: Operand topofstack = (Operand) this .pop();
050: return topofstack;
051: }
052:
053: public Operand peekTopOfStack() {
054: if (this .isEmpty() == false) {
055: Operand topofstack = (Operand) this .peek();
056: return topofstack;
057: }
058: return null;
059: }
060:
061: public Integer getStackTopType() {
062: Operand topofstack = (Operand) this .peek();
063: return new Integer(topofstack.getOperandType());
064: }
065:
066: /**
067: * Just A error or Debug method ...Can be used in case of ClassCastException
068: * The Advantage of This method is that it will set the position of the First
069: * corrupt Object in the stack...(If there are many , it will set the first)
070: * Usage
071: * 1> Call checkStackSanity
072: * 2>Then Either
073: * Call getWrongClassEntryPos For That operandStack(this class) Object
074: * Or
075: * Call getCorruptObject
076: *
077: * @return
078: */
079:
080: public boolean checkStackSanity() {
081: boolean ok = true;
082: int count = this .getEntryCount();
083: for (int i = 0; i < count; i++) {
084: Object o = this .peek();
085: if (o instanceof Operand) {
086: ok = true;
087: continue;
088: } else {
089: ok = false;
090: setCorruptEntryPosition(i);
091: break;
092: }
093: }
094: return ok;
095:
096: }
097:
098: private void setCorruptEntryPosition(int i) {
099: wrongClassEntryPos = i;
100: }
101:
102: private int wrongClassEntryPos = -1;
103:
104: /***
105: *
106: * @return position in stack of First corrupt object
107: * If the user desires the object itself Then
108: * See getCorruptObject() Method
109: *
110: */
111:
112: public int getWrongClassEntryPos() {
113: return wrongClassEntryPos;
114: }
115:
116: /***
117: *
118: * @return Either null or the corruptObject
119: * NOTE: This method returns null in case the
120: * user calls this method before calling
121: * checkStackSanity and ClassCastException
122: * has occurred OR There is no corrupt
123: * object in stack.
124: */
125:
126: public Object getCorruptObject() {
127: if (wrongClassEntryPos != -1) {
128: Object corruptObj = this .get(wrongClassEntryPos);
129: return corruptObj;
130: } else {
131: return null;
132: }
133:
134: }
135:
136: public Object push(Object value) {
137:
138: try {
139: if (value == null) {
140: throw new StackOperationException(
141: "Error while Pushing operand into Stack...\nnull Object Passed while trying to push operand to stack");
142: } else if (!(value instanceof Operand)) {
143: throw new StackOperationException(
144: "Error while Pushing operand into Stack... \nObject Passed is not type of Operand Class....");
145: }
146:
147: else // OK
148: {
149: Operand newtopofstack = (Operand) value;
150: return super .push(newtopofstack);
151: }
152:
153: } catch (StackOperationException stoe) {
154: AllExceptionHandler handler = new AllExceptionHandler(stoe);
155: handler.reportException();
156: return null;
157:
158: }
159:
160: }
161:
162: /***
163: * What was i thinking here..? can be removed i guess..
164: * @return
165: */
166: public boolean emptyMe() {
167: try {
168: for (;;) {
169: Object temp = this .getTopOfStack();
170: }
171:
172: } catch (EmptyStackException ese) {
173: // OK
174: return (this .getEntryCount() == 0); // Should Be True
175: }
176:
177: }
178:
179: }
|