KickJava   Java API By Example, From Geeks To Geeks.

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


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 *
39 * One prime number. This object is owned by an ActivePrimeContainer.
40 */

41 public class PrimeNumberImpl implements PrimeNumber, java.io.Serializable JavaDoc {
42     
43     static Logger logger = Logger.getLogger(PrimeNumberImpl.class.getName());
44   private long value;
45   private PrimeNumber next;
46   private ActivePrimeContainer container;
47   
48   /** This value serves only for debugging: tryModulo may not be called
49    * with a number inferior to a prior call. */

50   private long nextValue;
51
52   /**
53    * Constructor for PrimeNumberImpl.
54    */

55   public PrimeNumberImpl(ActivePrimeContainer container, long value) {
56     this.container = container;
57     this.value = value;
58   }
59
60   /**
61    * Tries to divide value by n. Returns if this succeeds, calls
62    * tryModulo on the next prime number if not. If there is no next
63    * prime number, a new prime has been found and is created.
64    * For debugging, a check is made on the order of the requests.
65    * @see org.objectweb.proactive.examples.eratosthenes.PrimeNumber#tryModulo(long)
66    */

67   public void tryModulo(long n) {
68     if (n % value != 0) {
69       if (next == null) {
70         nextValue = n;
71         next = container.newPrimeNumber(n);
72       } else {
73         if (n <= nextValue) {
74             logger.fatal("Requests arrived out of order. Should never occur when using FIFO serving.");
75             System.exit(2);
76         }
77         next.tryModulo(n);
78       }
79     }
80   }
81
82   /**
83    * @see org.objectweb.proactive.examples.eratosthenes.PrimeNumber#getValue()
84    */

85   public long getValue() {
86     return value;
87   }
88
89 }
90
Popular Tags