summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java1/aufgabe10/LineWordCount.java
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2014-12-07 21:21:51 +0100
committerStefan Suhren <suhren.stefan@fh-swf.de>2014-12-07 21:21:51 +0100
commitcfd8642679d1d105a275d793f7049b4a24fda4b7 (patch)
tree315498bcce7564992bb7a74ea4bc9f8c0bbabbd4 /src/de/fhswf/in/inf/java1/aufgabe10/LineWordCount.java
parent9acea903216dbe371dd7b41cbf23b46a5732bcb4 (diff)
downloadJava1-cfd8642679d1d105a275d793f7049b4a24fda4b7.tar.gz
Java1-cfd8642679d1d105a275d793f7049b4a24fda4b7.zip
Assignment No.10 implements classes.
Diffstat (limited to 'src/de/fhswf/in/inf/java1/aufgabe10/LineWordCount.java')
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe10/LineWordCount.java79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe10/LineWordCount.java b/src/de/fhswf/in/inf/java1/aufgabe10/LineWordCount.java
new file mode 100644
index 0000000..e4356a3
--- /dev/null
+++ b/src/de/fhswf/in/inf/java1/aufgabe10/LineWordCount.java
@@ -0,0 +1,79 @@
+/**
+ *
+ */
+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);
+ }
+ }
+ }
+}