-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
North Zhang -- Homework#4 #27
Open
northzzn
wants to merge
3
commits into
bug-hunterx:master
Choose a base branch
from
northzzn:homework#4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.epam.dataservice; | ||
|
||
public class NumberChecker { | ||
private PoliceForceService policeForceService; | ||
|
||
public NumberChecker(PoliceForceService policeForceService) { | ||
this.policeForceService = policeForceService; | ||
} | ||
|
||
public boolean isSpecialNumber(String policeForceName) { | ||
String contactNumber = policeForceService.getContactNo(policeForceName); | ||
return "ABCDEFGH".equals(contactNumber); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/epam/dataservice/accident/AccidentProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.epam.dataservice.accident; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
|
||
import com.epam.data.RoadAccident; | ||
import com.epam.dataservice.PoliceForceService; | ||
|
||
public class AccidentProcessor implements Runnable { | ||
|
||
public static String TIME_MORNING = "MORNING"; | ||
public static String TIME_AFTERNOON = "AFTERNOON"; | ||
public static String TIME_EVENING = "EVENING"; | ||
public static String TIME_NIGHT = "NIGHT"; | ||
|
||
private BlockingQueue<RoadAccident> readerQueue; | ||
private BlockingQueue<RoadAccident> daytimeQueue; | ||
private BlockingQueue<RoadAccident> nighttimeQueue; | ||
private PoliceForceService policeForceService; | ||
|
||
public AccidentProcessor(BlockingQueue<RoadAccident> readerQueue, BlockingQueue<RoadAccident> daytimeQueue, | ||
BlockingQueue<RoadAccident> nighttimeQueue) { | ||
this.readerQueue = readerQueue; | ||
this.daytimeQueue = daytimeQueue; | ||
this.nighttimeQueue = nighttimeQueue; | ||
this.policeForceService = new PoliceForceService(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
System.out.println("processor running..."); | ||
while (!Thread.currentThread().isInterrupted()) { | ||
RoadAccident roadAccident = this.readerQueue.take(); | ||
roadAccident.setForceContact(this.policeForceService.getContactNo(roadAccident.getPoliceForce())); | ||
|
||
if (roadAccident.getTime().getHour() >= 18) { | ||
roadAccident.setTimeosDay(TIME_NIGHT); | ||
nighttimeQueue.put(roadAccident); | ||
} else if (roadAccident.getTime().getHour() >= 12) { | ||
roadAccident.setTimeosDay(TIME_AFTERNOON); | ||
daytimeQueue.put(roadAccident); | ||
} else if (roadAccident.getTime().getHour() >= 6) { | ||
roadAccident.setTimeosDay(TIME_MORNING); | ||
daytimeQueue.put(roadAccident); | ||
} else { | ||
roadAccident.setTimeosDay(TIME_EVENING); | ||
nighttimeQueue.put(roadAccident); | ||
} | ||
} | ||
} catch (InterruptedException e) { | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/epam/dataservice/accident/AccidentReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.epam.dataservice.accident; | ||
|
||
import java.io.FileReader; | ||
import java.util.Iterator; | ||
import java.util.concurrent.BlockingQueue; | ||
|
||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVParser; | ||
import org.apache.commons.csv.CSVRecord; | ||
|
||
import com.epam.data.RoadAccident; | ||
import com.epam.dataservice.RoadAccidentParser; | ||
|
||
public class AccidentReader implements Runnable { | ||
|
||
private BlockingQueue<RoadAccident> readerQueue; | ||
private String accidentFile; | ||
private RoadAccidentParser roadAccidentParser; | ||
|
||
public AccidentReader(BlockingQueue<RoadAccident> readerQueue, String accidentFile) { | ||
this.readerQueue = readerQueue; | ||
this.accidentFile = accidentFile; | ||
roadAccidentParser = new RoadAccidentParser(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
System.out.println("start reading file " + this.accidentFile); | ||
CSVParser accidentFileParser = new CSVParser(new FileReader(accidentFile), CSVFormat.EXCEL.withHeader()); | ||
Iterator<CSVRecord> accidentFileIterator = accidentFileParser.iterator(); | ||
while (accidentFileIterator.hasNext()) { | ||
RoadAccident accident = roadAccidentParser.parseRecord(accidentFileIterator.next()); | ||
if (accident != null) | ||
readerQueue.put(accident); | ||
} | ||
accidentFileParser.close(); | ||
System.out.println("end reading file " + this.accidentFile); | ||
} catch (Exception e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/epam/dataservice/accident/AccidentWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.epam.dataservice.accident; | ||
|
||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.concurrent.BlockingQueue; | ||
|
||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVPrinter; | ||
|
||
import com.epam.data.RoadAccident; | ||
|
||
public class AccidentWriter implements Runnable { | ||
|
||
private BlockingQueue<RoadAccident> accidentQueue; | ||
private String accidentFile; | ||
|
||
public AccidentWriter(BlockingQueue<RoadAccident> accidentQueue, String accidentFile) { | ||
this.accidentQueue = accidentQueue; | ||
this.accidentFile = accidentFile; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
CSVPrinter csvPrinter = null; | ||
try { | ||
FileWriter accidentFileWriter = new FileWriter(this.accidentFile); | ||
csvPrinter = new CSVPrinter(accidentFileWriter, CSVFormat.DEFAULT.withRecordSeparator("\n")); | ||
csvPrinter.printRecord("Accident_Index", "Longitude", "Latitude", "Accident_Hour"); | ||
while (true) { | ||
RoadAccident accident = accidentQueue.take(); | ||
csvPrinter.printRecord(accident.getAccidentId(), accident.getLongitude(), accident.getLatitude(), accident.getTime().getHour()); | ||
} | ||
} catch (InterruptedException e) { | ||
//will interrupt when queue is empty and no more data come in | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
csvPrinter.flush(); | ||
csvPrinter.close(); | ||
} catch (IOException e1) { | ||
e1.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/test/java/com/epam/dataservice/AccidentReaderProcessorWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.epam.dataservice; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ArrayBlockingQueue; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.epam.data.RoadAccident; | ||
import com.epam.dataservice.accident.AccidentProcessor; | ||
import com.epam.dataservice.accident.AccidentReader; | ||
import com.epam.dataservice.accident.AccidentWriter; | ||
|
||
public class AccidentReaderProcessorWriterTest { | ||
|
||
private static int readerThreadCount = 2; | ||
private static int readerQueueCapacity = 10000; | ||
private static int processorThreadCount = 5; | ||
private static int daytimeQueueCapacity = 10000; | ||
private static int nighttimeQueueCapacity = 10000; | ||
private static List<String> accidentFileList = new ArrayList<String>(); | ||
static { | ||
accidentFileList.add("src/main/resources/DfTRoadSafety_Accidents_2009.csv"); | ||
accidentFileList.add("src/main/resources/DfTRoadSafety_Accidents_2010.csv"); | ||
accidentFileList.add("src/main/resources/DfTRoadSafety_Accidents_2011.csv"); | ||
accidentFileList.add("src/main/resources/DfTRoadSafety_Accidents_2012.csv"); | ||
accidentFileList.add("src/main/resources/DfTRoadSafety_Accidents_2013.csv"); | ||
} | ||
|
||
private static ExecutorService file2Queue(BlockingQueue<RoadAccident> readerQueue) { | ||
ExecutorService readerExecutor = Executors.newFixedThreadPool(readerThreadCount); | ||
for (String accidentFile : accidentFileList) { | ||
readerExecutor.execute(new AccidentReader(readerQueue, accidentFile)); | ||
} | ||
return readerExecutor; | ||
} | ||
|
||
private static List<Thread> processData(BlockingQueue<RoadAccident> readerQueue, | ||
BlockingQueue<RoadAccident> daytimeQueue, BlockingQueue<RoadAccident> nighttimeQueue) { | ||
List<Thread> processorThreadList = new ArrayList<Thread>(); | ||
for (int i = 0; i < processorThreadCount; i++) { | ||
Thread processorThread = new Thread(new AccidentProcessor(readerQueue, daytimeQueue, nighttimeQueue)); | ||
processorThread.start(); | ||
processorThreadList.add(processorThread); | ||
} | ||
return processorThreadList; | ||
} | ||
|
||
private static Thread write2File(BlockingQueue<RoadAccident> accidentQueue, String fileName) { | ||
Thread writerThread = new Thread(new AccidentWriter(accidentQueue, fileName)); | ||
writerThread.start(); | ||
return writerThread; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
BlockingQueue<RoadAccident> readerQueue = new ArrayBlockingQueue<RoadAccident>(readerQueueCapacity); | ||
BlockingQueue<RoadAccident> daytimeQueue = new ArrayBlockingQueue<RoadAccident>(daytimeQueueCapacity); | ||
BlockingQueue<RoadAccident> nighttimeQueue = new ArrayBlockingQueue<RoadAccident>(nighttimeQueueCapacity); | ||
|
||
ExecutorService readerExecutor = file2Queue(readerQueue); | ||
List<Thread> processorThreadList = processData(readerQueue, daytimeQueue, nighttimeQueue); | ||
Thread daytimeWriterThread = write2File(daytimeQueue, "src/main/resources/DaytimeAccidents.csv"); | ||
Thread nighttimeWriterThread = write2File(nighttimeQueue, "src/main/resources/NighttimeAccidents.csv"); | ||
|
||
readerExecutor.shutdown(); | ||
readerExecutor.awaitTermination(5, TimeUnit.MINUTES); | ||
while (!readerQueue.isEmpty()) | ||
Thread.sleep(1000); | ||
for (Thread processorThread : processorThreadList) | ||
processorThread.interrupt(); | ||
while (!daytimeQueue.isEmpty()) | ||
Thread.sleep(1000); | ||
daytimeWriterThread.interrupt(); | ||
while (!nighttimeQueue.isEmpty()) | ||
Thread.sleep(1000); | ||
nighttimeWriterThread.interrupt(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.epam.dataservice; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.util.Random; | ||
|
||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVParser; | ||
import org.apache.commons.csv.CSVRecord; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class IntegrationTest { | ||
|
||
@BeforeClass | ||
public static void setUpBeforeClass() throws Exception { | ||
AccidentReaderProcessorWriterTest.main(null); | ||
} | ||
|
||
@Test | ||
public void testAccidentReaderProcessorWriter4Daytime() { | ||
try { | ||
Reader reader = new FileReader("src/main/resources/DaytimeAccidents.csv"); | ||
CSVParser records = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); | ||
int i = 0; | ||
for (CSVRecord record : records) { | ||
Integer hour = Integer.valueOf(record.get(3)); | ||
assertTrue("record #" + i + " invalid", hour >= 6 && hour < 18); | ||
i++; | ||
} | ||
records.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testAccidentReaderProcessorWriter4Nighttime() { | ||
try { | ||
Reader reader = new FileReader("src/main/resources/NighttimeAccidents.csv"); | ||
CSVParser records = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); | ||
int i = 0; | ||
for (CSVRecord record : records) { | ||
Integer hour = Integer.valueOf(record.get(3)); | ||
assertTrue("record #" + i + " invalid", hour < 6 || hour >= 18); | ||
i++; | ||
} | ||
records.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/com/epam/dataservice/PoliceForceServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.epam.dataservice; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class PoliceForceServiceTest { | ||
|
||
private static PoliceForceService policeForceService; | ||
|
||
@BeforeClass | ||
public static void setUpBeforeClass() throws Exception { | ||
policeForceService = new PoliceForceService(); | ||
} | ||
|
||
@Test | ||
public void testGetContactNo() { | ||
assertEquals("1316386212", policeForceService.getContactNo("North Yorkshire")); | ||
assertEquals("1316386213", policeForceService.getContactNo("West Yorkshire")); | ||
assertEquals("1316386214", policeForceService.getContactNo("South Yorkshire")); | ||
assertEquals("1316386216", policeForceService.getContactNo("Humberside")); | ||
assertEquals("1316386217", policeForceService.getContactNo("Cleveland")); | ||
assertEquals("1316386220", policeForceService.getContactNo("West Midlands")); | ||
assertEquals("1316386221", policeForceService.getContactNo("Staffordshire")); | ||
assertEquals("1316386222", policeForceService.getContactNo("West Mercia")); | ||
assertEquals("1316386223", policeForceService.getContactNo("Warwickshire")); | ||
assertEquals("1316386230", policeForceService.getContactNo("Derbyshire")); | ||
assertEquals("1316386231", policeForceService.getContactNo("Nottinghamshire")); | ||
assertEquals("1316386232", policeForceService.getContactNo("Lincolnshire")); | ||
assertEquals("1316386233", policeForceService.getContactNo("Leicestershire")); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
North, never live empty Exception. At least leave comment why it's empty. It's extremely tough to find error in that case