KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > eratosthenes > ConsolePrimeOutputListener


1 package org.objectweb.proactive.examples.eratosthenes;
2
3 import org.apache.log4j.Logger;
4
5 /*
6 * ################################################################
7 *
8 * ProActive: The Java(TM) library for Parallel, Distributed,
9 * Concurrent computing with Security and Mobility
10 *
11 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
12 * Contact: proactive-support@inria.fr
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 * USA
28 *
29 * Initial developer(s): The ProActive Team
30 * http://www.inria.fr/oasis/ProActive/contacts.html
31 * Contributor(s):
32 *
33 * ################################################################
34 */

35
36 /**
37 * @author Jonathan Streit
38 * Serves to print newly found prime numbers to the console.
39 */

40 public class ConsolePrimeOutputListener implements PrimeOutputListener, java.io.Serializable JavaDoc {
41     
42     static Logger logger = Logger.getLogger(ConsolePrimeOutputListener.class.getName());
43   private long startTime;
44   private int numberCounter;
45
46   /**
47    * Constructor for ConsolePrimeOutputListener.
48    */

49   public ConsolePrimeOutputListener() {
50     super();
51   }
52
53   public void newPrimeNumberFound(long n) {
54     numberCounter ++;
55     if (startTime == 0) startTime = System.currentTimeMillis();
56     String JavaDoc time = Long.toString((System.currentTimeMillis() - startTime) / 1000);
57     String JavaDoc counter = Integer.toString(numberCounter);
58     StringBuffer JavaDoc line = new StringBuffer JavaDoc(50);
59     line.append(" ");
60     line.append("Prime number ");
61     for (int i = counter.length(); i < 6; i ++) line.append(' ');
62     line.append('#');
63     line.append(counter);
64     line.append(" found with value ");
65     line.append(n);
66     line.append("\t (");
67     for (int i = time.length(); i < 6; i ++) line.append('0');
68     line.append(time);
69     line.append("s)\n");
70     logger.info(line);
71   }
72
73 }
74
Popular Tags