home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-05-08 | 4.3 KB | 145 lines |
- /*
- * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
- *
- * This SOURCE CODE FILE, which has been provided by Borland as part
- * of a Borland product for use ONLY by licensed users of the product,
- * includes CONFIDENTIAL and PROPRIETARY information of Borland.
- *
- * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
- * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
- * THE PRODUCT.
- *
- * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
- * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
- * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
- * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
- * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
- * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
- * CODE FILE.
- */
- package borland.samples.jbcl.listview;
-
- import borland.jbcl.model.VectorModelListener;
- import borland.jbcl.model.WritableVectorModel;
- import borland.jbcl.model.VectorModelEvent;
- import borland.jbcl.util.EventMulticaster;
-
- public class PrimeFactory extends Thread implements WritableVectorModel{
- long testNumber = 30;
- long checkNumber = 31;
- private long[] increment = {1,7,11,13,17,19,23,29};
- long[] primes = new long[500000] ;
- int count = 0;
- long nextSquare = 49;
- int squareSubscript = 3;
- private EventMulticaster modelListeners = new EventMulticaster();
- private boolean events = true;
-
- public PrimeFactory() {
- //the only numbers we need to check mod 30
- primes[count++] = 2;
- primes[count++] = 3;
- primes[count++] = 5;
- primes[count++] = 7;
- primes[count++] = 11;
- primes[count++] = 13;
- primes[count++] = 17;
- primes[count++] = 19;
- primes[count++] = 23;
- primes[count++] = 29;
- }
-
- public void run() {
- while (count < primes.length - 8){
- for (int i = 0; i < 8;i++) {
- if (test(testNumber + increment[i])) {
- processModelEvent(new VectorModelEvent(this,VectorModelEvent.ITEM_ADDED,count-1));
- //yield(); the ListView structureChanged ModelEvent listener method does this so we don't have to
- }
- }
- testNumber += 30;
- }
- }
-
- boolean test(long check) {
- //if square root of the limiting prime, bump the limiting prime
- checkNumber = check;
- if (check == nextSquare) {
- squareSubscript++;
- nextSquare = primes[squareSubscript] * primes[squareSubscript];
- //System.err.println("nextSquare = " + nextSquare);
- return false;
- }
- int i = 3;
-
- while (i < squareSubscript) {
- int start = i;
- int end = squareSubscript < (start + 100) ? squareSubscript : start + 100;
- for (i = start;i <= end;i++) {
- if ((check/primes[i])*primes[i] == check)
- return false;
- }
- }
- primes[count++] = check;
- return true;
- }
-
- public long getCheckNumber() {
- return checkNumber;
- }
-
- public boolean canSet(int parm1, boolean parm2) {
- return false;
- }
-
- public void set(int parm1, Object parm2) { }
- public void touched(int parm1) { }
- public boolean isVariableSize() {
- return true;
- }
- public void addItem(Object parm1) { }
- public void addItem(int parm1, Object parm2) { }
- public void remove(int parm1) { }
- public void removeAll() { }
-
- public void enableModelEvents(boolean enable) {
- if (events != enable) {
- events = enable;
- if (enable)
- processModelEvent(new VectorModelEvent(this, VectorModelEvent.STRUCTURE_CHANGED));
- }
- }
-
- public Object get(int parm1) {
- return new Long(primes[parm1]);
- }
-
- public int getCount() {
- return count;
- }
-
- public int find(Object parm1) {
- long value = ((Long) parm1).longValue();
- for (int i = 1; i < count; i++) {
- if (primes[i] >= value)
- return i;
- }
- return count-1;
- }
-
- public void addModelListener(VectorModelListener listener) {
- modelListeners.add(listener);
- }
-
- public void removeModelListener(VectorModelListener listener) {
- modelListeners.remove(listener);
- }
-
- protected void processModelEvent(VectorModelEvent e) {
- if (events && modelListeners.hasListeners()) {
- modelListeners.dispatch(e);
- yield();
- }
- }
- }
-