Skip to content

Commit 3e0098a

Browse files
committed
Code refactoring for Java 8
1 parent f9d1be8 commit 3e0098a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+218
-148
lines changed

Ant-Common.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
<attribute name="timeOut" default="15000"/>
2424
<attribute name="msg" default=""/>
2525
<sequential>
26-
<echo>Running: ${chapter} @{cls}&#13;</echo>
27-
<echo file="${antoutput}" append="true">Running: ${chapter} @{cls}&#13;</echo>
26+
<echo>[${chapter}] Running: java @{cls} @{arguments}&#13;</echo>
27+
<echo file="${antoutput}" append="true">[${chapter}] Running: java @{cls} @{arguments}&#13;</echo>
2828
<java
2929
classname="@{cls}"
3030
classpath="${java.class.path};${basedir};${basedir}/.."
@@ -35,7 +35,7 @@
3535
output="${antoutput}" append="true">
3636
<arg line="@{arguments}"/>
3737
</java>
38-
<echo file="${antoutput}" append="true">Finished: ${chapter} @{cls}&#13;</echo>
38+
<echo file="${antoutput}" append="true">[${chapter}] Finished: java @{cls} @{arguments}&#13;</echo>
3939
<echo file="${antoutput}" append="true">@{msg}&#13;</echo>
4040
<echo file="${antoutput}" append="true">--------------------------------&#13;</echo>
4141
</sequential>
@@ -49,8 +49,8 @@
4949
<attribute name="timeOut" default="15000"/>
5050
<attribute name="msg" default=""/>
5151
<sequential>
52-
<echo>Running: ${chapter} @{cls}&#13;</echo>
53-
<echo append="true">Running: ${chapter} @{cls}&#13;</echo>
52+
<echo>[${chapter}] Running: java @{cls} @{arguments}&#13;</echo>
53+
<echo append="true">[${chapter}] Running: java @{cls} @{arguments}&#13;</echo>
5454
<java
5555
classname="@{cls}"
5656
classpath="${java.class.path};${basedir};${basedir}/.."
@@ -61,7 +61,7 @@
6161
>
6262
<arg line="@{arguments}"/>
6363
</java>
64-
<echo append="true">Finished: ${chapter} @{cls}&#13;</echo>
64+
<echo append="true">[${chapter}] Finished: java @{cls} @{arguments}&#13;</echo>
6565
<echo append="true">@{msg}&#13;</echo>
6666
<echo append="true">--------------------------------&#13;</echo>
6767
</sequential>

arrays/GeneratorsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public static void test(Class<?> surroundingClass) {
1111
for(int i = 0; i < size; i++)
1212
System.out.printf(g.next() + " ");
1313
System.out.println();
14-
} catch(Exception e) {
14+
} catch(InstantiationException |
15+
IllegalAccessException e) {
1516
throw new RuntimeException(e);
1617
}
1718
}

concurrency/ActiveObjectDemo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public static void main(String[] args) {
5252
if(f.isDone()) {
5353
try {
5454
print(f.get());
55-
} catch(Exception e) {
55+
} catch(InterruptedException |
56+
ExecutionException e) {
5657
throw new RuntimeException(e);
5758
}
5859
results.remove(f);

concurrency/GreenhouseScheduler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,10 @@ public void run() {
137137
if(rand.nextInt(5) == 4)
138138
tempDirection = -tempDirection;
139139
// Store previous value:
140-
lastTemp = lastTemp +
141-
tempDirection * (1.0f + rand.nextFloat());
140+
lastTemp += tempDirection * (1.0f + rand.nextFloat());
142141
if(rand.nextInt(5) == 4)
143142
humidityDirection = -humidityDirection;
144-
lastHumidity = lastHumidity +
145-
humidityDirection * rand.nextFloat();
143+
lastHumidity += humidityDirection * rand.nextFloat();
146144
// Calendar must be cloned, otherwise all
147145
// DataPoints hold references to the same lastTime.
148146
// For a basic object like Calendar, clone() is OK.

concurrency/InterruptingIdiom.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void run() {
3535
print("Calculating");
3636
// A time-consuming, non-blocking operation:
3737
for(int i = 1; i < 2500000; i++)
38-
d = d + (Math.PI + Math.E) / d;
38+
d += (Math.PI + Math.E) / d;
3939
print("Finished time-consuming operation");
4040
} finally {
4141
n2.cleanup();

concurrency/Pool.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public Pool(Class<T> classObject, int size) {
1818
try {
1919
// Assumes a default constructor:
2020
items.add(classObject.newInstance());
21-
} catch(Exception e) {
21+
} catch(InstantiationException |
22+
IllegalAccessException e) {
2223
throw new RuntimeException(e);
2324
}
2425
}

concurrency/ResponsiveUI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class UnresponsiveUI {
66
private volatile double d = 1;
77
public UnresponsiveUI() throws Exception {
88
while(d > 0)
9-
d = d + (Math.PI + Math.E) / d;
9+
d += (Math.PI + Math.E) / d;
1010
System.in.read(); // Never gets here
1111
}
1212
}
@@ -20,7 +20,7 @@ public ResponsiveUI() {
2020
@Override
2121
public void run() {
2222
while(true) {
23-
d = d + (Math.PI + Math.E) / d;
23+
d += (Math.PI + Math.E) / d;
2424
}
2525
}
2626
public static void main(String[] args) throws Exception {

concurrency/SynchronizationComparisons.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public void run() {
3535
accumulate();
3636
try {
3737
barrier.await();
38-
} catch(Exception e) {
38+
} catch(InterruptedException |
39+
BrokenBarrierException e) {
3940
throw new RuntimeException(e);
4041
}
4142
}
@@ -48,7 +49,8 @@ public void run() {
4849
value = read();
4950
try {
5051
barrier.await();
51-
} catch(Exception e) {
52+
} catch(InterruptedException |
53+
BrokenBarrierException e) {
5254
throw new RuntimeException(e);
5355
}
5456
}
@@ -61,7 +63,8 @@ public void timedTest() {
6163
}
6264
try {
6365
barrier.await();
64-
} catch(Exception e) {
66+
} catch(InterruptedException |
67+
BrokenBarrierException e) {
6568
throw new RuntimeException(e);
6669
}
6770
duration = System.nanoTime() - start;

containers/CountedString.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void main(String[] args) {
4444
CountedString[] cs = new CountedString[5];
4545
for(int i = 0; i < cs.length; i++) {
4646
cs[i] = new CountedString("hi");
47-
map.put(cs[i], i); // Autobox int -> Integer
47+
map.put(cs[i], i); // Autobox int to Integer
4848
}
4949
print(map);
5050
for(CountedString cstring : cs) {

containers/TypesForSets.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//: containers/TypesForSets.java
22
// Methods necessary to put your own type in a Set.
3+
import java.lang.reflect.InvocationTargetException;
34
import java.util.*;
45

56
class SetType {
@@ -34,7 +35,12 @@ static <T> Set<T> fill(Set<T> set, Class<T> type) {
3435
for(int i = 0; i < 10; i++)
3536
set.add(
3637
type.getConstructor(int.class).newInstance(i));
37-
} catch(Exception e) {
38+
} catch(NoSuchMethodException |
39+
SecurityException |
40+
InstantiationException |
41+
IllegalAccessException |
42+
IllegalArgumentException |
43+
InvocationTargetException e) {
3844
throw new RuntimeException(e);
3945
}
4046
return set;

0 commit comments

Comments
 (0)