Java tutorial
/* 50AH-code ========= 50 Android Hacks (http://manning.com/sessa/) book source code Copyright (c) 2012 Manning Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package roboguice.calculator.util; import android.content.SharedPreferences; import com.google.inject.Inject; import roboguice.activity.event.OnPauseEvent; import roboguice.activity.event.OnResumeEvent; import roboguice.event.Observes; import roboguice.inject.ContextSingleton; import java.math.BigDecimal; import java.util.Stack; /** * A Stack of numbers that can be operated on. * * In addition to maintaining a stack, this class also maintains an accumulator register that * can be used to keep track of the digits that a user is typing up until they hit 'enter' * to push the accumulated digits onto the stack. * * Persists itself to shared preferences. * * Is a singleton. */ @ContextSingleton public class RpnStack extends Stack<BigDecimal> { @Inject SharedPreferences prefs; String digitAccumulator = ""; public String getDigitAccumulator() { return digitAccumulator; } public void setDigitAccumulator(String digitAccumulator) { this.digitAccumulator = digitAccumulator; } public void appendToDigitAccumulator(CharSequence text) { digitAccumulator += text; } public void pushDigitAccumulatorOnStack() { if (digitAccumulator.length() > 0) { push(new BigDecimal(digitAccumulator)); digitAccumulator = ""; } } public void restoreOnResume(@Observes OnResumeEvent e) { for (int i = 0; prefs.contains(String.valueOf(i)); ++i) insertElementAt(new BigDecimal(prefs.getString(String.valueOf(i), null)), i); } public void persistOnPause(@Observes OnPauseEvent e) { final SharedPreferences.Editor edit = prefs.edit(); edit.clear(); for (int i = 0; i < this.size(); ++i) edit.putString(String.valueOf(i), this.get(i).toString()); edit.commit(); } }