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.stock;
033:
034: import java.util.*;
035:
036: import javax.microedition.rms.*;
037:
038: /**
039: * <p>This class provides an implementation of the <code>Database</code>
040: * class specific to alert records.</p>
041: */
042: public class AlertDatabase extends Database {
043: /**
044: * Default Constructor
045: */
046: public AlertDatabase() {
047: rc = new AlertComparator();
048: }
049:
050: /**
051: * <p>This methods cleans out the database of all alerts that match the
052: * <code>tkrSymbol</code> passed in. An appropriate use would be when
053: * removing a stock from the database, all alerts for that stock are no
054: * longer valid, so call this method then.</p>
055: *
056: * @param tkrSymbol The name of the stock to match with alerts
057: */
058: public synchronized void removeUselessAlerts(String tkrSymbol) {
059: Enumeration IDs = recordIDs.elements();
060:
061: while (IDs.hasMoreElements()) {
062: int index = ((Integer) IDs.nextElement()).intValue();
063:
064: try {
065: String data = new String(database.getRecord(index));
066: data = data.substring(0, data.indexOf(';'));
067:
068: if (data.equals(tkrSymbol)) {
069: database.deleteRecord(index);
070: recordIDs.removeElement(new Integer(index));
071: }
072: } catch (RecordStoreException rse) {
073: return;
074: }
075: }
076: }
077:
078: /**
079: * <p>Get a <code>RecordEnumeration</code> of records in the database who
080: * match the <code>AlertFilter</code> conditions</p>
081: *
082: * @return <code>RecordEnumeration</code> of all stock records that match
083: * the <code>RecordFilter</code>
084: * @param tkrSymbol The name of the stock to retrieve alerts for
085: * @param price The price of the stock to retrieve alerts for
086: * @throws <code>RecordStoreNotOpenException</code> is thrown when
087: * trying to close a <code>RecordStore</code> that is not open
088: */
089: public synchronized RecordEnumeration enumerateRecords(
090: String tkrSymbol, int price)
091: throws RecordStoreNotOpenException {
092: return database.enumerateRecords(new AlertFilter(tkrSymbol,
093: price), null, false);
094: }
095:
096: /**
097: * <p>Filters the records based on stock symbol and price. If price is
098: * passed as 0 then all records (excluding the first one that stores the
099: * lastID) are selected</p>
100: *
101: * @see javax.microedition.rms.RecordFilter
102: */
103: private class AlertFilter implements RecordFilter {
104: /**
105: * <p>The stock symbol to filter with</p>
106: */
107: private String symbol = null;
108:
109: /**
110: * <p>The price to filter with</p>
111: */
112: private int price = 0;
113:
114: /**
115: * <p>Constructor for the <code>AlertFilter</code></p>
116: *
117: * @param tkrSymbol The name of the stock to filter for
118: * @param matchPrice The price to match with the alert
119: */
120: public AlertFilter(String tkrSymbol, int matchPrice) {
121: symbol = tkrSymbol;
122: price = matchPrice;
123: }
124:
125: /**
126: * <p>Returns true if the candidate matches the symbol and price<p>
127: *
128: * @returns true if the candidate matches the criteria
129: * @param candidate The data to check against the criteria
130: */
131: public boolean matches(byte[] candidate) {
132: if (candidate.length > 4) {
133: if (price == 0) {
134: return true;
135: }
136:
137: String c = new String(candidate);
138:
139: if (symbol.equals(c.substring(0, c.indexOf(';')))) {
140: if (price >= Integer
141: .valueOf(
142: c.substring(c.indexOf(';') + 1, c
143: .length())).intValue()) {
144: return true;
145: }
146: }
147: }
148:
149: return false;
150: }
151: }
152:
153: /**
154: * <p>Class to compare two records and see if they are equal</p>
155: *
156: * @see javax.microedition.rms.RecordComparator
157: */
158: private class AlertComparator implements RecordComparator {
159: /**
160: * <p>Checks to see if rec1 matches rec2</p>
161: *
162: * @returns <code>RecordComparator.EQUIVALENT</code> if the records
163: * match or <code>Integer.MAX_VALUE</code> if they don't
164: * @param rec1 the data to compare against
165: * @param rec2 the data to compare with
166: */
167: public int compare(byte[] rec1, byte[] rec2) {
168: System.out.println(new String(rec1) + " ?==? "
169: + new String(rec2));
170:
171: String record1 = new String(rec1);
172: String record2 = new String(rec2);
173:
174: if (record1.equals(record2)) {
175: return RecordComparator.EQUIVALENT;
176: }
177:
178: return Integer.MAX_VALUE;
179: }
180: }
181: }
|