Thursday Apr 30, 2009

Mutability, class creation, singletons, and performance

An illustration of how much execution time can be saved when using immutable singleton classes.[Read More]

Monday Apr 27, 2009

A type-safe heterogeneous container for parameters that validates the parameter

A type-safe heterogeneous container for parameters with validation.[Read More]

Monday Nov 17, 2008

Regenerator G1

Some of us might associate G1 with "Regenerator G1" - if you don't know, that was something to do with Godzilla's DNA makeup in one of the great Godzilla movies. It turns out that G1 is "Garbage First", a new Garbage Collector for JDK7.

Technorati Tags:

Friday May 09, 2008

FindBugs

Check out FindBugs for locating bugs software defects in your code. Not that there are any, of course.

Technorati Tags: ,

Thursday May 01, 2008

Servlet 3.0 (JSR 315) update

Servlet 3.0 (JSR 315) update:
Servlet 3.0 (JSR 315) update

Technorati Tags:

Friday Feb 15, 2008

Wal-Mart to Phase out HD DVD

Is the beginning of the end for the HD DVD - Blu-ray high-definition DVD wars? On Feb. 15th, 2008, Wal-Mart announced it would phase out HD DVD discs in favor of Blu-ray discs.

What does Java have to do with this development? It's my understanding that Java is used to implement interactive elements in Blu-ray discs.

Technorati Tags: , , , ,

Friday Oct 19, 2007

Prime Twins in Java

From an algorithm by Shimrat, University of Alberta, Alberta, CA, published in "Collected Algorithms of the ACM, Volume II", this is my interpretation of the "PrimeTwins" in Java:
import java.util.Enumeration;
import java.util.Vector;

/\*\*
 \* Models an event generated by the construction of
 \* prime twins
 \*/
class PrimeTwinsEvent {

    /\*\*
     \* creates a PrimeTwinsEvent object.
     \*
     \* @param serial the serial number (sequence) of twins
     \* @param twin1 the lower twin
     \* @param twin2 the higher twin
     \*/
    public PrimeTwinsEvent(int serial,int twin1,int twin2) {
	this.serial = serial;
	this.twin1 = twin1;
	this.twin2 = twin2;
    }

    // accessors
    public int getSerial() { return this.serial; }
    public int getTwin1() { return this.twin1; }
    public int getTwin2() { return this.twin2; }

    /\*\*
     \* Prints an String interpretation of
     \* this object
     \*/
    public String toString() {
	return "serial: "
	    + serial
	    + " twin1: "
	    + twin1
	    + " twin2: "
	    + twin2;
    }

    private int serial;
    private int twin1;
    private int twin2;

}

/\*\*
 \* Classes wishing to be notifed of PrimeTwinsEvents
 \* must implement this interface
 \*/
interface PrimeTwinsListener {
    public void action(PrimeTwinsEvent event);
}

/\*\*
 \* From an algorithm by M. Shimrat, University of Alberta, Alberta, Canada,
 \* and published in the "Collected Algorithms of the ACM, Volume II", a
 \* PrimeTwins object has a generate method
 \* that generates successive prime numbers separated by two ("prime twins")
 \* and notifies listeners, if any, of the prime twins and a sequential
 \* serial number.
 \*
 \* The Java is original, but the algorithm is from the author named above.
 \*
 \* @author Terry J. Gardner
 \*/
public class PrimeTwins {

    /\*\*
     \* add a listener to be notfied. if l is null,
     \* then no action is taken, and no exception is thrown.
     \*
     \* @param l the listener to be notfied
     \*/
    public void addPrimeTwinsListener(PrimeTwinsListener l) {
	if(l == null) return;
	listeners.add(l);
    }

    /\*\*
     \* notify all configured listeners
     \*
     \* @param event the event
     \*/
    public void notifyAllListeners(PrimeTwinsEvent event) {
	Enumeration e = listeners.elements();
	while(e.hasMoreElements()) {
	    PrimeTwinsListener l = e.nextElement();
	    l.action(event);
	}
    }

    /\*\*
     \* generates successive prime twins (primes differing by two)
     \* and notifies all PrimeTwinsListeners.
     \*
     \* @param MAX maximum number of twins
     \* @throws IllegalArgumentException if max is not positive
     \*/
    public void generate(final int MAX) throws IllegalArgumentException {
	if(MAX <= 0) {
	    throw new IllegalArgumentException("max must be positive");
	}
	int j,m,previous,current,t;
	int[] p = new int[MAX+1];
	p[1] = j = 2;
	p[2] = previous = 3;
	t = 0;
	for(current = 5; current < p[j] \* p[j]; current += 2) {
	    m = 1;
	    int mult = p[m] \* p[m];
	    while(mult <= current) {
		if(current == current/mult) return;
		if(j < MAX) {
		    p[++j] = current;
		}
		if(current == previous + 2) {
		    ++t;
		    notifyAllListeners(new PrimeTwinsEvent(t,previous,current));
		}
		previous = current;
		++m;
		mult = p[m] \* p[m];
	    }
	}
    }

    // listeners
    private Vector listeners = new Vector();
    
}

No-one could claim that Java is perfect, but it is easy to use to realize algorithms.
About

Sun, LDAP, SLAMD, DSLA, java, Struts, networking, chess, books, cooking, wine, and many other things.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today