Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 64 65 66 67 68 69 < 70 > 71 72 73 74 75 76 .. 166 >> Next

032 private TextBox detailsScreen;
033
034 // screen components
035 ChoiceGroup opsChoice;
036 ChoiceGroup modeChoice;
037 TextField targetTF;
038 int page = 1;
039
040 Vector products;
041 String xmlBuf;
042 int [] detailIndexes;
043 boolean updateIndexes;
044
045 boolean debug = true;
046 Timer ticker = new Timer();
Listing 22.5 (continued)
Here are the optimizations in the class definition:
Guess the Size of Vectors. Resizing Vectors is expensive. This is demonstrated by using the constant in line 11.
Use Local Variables. Local variables are accessed faster than class members. You will notice that we have eliminated all of the public static Strings. This optimization could be used further in this code.
Avoid String Comparisons. Notice in lines 14 to 18 that the Command references are declared as class members, and these will be compared against in the event handler instead of comparing Strings.
Avoid Temporary Objects. In lines 21 to 24 we use class data members for the Alert references and thus reuse the objects after instantiating them lazily.
186 Item 22
048 public MicroAmazonSearch()
049 {
050 final String [] legalOps = { "KeywordSearch", 2
"BrowseNodeSearch", "AsinSearch",
051 "UpcSearch", "AuthorSearch", 2
"ArtistSearch",
052 /* reduce to work in 64k ...
053 "ActorSearch", "DirectorSearch",
"ManufacturerSearch",
054 "ListManiaSearch", "SimilaritySearch" ,
055 */
056 };
057
058 final String [] legalModes = { "baby", "books", 2
"classical", "dvd", "electronics",
059 /* reduce to conserve memory (< 64k).
060 "garden", "kitchen", "magazines", ' 2
"music", "pc-hardware",
061 "photo", "software", "toys", 2
"universal", "vhs",
062 "videogames",
063 */
064 };
065 final String CMD_SEARCH = "Search";
// - removed remaining final Strings for brevity
070
071 // Create GUI components here...
072 display = Display.getDisplay(this);
073
074 // commands
075 searchCommand = new Command(CMD_SEARCH, Command.SCREEN, 1);
076 detailsCommand = new Command(CMD_DETAILS, Command.SCREEN,
1); 077 nextResultsCommand = new Command(CMD_NEXT_TEN,
Command SCREEN, 1);
078 backCommand = new Command(CMD_BACK, Command.SCREEN, 2);
079 exitCommand = new Command(CMD_EXIT, Command.SCREEN, 2);
080
081 // Create 3 screens: 1. Search, 2. Results, 3. Details
082 // search form
083 searchScreen = new Form("Search Terms");
// removed the construction of the searchScreen no change.
094
095 // other screens, lazy instantiated
096 }
Listing 22.5 (continued)
The MicroAmazonSearch constructor has three optimizations:
Use Local Variables. The arrays for operations (lines 50 to 63) and modes were moved to become local variables so the memory is reclaimed at method return.
J2ME Performance and Pitfalls 187
Declare Variables and MethodsFinal. Final references are accessed faster and declaring both final and static is the fastest. Both the arrays and line 65 demonstrate this.
UseLazy Instantiation. Line 95 no longer contains the other screens, as we wait until they are needed by the user to create them.
098 public void startApp()
099 {
100 display.setCurrent(searchScreen);
101 // clean up as User decides what to do
102 System.gc();
103 }
104
// removed pauseApp() and destroyApp as they do nothing
114
115 public void commandAction(Command c, Displayable s)
116 {
117 String targets = null;
118 try
119 {
120 if (c == exitCommand)
121 {
122 destroyApp(false);
123 notifyDestroyed();
124 }
125 else if (c == searchCommand)
126 {
127 // check we have the valid parameters\
128 targets = targetTF.getString();
129 if (targets.length() == 0)
130 {
131: // lazy instantiation!
132 if (searchAlert == null)
133 searchAlert = new Alert("Search Error", 2
"'Search For' text field cannot be empty.", null, AlertType.ERROR);
134 display.setCurrent(searchAlert);
135 }
136 else
137 {
138 page = 1; // reset
139 xmlBuf = null;
140 updatelndexes = true;
141 // memory intensive, so get as much as we can
142 System.gc();
143 doAmazonSearch(page, targets);
144 System.gc();
145 }
146 }
147 else if (c == detailsCommand)
148 {
149 // get item selected in list
Listing 22.5 (continued)
188 Item 22
150 int selected = resultsScreen.getSelectedIndex();
151 if (selected >= 0)
152 {
153 String product = 2
resultsScreen.getString(selected);
154 showDetails(product, xmlBuf);
155 }
156 else
157 {
158 if (detailAlert == null) 2
159 detailAlert = new Alert("Error", "Must
select a product to see details.", null, AlertType.ERROR);
160 display.setCurrent(detailAlert);
161 }
162 }
// - removed handling of backCommand and nextResultsCommand for brevity
182 } catch (Throwable t)
183 {
184 if (debug) t.printStackTrace();
185 if (genericAlert == null) 2
186 genericAlert = new Alert("Error", "ERROR: reason:
" + t.toString(),null, AlertType.ERROR);
187 else
Previous << 1 .. 64 65 66 67 68 69 < 70 > 71 72 73 74 75 76 .. 166 >> Next