Source Code Cross Referenced for AlertDatabase.java in  » IDE-Netbeans » mobility » example » stock » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » mobility » example.stock 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.