001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example.mia.demo;
033:
034: import java.util.Random;
035:
036: import javax.microedition.global.*;
037: import javax.microedition.lcdui.*;
038: import javax.microedition.midlet.MIDlet;
039:
040: /**
041: * StrComparatorDemo MIDlet demonstrates locale dependent
042: * sorting of strings.
043: * Slovak cities are sorted using default comparator or comparator
044: * initialized with slovak locale.
045: *
046: * @version
047: */
048: public final class StrComparatorDemo extends MIDlet implements
049: CommandListener {
050: private static final Random random = new Random();
051: private final Command exitCommand;
052: private final Command shuffleCommand;
053: private final Command sortDefCommand;
054: private final Command sortSkyCommand;
055: private final Form mainForm;
056: private StringComparator defComparator;
057: private StringComparator skyComparator;
058: private String[] cities = new String[] { "Horovce",
059: "Chorv\u00e1tsky Grob", "Z\u00e1kamenn\u00e9",
060: "\u0160trba", "Zalaba", "Str\u00e1\u017eske",
061: "Chorv\u00e1ty", "Hoste", "Stre\u010dno", "\u017dakovce",
062: "Z\u00e1kop\u010die", "\u017dakarovce" };
063:
064: public StrComparatorDemo() {
065: mainForm = new Form("String Comparator");
066:
067: updateStrings();
068:
069: try {
070: defComparator = new StringComparator(null,
071: StringComparator.IDENTICAL);
072: skyComparator = new StringComparator("sk",
073: StringComparator.IDENTICAL);
074: } catch (UnsupportedLocaleException e) {
075: }
076:
077: exitCommand = new Command("Exit", Command.EXIT, 1);
078: shuffleCommand = new Command("Shuffle", Command.SCREEN, 1);
079: sortDefCommand = new Command("Sort - default", Command.SCREEN,
080: 2);
081: sortSkyCommand = new Command("Sort - slovak", Command.SCREEN, 2);
082:
083: mainForm.addCommand(exitCommand);
084: mainForm.addCommand(shuffleCommand);
085: mainForm.addCommand(sortDefCommand);
086: mainForm.addCommand(sortSkyCommand);
087:
088: mainForm.setCommandListener(this );
089: }
090:
091: private void updateStrings() {
092: mainForm.deleteAll();
093:
094: for (int i = 0; i < cities.length; ++i) {
095: mainForm.append(cities[i] + "\n");
096: }
097: }
098:
099: private static void sort(String[] array, StringComparator comparator) {
100: boolean changed;
101: changed = true;
102:
103: for (int j = array.length - 1; (j > 0) && changed; --j) {
104: changed = false;
105:
106: for (int i = 0; i < j; ++i) {
107: if (comparator.compare(array[i], array[i + 1]) > 0) {
108: String tmp = array[i + 1];
109: array[i + 1] = array[i];
110: array[i] = tmp;
111: changed = true;
112: }
113: }
114: }
115: }
116:
117: private static void shuffle(String[] array2, String[] array,
118: int step) {
119: int k = 0;
120:
121: for (int i = step - 1; i >= 0; --i) {
122: for (int j = i; j < array.length; j += step) {
123: array2[k++] = array[j];
124: }
125: }
126: }
127:
128: private static void shuffle(String[] array) {
129: String[] array2 = new String[array.length];
130:
131: for (int i = 0; i < 10; ++i) {
132: shuffle(array2, array, random.nextInt(array.length) + 1);
133:
134: String[] tmpArray = array;
135: array = array2;
136: array2 = tmpArray;
137: }
138: }
139:
140: protected void startApp() {
141: Display.getDisplay(this ).setCurrent(mainForm);
142: }
143:
144: protected void pauseApp() {
145: }
146:
147: protected void destroyApp(boolean unconditional) {
148: }
149:
150: public void commandAction(Command c, Displayable d) {
151: if (c == shuffleCommand) {
152: shuffle(cities);
153: updateStrings();
154: } else if (c == sortDefCommand) {
155: sort(cities, defComparator);
156: updateStrings();
157: } else if (c == sortSkyCommand) {
158: sort(cities, skyComparator);
159: updateStrings();
160: } else if (c == exitCommand) {
161: destroyApp(false);
162: notifyDestroyed();
163: }
164: }
165: }
|