blob: e4356a3e050aaae1dc5e98137bde7db578c064cf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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);
}
}
}
}
|