diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2014-12-10 14:32:44 +0100 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2014-12-10 14:32:44 +0100 |
| commit | 0669d3e06127a04c2a419a10664a259847e194fb (patch) | |
| tree | 3e8bdf3e41c635d35252127659ebb6cc2943417a /src | |
| parent | cfd8642679d1d105a275d793f7049b4a24fda4b7 (diff) | |
| download | Java1-0669d3e06127a04c2a419a10664a259847e194fb.tar.gz Java1-0669d3e06127a04c2a419a10664a259847e194fb.zip | |
Assignment No.10 after correction
Diffstat (limited to 'src')
3 files changed, 66 insertions, 82 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe10/LineWordCount.java b/src/de/fhswf/in/inf/java1/aufgabe10/LineWordCount.java deleted file mode 100644 index e4356a3..0000000 --- a/src/de/fhswf/in/inf/java1/aufgabe10/LineWordCount.java +++ /dev/null @@ -1,79 +0,0 @@ -/** - * - */ -package de.fhswf.in.inf.java1.aufgabe10; - -import java.util.concurrent.ConcurrentSkipListMap; - -/** - * Counts the words in one line. - * - * @author $Author: $ - * @version $Revision: $, $Date: $ UTC - */ -public class LineWordCount implements Runnable -{ - - private String[] lineArray; - - private ConcurrentSkipListMap<String, Integer> lineWordMap; - - private static final Integer NEWVALUE = new Integer(1); - - /** - * Creates the object which will be threaded. - * - * @param wordMap - * The wordMap into which the counts should be added. - * @param line - * The line to be counted. - */ - public LineWordCount(ConcurrentSkipListMap<String, Integer> wordMap, - String line) - { - if (wordMap == null) - { - throw new IllegalArgumentException("WordMap can't be null."); - } - if (line == null) - { - throw new IllegalArgumentException("Line can't be null."); - } - if (line.isEmpty()) - { - throw new IllegalArgumentException("Line can't be empty."); - } - lineWordMap = wordMap; - lineArray = line.split("[^\\p{IsAlphabetic}\\p{Digit}]+"); - } - - /* - * (non-Javadoc) - * - * @see java.lang.Runnable#run() - */ - @Override - public final void run() - { - for (String word : lineArray) - { - // Split creates empty String, if first char is a split char. - if (!word.isEmpty()) - { - word = word.toLowerCase(); - boolean success; - - do - { - success = true; - Integer tmp = lineWordMap.putIfAbsent(word, NEWVALUE); - if (tmp != null) - { - success = lineWordMap.replace(word, tmp, tmp + 1); - } - } - while (!success); - } - } - } -} diff --git a/src/de/fhswf/in/inf/java1/aufgabe10/ParallelWordCount.java b/src/de/fhswf/in/inf/java1/aufgabe10/ParallelWordCount.java index 5793f02..04a2bcb 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe10/ParallelWordCount.java +++ b/src/de/fhswf/in/inf/java1/aufgabe10/ParallelWordCount.java @@ -22,6 +22,70 @@ import java.util.concurrent.TimeUnit; */ public class ParallelWordCount { + private static final Integer NEWVALUE = new Integer(1); + + /** + * Counts words in one line. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ + private class LineWordCount implements Runnable + { + + private String[] lineArray; + + /** + * Creates the object which will be threaded. + * + * @param wordMap + * The wordMap into which the counts should be added. + * @param line + * The line to be counted. + */ + public LineWordCount(String line) + { + if (line == null) + { + throw new IllegalArgumentException("Line can't be null."); + } + if (line.isEmpty()) + { + throw new IllegalArgumentException("Line can't be empty."); + } + lineArray = line.split("[^\\p{IsAlphabetic}\\p{Digit}]+"); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Runnable#run() + */ + @Override + public final void run() + { + for (String word : lineArray) + { + // Split creates empty String, if first char is a split char. + if (!word.isEmpty()) + { + word = word.toLowerCase(); + boolean success; + + do + { + success = true; + Integer tmp = wordMap.putIfAbsent(word, NEWVALUE); + if (tmp != null) + { + success = wordMap.replace(word, tmp, tmp + 1); + } + } + while (!success); + } + } + } + } private static final int TIMEOUT = 30; @@ -43,7 +107,6 @@ public class ParallelWordCount */ public final void readFile(File file) { - try (BufferedReader f = new BufferedReader(new FileReader(file))) { if (wordMap == null) @@ -60,7 +123,7 @@ public class ParallelWordCount while ((line = f.readLine()) != null) { - Runnable worker = new LineWordCount(wordMap, line); + Runnable worker = new LineWordCount(line); executer.execute(worker); } diff --git a/src/de/fhswf/in/inf/java1/aufgabe10/WordCountTest.java b/src/de/fhswf/in/inf/java1/aufgabe10/WordCountTest.java index 6d9b12c..b7c1a61 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe10/WordCountTest.java +++ b/src/de/fhswf/in/inf/java1/aufgabe10/WordCountTest.java @@ -31,7 +31,7 @@ public final class WordCountTest public static void main(String[] args) { File file = new File( - "/home/aentfs/workspace/Java1/misc/Blatt9TestFile2.txt"); + "C:/eclipse/workspace/java1/misc/Blatt9TestFile2.txt"); ParallelWordCount tmp = new ParallelWordCount(); tmp.readFile(file); |
