1 /*
2 * Copyright (C) 2006 uguu@users.sourceforge.jp, All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of Clarkware Consulting, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without prior written permission. For written
18 * permission, please contact clarkware@clarkware.com.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * CLARKWARE CONSULTING OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 package jp.sourceforge.rpn_computer;
33
34 import java.util.Stack;
35
36 /**
37 * <p>
38 * 計算中の状態を保持するコンテキストです。
39 * </p>
40 *
41 * @author uguu@users.sourceforge.jp
42 */
43 public final class RpnContext {
44
45 private Stack stack = new Stack();
46
47 /**
48 * <p>
49 * スタックに格納されている要素の数を返します。
50 * </p>
51 *
52 * @return スタックに格納されている要素の数。
53 */
54 public int sizeStack() {
55 return this.stack.size();
56 }
57
58 /**
59 * <p>
60 * スタックの先頭から値を取り出します。取り出した値は、スタックから削除されます。スタックが空の場合、{@link ComputeException}例外をスローします。
61 * </p>
62 *
63 * @return スタックから取り出した値。
64 */
65 public Double popStack() {
66 if (this.stack.empty()) {
67 throw new ComputeException("スタックが空です。");
68 }
69 return (Double) this.stack.pop();
70 }
71
72 /**
73 * <p>
74 * スタックの先頭から値を取り出します。取り出した値は、スタックの先頭から削除されません。スタックが空の場合、{@link ComputeException}例外をスローします。
75 * </p>
76 *
77 * @return スタックから取り出した値。
78 */
79 public Double peekStack() {
80 if (this.stack.empty()) {
81 throw new ComputeException("スタックが空です。");
82 }
83 return (Double) this.stack.peek();
84 }
85
86 /**
87 * <p>
88 * スタックの先頭に値を追加します。
89 * </p>
90 *
91 * @param value
92 * スタックに追加する値。nullの場合、{@link ComputeException}例外をスローします。
93 */
94 public void pushStack(Double value) {
95 if (value == null) {
96 throw new ComputeException("スタックに追加しようとした値がnullです。");
97 }
98 this.stack.push(value);
99 }
100 }