FileDocCategorySizeDatePackage
PrimeFactory.javaAPI DocExample4379Sat Sep 12 03:01:00 BST 1998borland.samples.jbcl.listview

PrimeFactory

public class PrimeFactory extends Thread implements borland.jbcl.model.WritableVectorModel

Fields Summary
long
testNumber
long
checkNumber
private long[]
increment
long[]
primes
int
count
long
nextSquare
int
squareSubscript
private borland.jbcl.util.EventMulticaster
modelListeners
private boolean
events
Constructors Summary
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;
  
Methods Summary
public voidaddItem(int parm1, java.lang.Object parm2)

 
public voidaddItem(java.lang.Object parm1)

 
public voidaddModelListener(borland.jbcl.model.VectorModelListener listener)

    modelListeners.add(listener);
  
public booleancanSet(int parm1, boolean parm2)

    return false;
  
public voidenableModelEvents(boolean enable)

    if (events != enable) {
      events = enable;
      if (enable)
        processModelEvent(new VectorModelEvent(this, VectorModelEvent.STRUCTURE_CHANGED));
    }
  
public intfind(java.lang.Object parm1)

    long value =  ((Long) parm1).longValue();
    for (int i = 1; i < count; i++) {
      if (primes[i] >=  value)
        return i;
    }
    return count-1;
  
public java.lang.Objectget(int parm1)

    return new Long(primes[parm1]);
  
public longgetCheckNumber()

    return checkNumber;
  
public intgetCount()

    return count;
  
public booleanisVariableSize()

    return true;
  
protected voidprocessModelEvent(borland.jbcl.model.VectorModelEvent e)

    if (events && modelListeners.hasListeners()) {
      modelListeners.dispatch(e);
      yield();
    }
  
public voidremove(int parm1)

 
public voidremoveAll()

 
public voidremoveModelListener(borland.jbcl.model.VectorModelListener listener)

    modelListeners.remove(listener);
  
public voidrun()

    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;
    }
  
public voidset(int parm1, java.lang.Object parm2)

 
booleantest(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 voidtouched(int parm1)