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 javax.microedition.rms.*;
035:
036: /**
037: * <p>This class provides an implementation for the <code>Database</code>
038: * class specific to stock records.</p>
039: */
040: public class StockDatabase extends Database {
041: /**
042: * Default Constructor
043: */
044: public StockDatabase() {
045: rc = new StockComparator();
046: }
047:
048: /**
049: * <p>Get a <code>RecordEnumeration</code> of records in the database who
050: * match the <code>StockFilter</code> -- which just filters out the first
051: * entry in the database (ie. the lastID record)</p>
052: *
053: * @return <code>RecordEnumeration</code> of all stock records (ie.
054: * excluding the lastID record)
055: * @throws <code>RecordStoreNotOpenException</code> is thrown when trying
056: * to close a <code>RecordStore</code> that is not open
057: */
058: public synchronized RecordEnumeration enumerateRecords()
059: throws RecordStoreNotOpenException {
060: return database
061: .enumerateRecords(new StockFilter(), null, false);
062: }
063:
064: /**
065: * <p>Filters out the lastID record</p>
066: *
067: * @see javax.microedition.rms.RecordFilter
068: */
069: private class StockFilter implements RecordFilter {
070: /**
071: * Returns true if the candidate is less than 5 characters
072: */
073: public boolean matches(byte[] candidate) {
074: return ((candidate.length > 5) ? true : false);
075: }
076: }
077:
078: /**
079: * <p>Class to compare two records and see if they are equal</p>
080: *
081: * @see javax.microedition.rms.RecordComparator
082: */
083: private class StockComparator implements RecordComparator {
084: /**
085: * Checks to see if rec1 matches rec2
086: *
087: * @return RecordComparator.PRECEDES if the name of
088: * rec2 comes before the name of rec1 alphabetically,
089: * RecordComparator.EQUIVALENT
090: * if the records match,
091: * RecordComparator.FOLLOWS if the name of
092: * rec2 follows the name of rec1 alphabetically
093: *
094: * @param rec1 the data to compare against
095: * @param rec2 the data to compare with
096: */
097: public int compare(byte[] rec1, byte[] rec2) {
098: String name1 = Stock.getName(new String(rec1));
099: String name2 = Stock.getName(new String(rec2));
100: int result = name1.compareTo(name2);
101:
102: if (result < 0) {
103: return RecordComparator.PRECEDES;
104: } else if (result == 0) {
105: return RecordComparator.EQUIVALENT;
106: } else {
107: return RecordComparator.FOLLOWS;
108: }
109: }
110: }
111: }
|