Code refactoring for Java 8
This commit is contained in:
parent
f9d1be83be
commit
3e0098ae73
@ -23,8 +23,8 @@
|
|||||||
<attribute name="timeOut" default="15000"/>
|
<attribute name="timeOut" default="15000"/>
|
||||||
<attribute name="msg" default=""/>
|
<attribute name="msg" default=""/>
|
||||||
<sequential>
|
<sequential>
|
||||||
<echo>Running: ${chapter} @{cls} </echo>
|
<echo>[${chapter}] Running: java @{cls} @{arguments} </echo>
|
||||||
<echo file="${antoutput}" append="true">Running: ${chapter} @{cls} </echo>
|
<echo file="${antoutput}" append="true">[${chapter}] Running: java @{cls} @{arguments} </echo>
|
||||||
<java
|
<java
|
||||||
classname="@{cls}"
|
classname="@{cls}"
|
||||||
classpath="${java.class.path};${basedir};${basedir}/.."
|
classpath="${java.class.path};${basedir};${basedir}/.."
|
||||||
@ -35,7 +35,7 @@
|
|||||||
output="${antoutput}" append="true">
|
output="${antoutput}" append="true">
|
||||||
<arg line="@{arguments}"/>
|
<arg line="@{arguments}"/>
|
||||||
</java>
|
</java>
|
||||||
<echo file="${antoutput}" append="true">Finished: ${chapter} @{cls} </echo>
|
<echo file="${antoutput}" append="true">[${chapter}] Finished: java @{cls} @{arguments} </echo>
|
||||||
<echo file="${antoutput}" append="true">@{msg} </echo>
|
<echo file="${antoutput}" append="true">@{msg} </echo>
|
||||||
<echo file="${antoutput}" append="true">-------------------------------- </echo>
|
<echo file="${antoutput}" append="true">-------------------------------- </echo>
|
||||||
</sequential>
|
</sequential>
|
||||||
@ -49,8 +49,8 @@
|
|||||||
<attribute name="timeOut" default="15000"/>
|
<attribute name="timeOut" default="15000"/>
|
||||||
<attribute name="msg" default=""/>
|
<attribute name="msg" default=""/>
|
||||||
<sequential>
|
<sequential>
|
||||||
<echo>Running: ${chapter} @{cls} </echo>
|
<echo>[${chapter}] Running: java @{cls} @{arguments} </echo>
|
||||||
<echo append="true">Running: ${chapter} @{cls} </echo>
|
<echo append="true">[${chapter}] Running: java @{cls} @{arguments} </echo>
|
||||||
<java
|
<java
|
||||||
classname="@{cls}"
|
classname="@{cls}"
|
||||||
classpath="${java.class.path};${basedir};${basedir}/.."
|
classpath="${java.class.path};${basedir};${basedir}/.."
|
||||||
@ -61,7 +61,7 @@
|
|||||||
>
|
>
|
||||||
<arg line="@{arguments}"/>
|
<arg line="@{arguments}"/>
|
||||||
</java>
|
</java>
|
||||||
<echo append="true">Finished: ${chapter} @{cls} </echo>
|
<echo append="true">[${chapter}] Finished: java @{cls} @{arguments} </echo>
|
||||||
<echo append="true">@{msg} </echo>
|
<echo append="true">@{msg} </echo>
|
||||||
<echo append="true">-------------------------------- </echo>
|
<echo append="true">-------------------------------- </echo>
|
||||||
</sequential>
|
</sequential>
|
||||||
|
@ -11,7 +11,8 @@ public class GeneratorsTest {
|
|||||||
for(int i = 0; i < size; i++)
|
for(int i = 0; i < size; i++)
|
||||||
System.out.printf(g.next() + " ");
|
System.out.printf(g.next() + " ");
|
||||||
System.out.println();
|
System.out.println();
|
||||||
} catch(Exception e) {
|
} catch(InstantiationException |
|
||||||
|
IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,8 @@ public class ActiveObjectDemo {
|
|||||||
if(f.isDone()) {
|
if(f.isDone()) {
|
||||||
try {
|
try {
|
||||||
print(f.get());
|
print(f.get());
|
||||||
} catch(Exception e) {
|
} catch(InterruptedException |
|
||||||
|
ExecutionException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
results.remove(f);
|
results.remove(f);
|
||||||
|
@ -137,12 +137,10 @@ public class GreenhouseScheduler {
|
|||||||
if(rand.nextInt(5) == 4)
|
if(rand.nextInt(5) == 4)
|
||||||
tempDirection = -tempDirection;
|
tempDirection = -tempDirection;
|
||||||
// Store previous value:
|
// Store previous value:
|
||||||
lastTemp = lastTemp +
|
lastTemp += tempDirection * (1.0f + rand.nextFloat());
|
||||||
tempDirection * (1.0f + rand.nextFloat());
|
|
||||||
if(rand.nextInt(5) == 4)
|
if(rand.nextInt(5) == 4)
|
||||||
humidityDirection = -humidityDirection;
|
humidityDirection = -humidityDirection;
|
||||||
lastHumidity = lastHumidity +
|
lastHumidity += humidityDirection * rand.nextFloat();
|
||||||
humidityDirection * rand.nextFloat();
|
|
||||||
// Calendar must be cloned, otherwise all
|
// Calendar must be cloned, otherwise all
|
||||||
// DataPoints hold references to the same lastTime.
|
// DataPoints hold references to the same lastTime.
|
||||||
// For a basic object like Calendar, clone() is OK.
|
// For a basic object like Calendar, clone() is OK.
|
||||||
|
@ -35,7 +35,7 @@ class Blocked3 implements Runnable {
|
|||||||
print("Calculating");
|
print("Calculating");
|
||||||
// A time-consuming, non-blocking operation:
|
// A time-consuming, non-blocking operation:
|
||||||
for(int i = 1; i < 2500000; i++)
|
for(int i = 1; i < 2500000; i++)
|
||||||
d = d + (Math.PI + Math.E) / d;
|
d += (Math.PI + Math.E) / d;
|
||||||
print("Finished time-consuming operation");
|
print("Finished time-consuming operation");
|
||||||
} finally {
|
} finally {
|
||||||
n2.cleanup();
|
n2.cleanup();
|
||||||
|
@ -18,7 +18,8 @@ public class Pool<T> {
|
|||||||
try {
|
try {
|
||||||
// Assumes a default constructor:
|
// Assumes a default constructor:
|
||||||
items.add(classObject.newInstance());
|
items.add(classObject.newInstance());
|
||||||
} catch(Exception e) {
|
} catch(InstantiationException |
|
||||||
|
IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ class UnresponsiveUI {
|
|||||||
private volatile double d = 1;
|
private volatile double d = 1;
|
||||||
public UnresponsiveUI() throws Exception {
|
public UnresponsiveUI() throws Exception {
|
||||||
while(d > 0)
|
while(d > 0)
|
||||||
d = d + (Math.PI + Math.E) / d;
|
d += (Math.PI + Math.E) / d;
|
||||||
System.in.read(); // Never gets here
|
System.in.read(); // Never gets here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -20,7 +20,7 @@ public class ResponsiveUI extends Thread {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while(true) {
|
while(true) {
|
||||||
d = d + (Math.PI + Math.E) / d;
|
d += (Math.PI + Math.E) / d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
@ -35,7 +35,8 @@ abstract class Accumulator {
|
|||||||
accumulate();
|
accumulate();
|
||||||
try {
|
try {
|
||||||
barrier.await();
|
barrier.await();
|
||||||
} catch(Exception e) {
|
} catch(InterruptedException |
|
||||||
|
BrokenBarrierException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -48,7 +49,8 @@ abstract class Accumulator {
|
|||||||
value = read();
|
value = read();
|
||||||
try {
|
try {
|
||||||
barrier.await();
|
barrier.await();
|
||||||
} catch(Exception e) {
|
} catch(InterruptedException |
|
||||||
|
BrokenBarrierException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,7 +63,8 @@ abstract class Accumulator {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
barrier.await();
|
barrier.await();
|
||||||
} catch(Exception e) {
|
} catch(InterruptedException |
|
||||||
|
BrokenBarrierException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
duration = System.nanoTime() - start;
|
duration = System.nanoTime() - start;
|
||||||
|
@ -44,7 +44,7 @@ public class CountedString {
|
|||||||
CountedString[] cs = new CountedString[5];
|
CountedString[] cs = new CountedString[5];
|
||||||
for(int i = 0; i < cs.length; i++) {
|
for(int i = 0; i < cs.length; i++) {
|
||||||
cs[i] = new CountedString("hi");
|
cs[i] = new CountedString("hi");
|
||||||
map.put(cs[i], i); // Autobox int -> Integer
|
map.put(cs[i], i); // Autobox int to Integer
|
||||||
}
|
}
|
||||||
print(map);
|
print(map);
|
||||||
for(CountedString cstring : cs) {
|
for(CountedString cstring : cs) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
//: containers/TypesForSets.java
|
//: containers/TypesForSets.java
|
||||||
// Methods necessary to put your own type in a Set.
|
// Methods necessary to put your own type in a Set.
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
class SetType {
|
class SetType {
|
||||||
@ -34,7 +35,12 @@ public class TypesForSets {
|
|||||||
for(int i = 0; i < 10; i++)
|
for(int i = 0; i < 10; i++)
|
||||||
set.add(
|
set.add(
|
||||||
type.getConstructor(int.class).newInstance(i));
|
type.getConstructor(int.class).newInstance(i));
|
||||||
} catch(Exception e) {
|
} catch(NoSuchMethodException |
|
||||||
|
SecurityException |
|
||||||
|
InstantiationException |
|
||||||
|
IllegalAccessException |
|
||||||
|
IllegalArgumentException |
|
||||||
|
InvocationTargetException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
return set;
|
return set;
|
||||||
|
@ -5,13 +5,13 @@ public class StringSwitch {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String color = "red";
|
String color = "red";
|
||||||
// Old way: using if-then
|
// Old way: using if-then
|
||||||
if(color == "red") {
|
if("red".equals(color)) {
|
||||||
print("RED");
|
print("RED");
|
||||||
} else if(color == "green") {
|
} else if("green".equals(color)) {
|
||||||
print("GREEN");
|
print("GREEN");
|
||||||
} else if(color == "blue") {
|
} else if("blue".equals(color)) {
|
||||||
print("BLUE");
|
print("BLUE");
|
||||||
} else if(color == "yellow") {
|
} else if("yellow".equals(color)) {
|
||||||
print("YELLOW");
|
print("YELLOW");
|
||||||
} else {
|
} else {
|
||||||
print("Unknown");
|
print("Unknown");
|
||||||
|
@ -11,12 +11,8 @@ public class EnumMaps {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
EnumMap<AlarmPoints,Command> em =
|
EnumMap<AlarmPoints,Command> em =
|
||||||
new EnumMap<>(AlarmPoints.class);
|
new EnumMap<>(AlarmPoints.class);
|
||||||
em.put(KITCHEN, (Command) () -> {
|
em.put(KITCHEN, () -> print("Kitchen fire!"));
|
||||||
print("Kitchen fire!");
|
em.put(BATHROOM, () -> print("Bathroom alert!"));
|
||||||
});
|
|
||||||
em.put(BATHROOM, (Command) () -> {
|
|
||||||
print("Bathroom alert!");
|
|
||||||
});
|
|
||||||
for(Map.Entry<AlarmPoints,Command> e : em.entrySet()) {
|
for(Map.Entry<AlarmPoints,Command> e : em.entrySet()) {
|
||||||
printnb(e.getKey() + ": ");
|
printnb(e.getKey() + ": ");
|
||||||
e.getValue().action();
|
e.getValue().action();
|
||||||
|
@ -93,9 +93,8 @@ public class DynamicFields {
|
|||||||
print("df: " + df);
|
print("df: " + df);
|
||||||
print("df.getField(\"d\") : " + df.getField("d"));
|
print("df.getField(\"d\") : " + df.getField("d"));
|
||||||
Object field = df.setField("d", null); // Exception
|
Object field = df.setField("d", null); // Exception
|
||||||
} catch(NoSuchFieldException e) {
|
} catch(NoSuchFieldException |
|
||||||
e.printStackTrace(System.out);
|
DynamicFieldsException e) {
|
||||||
} catch(DynamicFieldsException e) {
|
|
||||||
e.printStackTrace(System.out);
|
e.printStackTrace(System.out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,8 @@ public class LostMessage {
|
|||||||
} finally {
|
} finally {
|
||||||
lm.dispose();
|
lm.dispose();
|
||||||
}
|
}
|
||||||
} catch(Exception e) {
|
} catch(VeryImportantException |
|
||||||
|
HoHumException e) {
|
||||||
System.out.println(e);
|
System.out.println(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,8 @@ class WrapCheckedException {
|
|||||||
case 2: throw new RuntimeException("Where am I?");
|
case 2: throw new RuntimeException("Where am I?");
|
||||||
default: return;
|
default: return;
|
||||||
}
|
}
|
||||||
} catch(Exception e) { // Adapt to unchecked:
|
} catch(IOException | RuntimeException e) {
|
||||||
|
// Adapt to unchecked:
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,9 @@ public class Apply {
|
|||||||
try {
|
try {
|
||||||
for(T t: seq)
|
for(T t: seq)
|
||||||
f.invoke(t, args);
|
f.invoke(t, args);
|
||||||
} catch(Exception e) {
|
} catch(IllegalAccessException |
|
||||||
|
IllegalArgumentException |
|
||||||
|
InvocationTargetException e) {
|
||||||
// Failures are programmer errors
|
// Failures are programmer errors
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@ -32,7 +34,8 @@ class FilledList<T> extends ArrayList<T> {
|
|||||||
for(int i = 0; i < size; i++)
|
for(int i = 0; i < size; i++)
|
||||||
// Assumes default constructor:
|
// Assumes default constructor:
|
||||||
add(type.newInstance());
|
add(type.newInstance());
|
||||||
} catch(Exception e) {
|
} catch(InstantiationException |
|
||||||
|
IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,8 @@ public class Fill {
|
|||||||
// Assumes default constructor:
|
// Assumes default constructor:
|
||||||
try {
|
try {
|
||||||
collection.add(classToken.newInstance());
|
collection.add(classToken.newInstance());
|
||||||
} catch(Exception e) {
|
} catch(InstantiationException |
|
||||||
|
IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,8 @@ public class Fill2 {
|
|||||||
for(int i = 0; i < size; i++)
|
for(int i = 0; i < size; i++)
|
||||||
try {
|
try {
|
||||||
addable.add(classToken.newInstance());
|
addable.add(classToken.newInstance());
|
||||||
} catch(Exception e) {
|
} catch(InstantiationException |
|
||||||
|
IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,9 @@ public class GenericArray2<T> {
|
|||||||
System.out.println();
|
System.out.println();
|
||||||
try {
|
try {
|
||||||
Integer[] ia = gai.rep();
|
Integer[] ia = gai.rep();
|
||||||
} catch(Exception e) { System.out.println(e); }
|
} catch(Exception e) {
|
||||||
|
System.out.println(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} /* Output: (Sample)
|
} /* Output: (Sample)
|
||||||
0 1 2 3 4 5 6 7 8 9
|
0 1 2 3 4 5 6 7 8 9
|
||||||
|
@ -6,7 +6,8 @@ class ClassAsFactory<T> {
|
|||||||
public ClassAsFactory(Class<T> kind) {
|
public ClassAsFactory(Class<T> kind) {
|
||||||
try {
|
try {
|
||||||
x = kind.newInstance();
|
x = kind.newInstance();
|
||||||
} catch(Exception e) {
|
} catch(InstantiationException |
|
||||||
|
IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,10 @@ class CommunicateReflectively {
|
|||||||
} catch(NoSuchMethodException e) {
|
} catch(NoSuchMethodException e) {
|
||||||
print(speaker + " cannot sit");
|
print(speaker + " cannot sit");
|
||||||
}
|
}
|
||||||
} catch(Exception e) {
|
} catch(SecurityException |
|
||||||
|
IllegalAccessException |
|
||||||
|
IllegalArgumentException |
|
||||||
|
InvocationTargetException e) {
|
||||||
throw new RuntimeException(speaker.toString(), e);
|
throw new RuntimeException(speaker.toString(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,8 @@ implements Generator<Coffee>, Iterable<Coffee> {
|
|||||||
return (Coffee)
|
return (Coffee)
|
||||||
types[rand.nextInt(types.length)].newInstance();
|
types[rand.nextInt(types.length)].newInstance();
|
||||||
// Report programmer errors at run time:
|
// Report programmer errors at run time:
|
||||||
} catch(Exception e) {
|
} catch(InstantiationException |
|
||||||
|
IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ public class Button2b extends JFrame {
|
|||||||
b1 = new JButton("Button 1"),
|
b1 = new JButton("Button 1"),
|
||||||
b2 = new JButton("Button 2");
|
b2 = new JButton("Button 2");
|
||||||
private JTextField txt = new JTextField(10);
|
private JTextField txt = new JTextField(10);
|
||||||
private ActionListener bl = (ActionEvent e) -> {
|
private ActionListener bl = e -> {
|
||||||
String name1 = ((JButton)e.getSource()).getText();
|
String name1 = ((JButton)e.getSource()).getText();
|
||||||
txt.setText(name1);
|
txt.setText(name1);
|
||||||
};
|
};
|
||||||
|
@ -27,7 +27,12 @@ public class ButtonGroups extends JFrame {
|
|||||||
kind.getConstructor(String.class);
|
kind.getConstructor(String.class);
|
||||||
// Create a new object:
|
// Create a new object:
|
||||||
ab = (AbstractButton)ctor.newInstance(id);
|
ab = (AbstractButton)ctor.newInstance(id);
|
||||||
} catch(Exception ex) {
|
} catch(NoSuchMethodException |
|
||||||
|
SecurityException |
|
||||||
|
InstantiationException |
|
||||||
|
IllegalAccessException |
|
||||||
|
IllegalArgumentException |
|
||||||
|
InvocationTargetException ex) {
|
||||||
System.err.println("can't create " + kind);
|
System.err.println("can't create " + kind);
|
||||||
}
|
}
|
||||||
bg.add(ab);
|
bg.add(ab);
|
||||||
|
@ -12,15 +12,9 @@ public class CheckBoxes extends JFrame {
|
|||||||
cb2 = new JCheckBox("Check Box 2"),
|
cb2 = new JCheckBox("Check Box 2"),
|
||||||
cb3 = new JCheckBox("Check Box 3");
|
cb3 = new JCheckBox("Check Box 3");
|
||||||
public CheckBoxes() {
|
public CheckBoxes() {
|
||||||
cb1.addActionListener((ActionEvent e) -> {
|
cb1.addActionListener(e -> trace("1", cb1));
|
||||||
trace("1", cb1);
|
cb2.addActionListener(e -> trace("2", cb2));
|
||||||
});
|
cb3.addActionListener(e -> trace("3", cb3));
|
||||||
cb2.addActionListener((ActionEvent e) -> {
|
|
||||||
trace("2", cb2);
|
|
||||||
});
|
|
||||||
cb3.addActionListener((ActionEvent e) -> {
|
|
||||||
trace("3", cb3);
|
|
||||||
});
|
|
||||||
setLayout(new FlowLayout());
|
setLayout(new FlowLayout());
|
||||||
add(new JScrollPane(t));
|
add(new JScrollPane(t));
|
||||||
add(cb1);
|
add(cb1);
|
||||||
|
@ -11,18 +11,18 @@ public class ComboBoxes extends JFrame {
|
|||||||
"Somnescent", "Timorous", "Florid", "Putrescent"
|
"Somnescent", "Timorous", "Florid", "Putrescent"
|
||||||
};
|
};
|
||||||
private JTextField t = new JTextField(15);
|
private JTextField t = new JTextField(15);
|
||||||
private JComboBox c = new JComboBox();
|
private JComboBox<String> c = new JComboBox<>();
|
||||||
private JButton b = new JButton("Add items");
|
private JButton b = new JButton("Add items");
|
||||||
private int count = 0;
|
private int count = 0;
|
||||||
public ComboBoxes() {
|
public ComboBoxes() {
|
||||||
for(int i = 0; i < 4; i++)
|
for(int i = 0; i < 4; i++)
|
||||||
c.addItem(description[count++]);
|
c.addItem(description[count++]);
|
||||||
t.setEditable(false);
|
t.setEditable(false);
|
||||||
b.addActionListener((ActionEvent e) -> {
|
b.addActionListener(e -> {
|
||||||
if(count < description.length)
|
if(count < description.length)
|
||||||
c.addItem(description[count++]);
|
c.addItem(description[count++]);
|
||||||
});
|
});
|
||||||
c.addActionListener((ActionEvent e) -> {
|
c.addActionListener(e -> {
|
||||||
t.setText("index: "+ c.getSelectedIndex() + " " +
|
t.setText("index: "+ c.getSelectedIndex() + " " +
|
||||||
((JComboBox)e.getSource()).getSelectedItem());
|
((JComboBox)e.getSource()).getSelectedItem());
|
||||||
});
|
});
|
||||||
|
@ -11,9 +11,8 @@ class MyDialog extends JDialog {
|
|||||||
setLayout(new FlowLayout());
|
setLayout(new FlowLayout());
|
||||||
add(new JLabel("Here is my dialog"));
|
add(new JLabel("Here is my dialog"));
|
||||||
JButton ok = new JButton("OK");
|
JButton ok = new JButton("OK");
|
||||||
ok.addActionListener((ActionEvent e) -> {
|
ok.addActionListener(e ->
|
||||||
dispose(); // Closes the dialog
|
dispose()); // Closes the dialog
|
||||||
});
|
|
||||||
add(ok);
|
add(ok);
|
||||||
setSize(150,125);
|
setSize(150,125);
|
||||||
}
|
}
|
||||||
@ -23,9 +22,7 @@ public class Dialogs extends JFrame {
|
|||||||
private JButton b1 = new JButton("Dialog Box");
|
private JButton b1 = new JButton("Dialog Box");
|
||||||
private MyDialog dlg = new MyDialog(null);
|
private MyDialog dlg = new MyDialog(null);
|
||||||
public Dialogs() {
|
public Dialogs() {
|
||||||
b1.addActionListener((ActionEvent e) -> {
|
b1.addActionListener(e -> dlg.setVisible(true));
|
||||||
dlg.setVisible(true);
|
|
||||||
});
|
|
||||||
add(b1);
|
add(b1);
|
||||||
}
|
}
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -19,7 +19,7 @@ public class Faces extends JFrame {
|
|||||||
};
|
};
|
||||||
jb = new JButton("JButton", faces[3]);
|
jb = new JButton("JButton", faces[3]);
|
||||||
setLayout(new FlowLayout());
|
setLayout(new FlowLayout());
|
||||||
jb.addActionListener((ActionEvent e) -> {
|
jb.addActionListener(e -> {
|
||||||
if(mad) {
|
if(mad) {
|
||||||
jb.setIcon(faces[3]);
|
jb.setIcon(faces[3]);
|
||||||
mad = false;
|
mad = false;
|
||||||
@ -36,7 +36,7 @@ public class Faces extends JFrame {
|
|||||||
jb.setDisabledIcon(faces[4]);
|
jb.setDisabledIcon(faces[4]);
|
||||||
jb.setToolTipText("Yow!");
|
jb.setToolTipText("Yow!");
|
||||||
add(jb);
|
add(jb);
|
||||||
jb2.addActionListener((ActionEvent e) -> {
|
jb2.addActionListener(e -> {
|
||||||
if(jb.isEnabled()) {
|
if(jb.isEnabled()) {
|
||||||
jb.setEnabled(false);
|
jb.setEnabled(false);
|
||||||
jb2.setText("Enable");
|
jb2.setText("Enable");
|
||||||
|
@ -10,7 +10,7 @@ public class HTMLButton extends JFrame {
|
|||||||
"<html><b><font size=+2>" +
|
"<html><b><font size=+2>" +
|
||||||
"<center>Hello!<br><i>Press me now!");
|
"<center>Hello!<br><i>Press me now!");
|
||||||
public HTMLButton() {
|
public HTMLButton() {
|
||||||
b.addActionListener((ActionEvent e) -> {
|
b.addActionListener(e -> {
|
||||||
add(new JLabel("<html>" +
|
add(new JLabel("<html>" +
|
||||||
"<i><font size=+4>Kapow!"));
|
"<i><font size=+4>Kapow!"));
|
||||||
// Force a re-layout to include the new label:
|
// Force a re-layout to include the new label:
|
||||||
|
@ -25,16 +25,16 @@ InterruptableLongRunningCallable extends JFrame {
|
|||||||
private TaskManager<String,CallableTask> manager =
|
private TaskManager<String,CallableTask> manager =
|
||||||
new TaskManager<>();
|
new TaskManager<>();
|
||||||
public InterruptableLongRunningCallable() {
|
public InterruptableLongRunningCallable() {
|
||||||
b1.addActionListener((ActionEvent e) -> {
|
b1.addActionListener(e -> {
|
||||||
CallableTask task = new CallableTask();
|
CallableTask task = new CallableTask();
|
||||||
manager.add(task);
|
manager.add(task);
|
||||||
System.out.println(task + " added to the queue");
|
System.out.println(task + " added to the queue");
|
||||||
});
|
});
|
||||||
b2.addActionListener((ActionEvent e) -> {
|
b2.addActionListener(e -> {
|
||||||
for(String result : manager.purge())
|
for(String result : manager.purge())
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
});
|
});
|
||||||
b3.addActionListener((ActionEvent e) -> {
|
b3.addActionListener(e -> {
|
||||||
// Sample call to a Task method:
|
// Sample call to a Task method:
|
||||||
for(TaskItem<String,CallableTask> tt : manager)
|
for(TaskItem<String,CallableTask> tt : manager)
|
||||||
tt.task.id(); // No cast required
|
tt.task.id(); // No cast required
|
||||||
|
@ -32,12 +32,12 @@ public class InterruptableLongRunningTask extends JFrame {
|
|||||||
ExecutorService executor =
|
ExecutorService executor =
|
||||||
Executors.newSingleThreadExecutor();
|
Executors.newSingleThreadExecutor();
|
||||||
public InterruptableLongRunningTask() {
|
public InterruptableLongRunningTask() {
|
||||||
b1.addActionListener((ActionEvent e) -> {
|
b1.addActionListener(e -> {
|
||||||
Task task = new Task();
|
Task task = new Task();
|
||||||
executor.execute(task);
|
executor.execute(task);
|
||||||
System.out.println(task + " added to the queue");
|
System.out.println(task + " added to the queue");
|
||||||
});
|
});
|
||||||
b2.addActionListener((ActionEvent e) -> {
|
b2.addActionListener(e -> {
|
||||||
executor.shutdownNow(); // Heavy-handed
|
executor.shutdownNow(); // Heavy-handed
|
||||||
});
|
});
|
||||||
setLayout(new FlowLayout());
|
setLayout(new FlowLayout());
|
||||||
|
@ -13,12 +13,13 @@ public class List extends JFrame {
|
|||||||
"Praline Cream", "Mud Pie"
|
"Praline Cream", "Mud Pie"
|
||||||
};
|
};
|
||||||
private int count = 0;
|
private int count = 0;
|
||||||
private DefaultListModel lItems = new DefaultListModel();
|
private DefaultListModel<String> lItems =
|
||||||
private JList lst = new JList(lItems);
|
new DefaultListModel<>();
|
||||||
|
private JList<String> lst = new JList<>(lItems);
|
||||||
private JTextArea t =
|
private JTextArea t =
|
||||||
new JTextArea(flavors.length, 20);
|
new JTextArea(flavors.length, 20);
|
||||||
private JButton b = new JButton("Add Item");
|
private JButton b = new JButton("Add Item");
|
||||||
private ActionListener bl = (ActionEvent e) -> {
|
private ActionListener bl = e -> {
|
||||||
if(count < flavors.length) {
|
if(count < flavors.length) {
|
||||||
lItems.add(0, flavors[count++]);
|
lItems.add(0, flavors[count++]);
|
||||||
} else {
|
} else {
|
||||||
@ -27,12 +28,11 @@ public class List extends JFrame {
|
|||||||
b.setEnabled(false);
|
b.setEnabled(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
private ListSelectionListener ll =
|
private ListSelectionListener ll = e -> {
|
||||||
(ListSelectionEvent e) -> {
|
if(e.getValueIsAdjusting()) return;
|
||||||
if(e.getValueIsAdjusting()) return;
|
t.setText("");
|
||||||
t.setText("");
|
for(Object item : lst.getSelectedValuesList())
|
||||||
for(Object item : lst.getSelectedValues())
|
t.append(item + "\n");
|
||||||
t.append(item + "\n");
|
|
||||||
};
|
};
|
||||||
public List() {
|
public List() {
|
||||||
t.setEditable(false);
|
t.setEditable(false);
|
||||||
|
@ -11,16 +11,16 @@ public class LongRunningTask extends JFrame {
|
|||||||
b1 = new JButton("Start Long Running Task"),
|
b1 = new JButton("Start Long Running Task"),
|
||||||
b2 = new JButton("End Long Running Task");
|
b2 = new JButton("End Long Running Task");
|
||||||
public LongRunningTask() {
|
public LongRunningTask() {
|
||||||
b1.addActionListener((ActionEvent evt) -> {
|
b1.addActionListener(e -> {
|
||||||
try {
|
try {
|
||||||
TimeUnit.SECONDS.sleep(3);
|
TimeUnit.SECONDS.sleep(3);
|
||||||
} catch(InterruptedException e) {
|
} catch(InterruptedException ex) {
|
||||||
System.out.println("Task interrupted");
|
System.out.println("Task interrupted");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
System.out.println("Task completed");
|
System.out.println("Task completed");
|
||||||
});
|
});
|
||||||
b2.addActionListener((ActionEvent evt) -> {
|
b2.addActionListener(e -> {
|
||||||
// Interrupt yourself?
|
// Interrupt yourself?
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
});
|
});
|
||||||
|
@ -14,8 +14,8 @@ public class LookAndFeel extends JFrame {
|
|||||||
new JLabel("JLabel"),
|
new JLabel("JLabel"),
|
||||||
new JCheckBox("JCheckBox"),
|
new JCheckBox("JCheckBox"),
|
||||||
new JRadioButton("Radio"),
|
new JRadioButton("Radio"),
|
||||||
new JComboBox(choices),
|
new JComboBox<String>(choices),
|
||||||
new JList(choices),
|
new JList<String>(choices),
|
||||||
};
|
};
|
||||||
public LookAndFeel() {
|
public LookAndFeel() {
|
||||||
super("Look And Feel");
|
super("Look And Feel");
|
||||||
@ -35,7 +35,10 @@ public class LookAndFeel extends JFrame {
|
|||||||
try {
|
try {
|
||||||
UIManager.setLookAndFeel(UIManager.
|
UIManager.setLookAndFeel(UIManager.
|
||||||
getCrossPlatformLookAndFeelClassName());
|
getCrossPlatformLookAndFeelClassName());
|
||||||
} catch(Exception e) {
|
} catch(ClassNotFoundException |
|
||||||
|
InstantiationException |
|
||||||
|
IllegalAccessException |
|
||||||
|
UnsupportedLookAndFeelException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -43,7 +46,10 @@ public class LookAndFeel extends JFrame {
|
|||||||
try {
|
try {
|
||||||
UIManager.setLookAndFeel(UIManager.
|
UIManager.setLookAndFeel(UIManager.
|
||||||
getSystemLookAndFeelClassName());
|
getSystemLookAndFeelClassName());
|
||||||
} catch(Exception e) {
|
} catch(ClassNotFoundException |
|
||||||
|
InstantiationException |
|
||||||
|
IllegalAccessException |
|
||||||
|
UnsupportedLookAndFeelException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -51,7 +57,10 @@ public class LookAndFeel extends JFrame {
|
|||||||
try {
|
try {
|
||||||
UIManager.setLookAndFeel("com.sun.java."+
|
UIManager.setLookAndFeel("com.sun.java."+
|
||||||
"swing.plaf.motif.MotifLookAndFeel");
|
"swing.plaf.motif.MotifLookAndFeel");
|
||||||
} catch(Exception e) {
|
} catch(ClassNotFoundException |
|
||||||
|
InstantiationException |
|
||||||
|
IllegalAccessException |
|
||||||
|
UnsupportedLookAndFeelException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -12,7 +12,7 @@ public class MessageBoxes extends JFrame {
|
|||||||
new JButton("3 Vals")
|
new JButton("3 Vals")
|
||||||
};
|
};
|
||||||
private JTextField txt = new JTextField(15);
|
private JTextField txt = new JTextField(15);
|
||||||
private ActionListener al = (ActionEvent e) -> {
|
private ActionListener al = e -> {
|
||||||
String id = ((JButton)e.getSource()).getText();
|
String id = ((JButton)e.getSource()).getText();
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case "Alert":
|
case "Alert":
|
||||||
|
@ -51,7 +51,7 @@ public class MonitoredLongRunningCallable extends JFrame {
|
|||||||
private TaskManager<String,MonitoredCallable> manager =
|
private TaskManager<String,MonitoredCallable> manager =
|
||||||
new TaskManager<>();
|
new TaskManager<>();
|
||||||
public MonitoredLongRunningCallable() {
|
public MonitoredLongRunningCallable() {
|
||||||
b1.addActionListener((ActionEvent e) -> {
|
b1.addActionListener(e -> {
|
||||||
MonitoredCallable task = new MonitoredCallable(
|
MonitoredCallable task = new MonitoredCallable(
|
||||||
new ProgressMonitor(
|
new ProgressMonitor(
|
||||||
MonitoredLongRunningCallable.this,
|
MonitoredLongRunningCallable.this,
|
||||||
@ -60,11 +60,11 @@ public class MonitoredLongRunningCallable extends JFrame {
|
|||||||
manager.add(task);
|
manager.add(task);
|
||||||
System.out.println(task + " added to the queue");
|
System.out.println(task + " added to the queue");
|
||||||
});
|
});
|
||||||
b2.addActionListener((ActionEvent e) -> {
|
b2.addActionListener(e -> {
|
||||||
for(String result : manager.purge())
|
for(String result : manager.purge())
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
});
|
});
|
||||||
b3.addActionListener((ActionEvent e) -> {
|
b3.addActionListener(e -> {
|
||||||
for(String result : manager.getResults())
|
for(String result : manager.getResults())
|
||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
});
|
});
|
||||||
|
@ -11,9 +11,8 @@ public class Popup extends JFrame {
|
|||||||
public Popup() {
|
public Popup() {
|
||||||
setLayout(new FlowLayout());
|
setLayout(new FlowLayout());
|
||||||
add(t);
|
add(t);
|
||||||
ActionListener al = (ActionEvent e) -> {
|
ActionListener al = e ->
|
||||||
t.setText(((JMenuItem)e.getSource()).getText());
|
t.setText(((JMenuItem)e.getSource()).getText());
|
||||||
};
|
|
||||||
JMenuItem m = new JMenuItem("Hither");
|
JMenuItem m = new JMenuItem("Hither");
|
||||||
m.addActionListener(al);
|
m.addActionListener(al);
|
||||||
popup.add(m);
|
popup.add(m);
|
||||||
|
@ -24,9 +24,8 @@ public class Progress extends JFrame {
|
|||||||
sb.setBorder(new TitledBorder("Slide Me"));
|
sb.setBorder(new TitledBorder("Slide Me"));
|
||||||
pb.setModel(sb.getModel()); // Share model
|
pb.setModel(sb.getModel()); // Share model
|
||||||
add(sb);
|
add(sb);
|
||||||
sb.addChangeListener((ChangeEvent e) -> {
|
sb.addChangeListener(e ->
|
||||||
pm.setProgress(sb.getValue());
|
pm.setProgress(sb.getValue()));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
run(new Progress(), 300, 200);
|
run(new Progress(), 300, 200);
|
||||||
|
@ -12,10 +12,9 @@ public class RadioButtons extends JFrame {
|
|||||||
rb1 = new JRadioButton("one", false),
|
rb1 = new JRadioButton("one", false),
|
||||||
rb2 = new JRadioButton("two", false),
|
rb2 = new JRadioButton("two", false),
|
||||||
rb3 = new JRadioButton("three", false);
|
rb3 = new JRadioButton("three", false);
|
||||||
private ActionListener al = (ActionEvent e) -> {
|
private ActionListener al = e ->
|
||||||
t.setText("Radio button " +
|
t.setText("Radio button " +
|
||||||
((JRadioButton)e.getSource()).getText());
|
((JRadioButton)e.getSource()).getText());
|
||||||
};
|
|
||||||
public RadioButtons() {
|
public RadioButtons() {
|
||||||
rb1.addActionListener(al);
|
rb1.addActionListener(al);
|
||||||
rb2.addActionListener(al);
|
rb2.addActionListener(al);
|
||||||
|
@ -6,9 +6,8 @@ import static net.mindview.util.SwingConsole.*;
|
|||||||
|
|
||||||
public class SimpleMenus extends JFrame {
|
public class SimpleMenus extends JFrame {
|
||||||
private JTextField t = new JTextField(15);
|
private JTextField t = new JTextField(15);
|
||||||
private ActionListener al = (ActionEvent e) -> {
|
private ActionListener al = e ->
|
||||||
t.setText(((JMenuItem)e.getSource()).getText());
|
t.setText(((JMenuItem)e.getSource()).getText());
|
||||||
};
|
|
||||||
private JMenu[] menus = {
|
private JMenu[] menus = {
|
||||||
new JMenu("Winken"), new JMenu("Blinken"),
|
new JMenu("Winken"), new JMenu("Blinken"),
|
||||||
new JMenu("Nod")
|
new JMenu("Nod")
|
||||||
|
@ -11,8 +11,7 @@ public class SubmitLabelManipulationTask {
|
|||||||
frame.setSize(300, 100);
|
frame.setSize(300, 100);
|
||||||
frame.setVisible(true);
|
frame.setVisible(true);
|
||||||
TimeUnit.SECONDS.sleep(1);
|
TimeUnit.SECONDS.sleep(1);
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() ->
|
||||||
label.setText("Hey! This is Different!");
|
label.setText("Hey! This is Different!"));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} ///:~
|
} ///:~
|
||||||
|
@ -14,12 +14,10 @@ public class SubmitSwingProgram extends JFrame {
|
|||||||
}
|
}
|
||||||
static SubmitSwingProgram ssp;
|
static SubmitSwingProgram ssp;
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() ->
|
||||||
ssp = new SubmitSwingProgram();
|
ssp = new SubmitSwingProgram());
|
||||||
});
|
|
||||||
TimeUnit.SECONDS.sleep(1);
|
TimeUnit.SECONDS.sleep(1);
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() ->
|
||||||
ssp.label.setText("Hey! This is Different!");
|
ssp.label.setText("Hey! This is Different!"));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} ///:~
|
} ///:~
|
||||||
|
@ -18,10 +18,9 @@ public class TabbedPane1 extends JFrame {
|
|||||||
for(String flavor : flavors)
|
for(String flavor : flavors)
|
||||||
tabs.addTab(flavors[i],
|
tabs.addTab(flavors[i],
|
||||||
new JButton("Tabbed pane " + i++));
|
new JButton("Tabbed pane " + i++));
|
||||||
tabs.addChangeListener((ChangeEvent e) -> {
|
tabs.addChangeListener(e ->
|
||||||
txt.setText("Tab selected: " +
|
txt.setText("Tab selected: " +
|
||||||
tabs.getSelectedIndex());
|
tabs.getSelectedIndex()));
|
||||||
});
|
|
||||||
add(BorderLayout.SOUTH, txt);
|
add(BorderLayout.SOUTH, txt);
|
||||||
add(tabs);
|
add(tabs);
|
||||||
}
|
}
|
||||||
|
@ -16,13 +16,11 @@ public class TextArea extends JFrame {
|
|||||||
public TextArea() {
|
public TextArea() {
|
||||||
// Use up all the data:
|
// Use up all the data:
|
||||||
m.putAll(Countries.capitals());
|
m.putAll(Countries.capitals());
|
||||||
b.addActionListener((ActionEvent e) -> {
|
b.addActionListener(e -> {
|
||||||
for(Map.Entry me : m.entrySet())
|
for(Map.Entry me : m.entrySet())
|
||||||
t.append(me.getKey() + ": "+ me.getValue()+"\n");
|
t.append(me.getKey() + ": "+ me.getValue()+"\n");
|
||||||
});
|
});
|
||||||
c.addActionListener((ActionEvent e) -> {
|
c.addActionListener(e -> t.setText(""));
|
||||||
t.setText("");
|
|
||||||
});
|
|
||||||
setLayout(new FlowLayout());
|
setLayout(new FlowLayout());
|
||||||
add(new JScrollPane(t));
|
add(new JScrollPane(t));
|
||||||
add(b);
|
add(b);
|
||||||
|
@ -12,7 +12,7 @@ public class TextPane extends JFrame {
|
|||||||
private static Generator sg =
|
private static Generator sg =
|
||||||
new RandomGenerator.String(7);
|
new RandomGenerator.String(7);
|
||||||
public TextPane() {
|
public TextPane() {
|
||||||
b.addActionListener((ActionEvent e) -> {
|
b.addActionListener(e -> {
|
||||||
for(int i = 1; i < 10; i++)
|
for(int i = 1; i < 10; i++)
|
||||||
tp.setText(tp.getText() + sg.next() + "\n");
|
tp.setText(tp.getText() + sg.next() + "\n");
|
||||||
});
|
});
|
||||||
|
@ -7,7 +7,7 @@ public class ArraysOfPrimitives {
|
|||||||
int[] a2;
|
int[] a2;
|
||||||
a2 = a1;
|
a2 = a1;
|
||||||
for(int i = 0; i < a2.length; i++)
|
for(int i = 0; i < a2.length; i++)
|
||||||
a2[i] = a2[i] + 1;
|
a2[i] += 1;
|
||||||
for(int i = 0; i < a1.length; i++)
|
for(int i = 0; i < a1.length; i++)
|
||||||
print("a1[" + i + "] = " + a1[i]);
|
print("a1[" + i + "] = " + a1[i]);
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,41 @@
|
|||||||
//: innerclasses/LambdaExpressions.java
|
//: innerclasses/LambdaExpressions.java
|
||||||
|
import static net.mindview.util.Print.*;
|
||||||
|
|
||||||
interface Description {
|
interface Description {
|
||||||
String brief();
|
String brief();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Body {
|
||||||
|
String detailed(String head);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Multi {
|
||||||
|
String twoArg(String head, Double d);
|
||||||
|
}
|
||||||
|
|
||||||
public class LambdaExpressions {
|
public class LambdaExpressions {
|
||||||
Description desc = new Description() {
|
Description desc = new Description() {
|
||||||
@Override
|
@Override
|
||||||
public String brief() {
|
public String brief() {
|
||||||
return "Short info";
|
return "Short info";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Description desc2 = () -> "Short info";
|
Description desc2 = () -> "Short info";
|
||||||
|
|
||||||
|
Body bod = (h) -> h + " More details";
|
||||||
|
Body bod2 = h -> h + " No Parens!";
|
||||||
|
|
||||||
|
Multi mult = (h, n) -> h + n;
|
||||||
|
// Parens are required with multiple args:
|
||||||
|
// Multi mult2 = h, n -> h + n; // Nope
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
LambdaExpressions le =
|
||||||
|
new LambdaExpressions();
|
||||||
|
print(le.desc.brief());
|
||||||
|
print(le.desc2.brief());
|
||||||
|
print(le.bod.detailed("Hi!"));
|
||||||
|
print(le.bod2.detailed("Oh!"));
|
||||||
|
print(le.mult.twoArg("Pi! ", 3.14159));
|
||||||
|
}
|
||||||
} ///:~
|
} ///:~
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
<jrun cls="Games" />
|
<jrun cls="Games" />
|
||||||
<jrun cls="GreenhouseController" arguments='5000' />
|
<jrun cls="GreenhouseController" arguments='5000' />
|
||||||
<jrun cls="InheritInner" />
|
<jrun cls="InheritInner" />
|
||||||
|
<jrun cls="LambdaExpressions" />
|
||||||
<jrun cls="LocalInnerClass" />
|
<jrun cls="LocalInnerClass" />
|
||||||
<jrun cls="innerclasses.MultiImplementation" dirpath="../innerclasses" />
|
<jrun cls="innerclasses.MultiImplementation" dirpath="../innerclasses" />
|
||||||
<jrun cls="innerclasses.MultiInterfaces" dirpath="../innerclasses" />
|
<jrun cls="innerclasses.MultiInterfaces" dirpath="../innerclasses" />
|
||||||
|
@ -34,7 +34,7 @@ public class AtUnit implements ProcessFiles.Strategy {
|
|||||||
if(!cName.contains("."))
|
if(!cName.contains("."))
|
||||||
return; // Ignore unpackaged classes
|
return; // Ignore unpackaged classes
|
||||||
testClass = Class.forName(cName);
|
testClass = Class.forName(cName);
|
||||||
} catch(Exception e) {
|
} catch(IOException | ClassNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
TestMethods testMethods = new TestMethods();
|
TestMethods testMethods = new TestMethods();
|
||||||
@ -86,7 +86,9 @@ public class AtUnit implements ProcessFiles.Strategy {
|
|||||||
}
|
}
|
||||||
if(cleanup != null)
|
if(cleanup != null)
|
||||||
cleanup.invoke(testObject, testObject);
|
cleanup.invoke(testObject, testObject);
|
||||||
} catch(Exception e) {
|
} catch(IllegalAccessException |
|
||||||
|
IllegalArgumentException |
|
||||||
|
InvocationTargetException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -137,14 +139,17 @@ public class AtUnit implements ProcessFiles.Strategy {
|
|||||||
if(creator != null) {
|
if(creator != null) {
|
||||||
try {
|
try {
|
||||||
return creator.invoke(testClass);
|
return creator.invoke(testClass);
|
||||||
} catch(Exception e) {
|
} catch(IllegalAccessException |
|
||||||
|
IllegalArgumentException |
|
||||||
|
InvocationTargetException e) {
|
||||||
throw new RuntimeException("Couldn't run " +
|
throw new RuntimeException("Couldn't run " +
|
||||||
"@TestObject (creator) method.");
|
"@TestObject (creator) method.");
|
||||||
}
|
}
|
||||||
} else { // Use the default constructor:
|
} else { // Use the default constructor:
|
||||||
try {
|
try {
|
||||||
return testClass.newInstance();
|
return testClass.newInstance();
|
||||||
} catch(Exception e) {
|
} catch(InstantiationException |
|
||||||
|
IllegalAccessException e) {
|
||||||
throw new RuntimeException("Couldn't create a " +
|
throw new RuntimeException("Couldn't create a " +
|
||||||
"test object. Try using a @TestObject method.");
|
"test object. Try using a @TestObject method.");
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ public class ClassNameFinder {
|
|||||||
int super_class = data.readShort();
|
int super_class = data.readShort();
|
||||||
return classNameTable.get(
|
return classNameTable.get(
|
||||||
offsetTable.get(this_class)).replace('/', '.');
|
offsetTable.get(this_class)).replace('/', '.');
|
||||||
} catch(Exception e) {
|
} catch(IOException | RuntimeException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,8 @@ public class BasicGenerator<T> implements Generator<T> {
|
|||||||
try {
|
try {
|
||||||
// Assumes type is a public class:
|
// Assumes type is a public class:
|
||||||
return type.newInstance();
|
return type.newInstance();
|
||||||
} catch(Exception e) {
|
} catch(InstantiationException |
|
||||||
|
IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,8 +41,7 @@ public class ProcessFiles {
|
|||||||
}
|
}
|
||||||
// Demonstration of how to use it:
|
// Demonstration of how to use it:
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new ProcessFiles((File file) -> {
|
new ProcessFiles(file -> System.out.println(file),
|
||||||
System.out.println(file);
|
"java").start(args);
|
||||||
}, "java").start(args);
|
|
||||||
}
|
}
|
||||||
} /* (Execute to see output) *///:~
|
} /* (Execute to see output) *///:~
|
||||||
|
@ -19,7 +19,8 @@ extends ArrayList<TaskItem<R,C>> {
|
|||||||
if(item.future.isDone()) {
|
if(item.future.isDone()) {
|
||||||
try {
|
try {
|
||||||
results.add(item.future.get());
|
results.add(item.future.get());
|
||||||
} catch(Exception e) {
|
} catch(InterruptedException |
|
||||||
|
ExecutionException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
items.remove();
|
items.remove();
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
// {TimeOutDuringTesting}
|
// {TimeOutDuringTesting}
|
||||||
// Tests the ChatterServer by starting multiple
|
// Tests the ChatterServer by starting multiple
|
||||||
// clients, each of which sends datagrams.
|
// clients, each of which sends datagrams.
|
||||||
import java.lang.Thread;
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ public class MultiSimpleClient {
|
|||||||
if(SimpleClientThread.threadCount()
|
if(SimpleClientThread.threadCount()
|
||||||
< MAX_THREADS)
|
< MAX_THREADS)
|
||||||
new SimpleClientThread(addr);
|
new SimpleClientThread(addr);
|
||||||
Thread.currentThread().sleep(100);
|
Thread.sleep(100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} ///:~
|
} ///:~
|
||||||
|
@ -14,8 +14,8 @@ abstract class Shape {
|
|||||||
public abstract void erase();
|
public abstract void erase();
|
||||||
static Shape factory(String type)
|
static Shape factory(String type)
|
||||||
throws BadShapeCreation {
|
throws BadShapeCreation {
|
||||||
if(type == "Circle") return new Circle();
|
if("Circle".equals(type)) return new Circle();
|
||||||
if(type == "Square") return new Square();
|
if("Square".equals(type)) return new Square();
|
||||||
throw new BadShapeCreation(type);
|
throw new BadShapeCreation(type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,9 @@ public class ParseTrash {
|
|||||||
}
|
}
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch(Exception e) {
|
} catch(NumberFormatException |
|
||||||
|
Trash.PrototypeNotFoundException |
|
||||||
|
Trash.CannotCreateTrashException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,12 @@ public abstract class Trash {
|
|||||||
// new object:
|
// new object:
|
||||||
return (Trash)ctor.newInstance(
|
return (Trash)ctor.newInstance(
|
||||||
new Object[]{info.data});
|
new Object[]{info.data});
|
||||||
} catch(Exception ex) {
|
} catch(NoSuchMethodException |
|
||||||
|
SecurityException |
|
||||||
|
InstantiationException |
|
||||||
|
IllegalAccessException |
|
||||||
|
IllegalArgumentException |
|
||||||
|
InvocationTargetException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
throw new CannotCreateTrashException();
|
throw new CannotCreateTrashException();
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ class StaticSub extends StaticSuper {
|
|||||||
public class StaticPolymorphism {
|
public class StaticPolymorphism {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
StaticSuper sup = new StaticSub(); // Upcast
|
StaticSuper sup = new StaticSub(); // Upcast
|
||||||
System.out.println(sup.staticGet());
|
System.out.println(StaticSuper.staticGet());
|
||||||
System.out.println(sup.dynamicGet());
|
System.out.println(sup.dynamicGet());
|
||||||
}
|
}
|
||||||
} /* Output:
|
} /* Output:
|
||||||
|
@ -123,8 +123,14 @@ public class CopyConstructor {
|
|||||||
Object obj = ct.newInstance(new Object[] { t });
|
Object obj = ct.newInstance(new Object[] { t });
|
||||||
System.out.println("In ripen2, t is a " +
|
System.out.println("In ripen2, t is a " +
|
||||||
obj.getClass().getName());
|
obj.getClass().getName());
|
||||||
|
} catch(NoSuchMethodException |
|
||||||
|
SecurityException |
|
||||||
|
InstantiationException |
|
||||||
|
IllegalAccessException |
|
||||||
|
IllegalArgumentException |
|
||||||
|
InvocationTargetException e) {
|
||||||
|
System.out.println(e);
|
||||||
}
|
}
|
||||||
catch(Exception e) { System.out.println(e); }
|
|
||||||
}
|
}
|
||||||
public static void slice2(Fruit f) {
|
public static void slice2(Fruit f) {
|
||||||
try {
|
try {
|
||||||
@ -133,8 +139,14 @@ public class CopyConstructor {
|
|||||||
Object obj = ct.newInstance(new Object[] { f });
|
Object obj = ct.newInstance(new Object[] { f });
|
||||||
System.out.println("In slice2, f is a " +
|
System.out.println("In slice2, f is a " +
|
||||||
obj.getClass().getName());
|
obj.getClass().getName());
|
||||||
|
} catch(NoSuchMethodException |
|
||||||
|
SecurityException |
|
||||||
|
InstantiationException |
|
||||||
|
IllegalAccessException |
|
||||||
|
IllegalArgumentException |
|
||||||
|
InvocationTargetException e) {
|
||||||
|
System.out.println(e);
|
||||||
}
|
}
|
||||||
catch(Exception e) { System.out.println(e); }
|
|
||||||
}
|
}
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Tomato tomato = new Tomato();
|
Tomato tomato = new Tomato();
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
// Uses remote object PerfectTime
|
// Uses remote object PerfectTime
|
||||||
// {RunByHand}
|
// {RunByHand}
|
||||||
package remote.ptime;
|
package remote.ptime;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.rmi.*;
|
import java.rmi.*;
|
||||||
import java.rmi.registry.*;
|
import java.rmi.registry.*;
|
||||||
|
|
||||||
@ -15,7 +16,9 @@ public class DisplayPerfectTime {
|
|||||||
for(int i = 0; i < 10; i++)
|
for(int i = 0; i < 10; i++)
|
||||||
System.out.println("Perfect time = " +
|
System.out.println("Perfect time = " +
|
||||||
t.getPerfectTime());
|
t.getPerfectTime());
|
||||||
} catch(Exception e) {
|
} catch(NotBoundException |
|
||||||
|
MalformedURLException |
|
||||||
|
RemoteException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,9 @@ public class PerfectTime
|
|||||||
Naming.bind(
|
Naming.bind(
|
||||||
"//MindviewToshibaLaptop:2005/PerfectTime", pt);
|
"//MindviewToshibaLaptop:2005/PerfectTime", pt);
|
||||||
System.out.println("Ready to do time");
|
System.out.println("Ready to do time");
|
||||||
} catch(Exception e) {
|
} catch(RemoteException |
|
||||||
|
AlreadyBoundException |
|
||||||
|
MalformedURLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,8 @@ public class FilledList<T> {
|
|||||||
try {
|
try {
|
||||||
for(int i = 0; i < nElements; i++)
|
for(int i = 0; i < nElements; i++)
|
||||||
result.add(type.newInstance());
|
result.add(type.newInstance());
|
||||||
} catch(Exception e) {
|
} catch(InstantiationException |
|
||||||
|
IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -11,9 +11,8 @@ public abstract class PetCreator {
|
|||||||
int n = rand.nextInt(types().size());
|
int n = rand.nextInt(types().size());
|
||||||
try {
|
try {
|
||||||
return types().get(n).newInstance();
|
return types().get(n).newInstance();
|
||||||
} catch(InstantiationException e) {
|
} catch(InstantiationException |
|
||||||
throw new RuntimeException(e);
|
IllegalAccessException e) {
|
||||||
} catch(IllegalAccessException e) {
|
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user