Commit 03f18a19 authored by mohamad's avatar mohamad

fixing

parent a7f786a9
......@@ -13,8 +13,10 @@
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
<option value="Interface" />
<option value="AnnotationType" />
<option value="Record" />
<option value="Class" />
</list>
</option>
</component>
......@@ -47,7 +49,7 @@
<recent name="FibonacciSeries.implementation" />
</key>
</component>
<component name="RunManager" selected="Application.fibonacciSeries.runners.Main">
<component name="RunManager" selected="JUnit.Tests.testPrimeFinder">
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="primeNumbers.runners.Main" />
<module name="Lab1" />
......@@ -74,8 +76,25 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Tests.testPrimeFinder" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab1" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="primeNumbersTests.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="primeNumbersTests" />
<option name="MAIN_CLASS_NAME" value="primeNumbersTests.Tests" />
<option name="METHOD_NAME" value="testPrimeFinder" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.Tests.testPrimeFinder" />
<item itemvalue="Application.fibonacciSeries.runners.Main" />
<item itemvalue="Application.Main" />
</list>
......
......@@ -6,4 +6,5 @@ import java.util.List;
public interface IFibonacciRangeCalculator {
List<FibonacciStructure> calculate(long start, long end);
List<FibonacciStructure> getResult();
}
package fibonacciSeries.abstractions;
import fibonacciSeries.utilities.FibonacciStructureEvaluator;
public interface IFibonacciRangeCalculatorMultiThreaded {
FibonacciStructureEvaluator calculate(long start, long end,int numberOfThreads);
}
//package fibonacciSeries.implementation.range;
//
//import fibonacciSeries.abstractions.IFibonacciRangeCalculatorMultiThreaded;
//import fibonacciSeries.utilities.FibonacciStructure;
//import fibonacciSeries.utilities.FibonacciStructureEvaluator;
//import fibonacciSeries.utilities.RunnableFibonacciRangeCalculator;
//
//import java.util.ArrayList;
//import java.util.List;
//
//public class FibonacciRangeCalculatorMultiThreaded implements IFibonacciRangeCalculatorMultiThreaded {
//
// @Override
// public FibonacciStructureEvaluator calculate(long start, long end, int numberOfThreads) {
// List<Thread> threads = new ArrayList<>();
// List<RunnableFibonacciRangeCalculator> runnables = new ArrayList<>();
// for(int i = 0; i < numberOfThreads; i++) {
// FibonacciStructure first = new FibonacciStructure(1l,0l);
// FibonacciStructure second = new FibonacciStructure(0l,1l);
// RunnableFibonacciRangeCalculator runnable =
// new RunnableFibonacciRangeCalculator(
// new FibonacciRangeCalculatorUsingStructure(),,);
// Thread thread = new Thread();
// }
//
// return null;
// }
//}
......@@ -11,6 +11,12 @@ import java.util.List;
public class FibonacciRangeCalculatorUsingStructure implements IFibonacciRangeCalculator {
private final FibonacciStructure _fZeroOfTheRange;
private final FibonacciStructure _fOneOfTheRange;
private List<FibonacciStructure> _result;
@Override
public List<FibonacciStructure> getResult() {
return _result;
}
public FibonacciRangeCalculatorUsingStructure(FibonacciStructure _fZeroOfTheRange, FibonacciStructure _fOneOfTheRange) {
this._fZeroOfTheRange = _fZeroOfTheRange;
......@@ -25,12 +31,13 @@ public class FibonacciRangeCalculatorUsingStructure implements IFibonacciRangeCa
FibonacciStructure first = _fZeroOfTheRange;
FibonacciStructure second = _fOneOfTheRange;
for(long i = start; i < end; i++) {
for (long i = start; i < end; i++) {
FibonacciStructure pushed = service.add(first, second);
result.add(pushed);
first = second;
second = pushed;
}
_result = result;
return result;
}
}
package fibonacciSeries.runners;
import fibonacciSeries.abstractions.IFibonacciCalculator;
import fibonacciSeries.abstractions.IFibonacciRangeCalculator;
import fibonacciSeries.implementation.item.MathFibonacciCalculator;
import fibonacciSeries.implementation.range.FibonacciRangeCalculatorUsingStructure;
import fibonacciSeries.utilities.FibonacciStructure;
import java.util.List;
public class Main {
public static void main(String[] args) {
IFibonacciCalculator calculator = new MathFibonacciCalculator();
for (int i = 0; i < 10; i++) {
System.out.println("F("+i+") = "+calculator.calculate(i));
// IFibonacciCalculator calculator = new MathFibonacciCalculator();
// for (int i = 0; i < 10; i++) {
// System.out.println("F("+i+") = "+calculator.calculate(i));
// }
FibonacciStructure first = new FibonacciStructure(1l,0l);
FibonacciStructure second = new FibonacciStructure(0l,1l);
IFibonacciRangeCalculator rangeCalculator =
new FibonacciRangeCalculatorUsingStructure(first,second);
List<FibonacciStructure> result = rangeCalculator.calculate(2,40);
for(FibonacciStructure structure : result){
System.out.println(structure.getFibonacciValue(0,1));
}
}
}
package fibonacciSeries.utilities;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BenchMarked {
}
package fibonacciSeries.utilities;
import java.util.List;
public class FibonacciStructureEvaluator {
}
package fibonacciSeries.utilities;
public interface IFibonacciStructureService {
FibonacciStructure add(FibonacciStructure first,FibonacciStructure second);
}
package fibonacciSeries.utilities;
import fibonacciSeries.abstractions.IFibonacciRangeCalculator;
public class RunnableFibonacciRangeCalculator implements Runnable{
private IFibonacciRangeCalculator _rangeCalculator;
private Long _start;
private Long _end;
public IFibonacciRangeCalculator getRangeCalculator() {
return _rangeCalculator;
}
public Long getStart() {
return _start;
}
public Long getEnd() {
return _end;
}
public RunnableFibonacciRangeCalculator(IFibonacciRangeCalculator rangeCalculator, Long start, Long end) {
_end = end;
_start = start;
_rangeCalculator = rangeCalculator;
}
@Override
public void run() {
_rangeCalculator.calculate(_start, _end);
}
}
......@@ -7,7 +7,6 @@ import java.util.Collection;
public class ParallelPrimeNumbersExtractor implements IPrimeNumbersExtractor {
private final int _numberOfThreads;
// https://www.primesfordays.com/api/primes/nth/555/ for testing purposes
public ParallelPrimeNumbersExtractor(int numberOfThreads) {
_numberOfThreads = numberOfThreads;
......@@ -32,21 +31,29 @@ public class ParallelPrimeNumbersExtractor implements IPrimeNumbersExtractor {
RunnablePrimeNumbersExtractor extractor = new RunnablePrimeNumbersExtractor(base,last);
Thread thread = new Thread(extractor);
base = base+range+1;
// System.out.println("Thread started");
thread.start();
extractors.add(extractor);
threads.add(thread);
}
for (Thread thread : threads) {
try {
// System.out.println("Joining thread " + thread.getName());
thread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("Threads Joined");
long startTime = System.currentTimeMillis();
for (RunnablePrimeNumbersExtractor extractor : extractors) {
result.addAll(extractor.get_primeNumbers());
}
long endTime = System.currentTimeMillis();
long time = (endTime - startTime)/1000;
System.out.println("time to join the results : "+ time + " seconds");
return result;
}
}
......@@ -21,6 +21,7 @@ class RunnablePrimeNumbersExtractor implements Runnable {
@Override
public void run() {
for (int i = _start; i < _end; i++) {
// System.out.println("from " + _start + " to " +_end);
boolean isPrime = true;
for (int j = 2; j < Math.sqrt(i); j++) {
if (i % j == 0) {
......@@ -32,5 +33,6 @@ class RunnablePrimeNumbersExtractor implements Runnable {
_primeNumbers.add(i);
}
}
// System.out.println("Thread finished " + this._start + ", "+ this._end);
}
}
package primeNumbersTests;
import junit.framework.TestCase;
import primeNumbers.abstractions.IPrimeNumbersExtractor;
import primeNumbers.implementation.ParallelPrimeNumbersExtractor;
import utilites.BenchmarkTester;
import java.util.Collection;
public class Tests extends TestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
}
public void testPrimeFinder() throws Exception {
BenchmarkTester.run(()->{
System.out.println("Test started .... ");
IPrimeNumbersExtractor extractor = new ParallelPrimeNumbersExtractor(4);
Integer last = 1000000000;
Collection<Integer> collection = extractor.extract(0,last);
System.out.println("the number of primes between 0 and "+ last+" is " + collection.size());
return 0;
});
}
}
package utilites;
import java.util.concurrent.Callable;
public class BenchmarkTester {
public static void run(Callable func) throws Exception {
long start = System.currentTimeMillis();
func.call();
long end = System.currentTimeMillis();
double time = (double)(end - start)/(double)1000;
System.out.println("the run takes "+time+" seconds");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment