Removed two appendices
This commit is contained in:
parent
8b00b51c6f
commit
63e43bbe05
@ -1,2 +1,2 @@
|
||||
cp ..\..\OnJava-Tools\verify_output.py .
|
||||
python verify_output.py
|
||||
py -3 verify_output.py
|
||||
|
@ -79,10 +79,10 @@ Aquamarine
|
||||
Azure
|
||||
ten
|
||||
eleven
|
||||
------------------------------
|
||||
******************************
|
||||
Collections.max(c) = ten
|
||||
Collections.min(c) = AliceBlue
|
||||
------------------------------
|
||||
******************************
|
||||
AliceBlue
|
||||
AntiqueWhite
|
||||
Aquamarine
|
||||
@ -93,7 +93,7 @@ Brown
|
||||
BurlyWood
|
||||
CadetBlue
|
||||
Chartreuse
|
||||
------------------------------
|
||||
******************************
|
||||
AntiqueWhite
|
||||
Aquamarine
|
||||
Azure
|
||||
@ -103,13 +103,13 @@ Brown
|
||||
BurlyWood
|
||||
CadetBlue
|
||||
Chartreuse
|
||||
------------------------------
|
||||
******************************
|
||||
AntiqueWhite
|
||||
Aquamarine
|
||||
Azure
|
||||
ten
|
||||
eleven
|
||||
------------------------------
|
||||
******************************
|
||||
AntiqueWhite
|
||||
Aquamarine
|
||||
Azure
|
||||
@ -119,11 +119,11 @@ Brown
|
||||
BurlyWood
|
||||
CadetBlue
|
||||
Chartreuse
|
||||
------------------------------
|
||||
******************************
|
||||
c.contains(Azure) = true
|
||||
c.containsAll(c2) = true
|
||||
c2.isEmpty() = true
|
||||
------------------------------
|
||||
******************************
|
||||
PapayaWhip
|
||||
PeachPuff
|
||||
Peru
|
||||
|
@ -37,7 +37,7 @@ public class HTMLColorTest {
|
||||
0xFFF8DC: Cornsilk
|
||||
0xDC143C: Crimson
|
||||
0x00FFFF: Cyan
|
||||
------------------------------
|
||||
******************************
|
||||
AliceBlue 0xF0F8FF
|
||||
AntiqueWhite 0xFAEBD7
|
||||
Aquamarine 0x7FFFD4
|
||||
@ -58,7 +58,7 @@ CornflowerBlue 0x6495ED
|
||||
Cornsilk 0xFFF8DC
|
||||
Crimson 0xDC143C
|
||||
Cyan 0x00FFFF
|
||||
------------------------------
|
||||
******************************
|
||||
AliceBlue
|
||||
AntiqueWhite
|
||||
Aquamarine
|
||||
@ -79,7 +79,7 @@ CornflowerBlue
|
||||
Cornsilk
|
||||
Crimson
|
||||
Cyan
|
||||
------------------------------
|
||||
******************************
|
||||
0xF0F8FF
|
||||
0xFAEBD7
|
||||
0x7FFFD4
|
||||
|
@ -35,9 +35,9 @@ public class NavMap {
|
||||
}
|
||||
/* Output:
|
||||
0x000000: Black
|
||||
------------------------------
|
||||
******************************
|
||||
0xFFFFFF: White
|
||||
------------------------------
|
||||
******************************
|
||||
0x000000: Black
|
||||
0x000080: Navy
|
||||
0x00008B: DarkBlue
|
||||
@ -51,11 +51,11 @@ public class NavMap {
|
||||
0x00CED1: DarkTurquoise
|
||||
0x00FA9A: MediumSpringGreen
|
||||
0x00FF00: Lime
|
||||
------------------------------
|
||||
******************************
|
||||
0x00BFFF: DeepSkyBlue
|
||||
------------------------------
|
||||
******************************
|
||||
0x008B8B: DarkCyan
|
||||
------------------------------
|
||||
******************************
|
||||
0x00FF00: Lime
|
||||
0x00FA9A: MediumSpringGreen
|
||||
0x00CED1: DarkTurquoise
|
||||
@ -69,7 +69,7 @@ public class NavMap {
|
||||
0x00008B: DarkBlue
|
||||
0x000080: Navy
|
||||
0x000000: Black
|
||||
------------------------------
|
||||
******************************
|
||||
0xFFE4E1: MistyRose
|
||||
0xFFEBCD: BlanchedAlmond
|
||||
0xFFEFD5: PapayaWhip
|
||||
@ -83,7 +83,7 @@ public class NavMap {
|
||||
0xFFFFE0: LightYellow
|
||||
0xFFFFF0: Ivory
|
||||
0xFFFFFF: White
|
||||
------------------------------
|
||||
******************************
|
||||
0xDA70D6: Orchid
|
||||
0xDAA520: GoldenRod
|
||||
0xDB7093: PaleVioletRed
|
||||
|
@ -12,7 +12,7 @@ public class Meal {
|
||||
Food food = course.randomSelection();
|
||||
System.out.println(food);
|
||||
}
|
||||
System.out.println("---");
|
||||
System.out.println("***");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -21,25 +21,25 @@ SALAD
|
||||
VINDALOO
|
||||
GELATO
|
||||
CAPPUCCINO
|
||||
---
|
||||
***
|
||||
SPRING_ROLLS
|
||||
BURRITO
|
||||
GELATO
|
||||
HERB_TEA
|
||||
---
|
||||
***
|
||||
SPRING_ROLLS
|
||||
BURRITO
|
||||
TIRAMISU
|
||||
CAPPUCCINO
|
||||
---
|
||||
***
|
||||
SPRING_ROLLS
|
||||
BURRITO
|
||||
BLACK_FOREST_CAKE
|
||||
LATTE
|
||||
---
|
||||
***
|
||||
SALAD
|
||||
PAD_THAI
|
||||
GELATO
|
||||
TEA
|
||||
---
|
||||
***
|
||||
*/
|
||||
|
@ -41,7 +41,7 @@ public enum Meal2 {
|
||||
Food food = meal.randomSelection();
|
||||
System.out.println(food);
|
||||
}
|
||||
System.out.println("---");
|
||||
System.out.println("***");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -50,25 +50,25 @@ SALAD
|
||||
VINDALOO
|
||||
GELATO
|
||||
CAPPUCCINO
|
||||
---
|
||||
***
|
||||
SPRING_ROLLS
|
||||
BURRITO
|
||||
GELATO
|
||||
HERB_TEA
|
||||
---
|
||||
***
|
||||
SPRING_ROLLS
|
||||
BURRITO
|
||||
TIRAMISU
|
||||
CAPPUCCINO
|
||||
---
|
||||
***
|
||||
SPRING_ROLLS
|
||||
BURRITO
|
||||
BLACK_FOREST_CAKE
|
||||
LATTE
|
||||
---
|
||||
***
|
||||
SALAD
|
||||
PAD_THAI
|
||||
GELATO
|
||||
TEA
|
||||
---
|
||||
***
|
||||
*/
|
||||
|
@ -1,49 +0,0 @@
|
||||
// network/ChatterClient.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Starts multiple clients, each of which sends datagrams.
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
public class ChatterClient implements Runnable {
|
||||
private InetAddress host;
|
||||
private byte[] buf = new byte[1000];
|
||||
private DatagramPacket dp =
|
||||
new DatagramPacket(buf, buf.length);
|
||||
private static int counter = 0;
|
||||
private int id = counter++;
|
||||
|
||||
public ChatterClient(InetAddress host) {
|
||||
this.host = host;
|
||||
System.out.println(
|
||||
"ChatterClient #" + id + " starting");
|
||||
}
|
||||
public void sendAndEcho(String msg) {
|
||||
try (
|
||||
// Auto-assign port number:
|
||||
DatagramSocket s = new DatagramSocket();
|
||||
) {
|
||||
// Make and send a datagram:
|
||||
s.send(Dgram.toDatagram(
|
||||
msg, host, ChatterServer.INPORT));
|
||||
// Block until it echoes back:
|
||||
s.receive(dp);
|
||||
// Display the echoed contents:
|
||||
String rcvd = "Client #" + id +
|
||||
", rcvd from " +
|
||||
dp.getAddress() + ", " +
|
||||
dp.getPort() + ": " +
|
||||
Dgram.toString(dp);
|
||||
System.out.println(rcvd);
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
for(int i = 0; i < 10; i++)
|
||||
sendAndEcho(
|
||||
"Client #" + id + ", message #" + i);
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
// network/ChatterServer.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// A server that echoes datagrams
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
public class ChatterServer implements Runnable {
|
||||
static final int INPORT = 1711;
|
||||
private byte[] buf = new byte[1000];
|
||||
private DatagramPacket dp =
|
||||
new DatagramPacket(buf, buf.length);
|
||||
@Override
|
||||
public void run() {
|
||||
// Can listen & send on the same socket:
|
||||
try (
|
||||
DatagramSocket socket = new DatagramSocket(INPORT)
|
||||
) {
|
||||
System.out.println("Server started");
|
||||
while(true) {
|
||||
// Block until a datagram appears:
|
||||
socket.receive(dp);
|
||||
String rcvd = Dgram.toString(dp) +
|
||||
", from address: " + dp.getAddress() +
|
||||
", port: " + dp.getPort();
|
||||
System.out.println(rcvd);
|
||||
String echoString =
|
||||
"Echoed: " + rcvd;
|
||||
// Extract address and port from received
|
||||
// datagram and send it back there:
|
||||
DatagramPacket echo =
|
||||
Dgram.toDatagram(echoString,
|
||||
dp.getAddress(), dp.getPort());
|
||||
socket.send(echo);
|
||||
}
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
// network/Dgram.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Converts between Strings and DataGramPackets
|
||||
import java.net.*;
|
||||
|
||||
public class Dgram {
|
||||
public static DatagramPacket toDatagram(
|
||||
String s, InetAddress destIA, int destPort) {
|
||||
byte[] buf = s.getBytes();
|
||||
return new DatagramPacket(buf, buf.length,
|
||||
destIA, destPort);
|
||||
}
|
||||
public static String toString(DatagramPacket p) {
|
||||
return
|
||||
new String(p.getData(), 0, p.getLength());
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
// network/Local.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
import java.net.*;
|
||||
|
||||
public class Local {
|
||||
public static InetAddress host() {
|
||||
try {
|
||||
return InetAddress.getByName(null);
|
||||
} catch(UnknownHostException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
// network/LocalHost.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// The local loopback IP address
|
||||
import java.net.*;
|
||||
|
||||
public class LocalHost {
|
||||
public static void
|
||||
main(String[] args) throws Exception {
|
||||
InetAddress
|
||||
local1 = InetAddress.getByName(null),
|
||||
local2 = InetAddress.getByName("localhost"),
|
||||
local3 = InetAddress.getByName("127.0.0.1"),
|
||||
local4 = InetAddress.getLoopbackAddress();
|
||||
System.out.println(local1);
|
||||
System.out.println(local2);
|
||||
System.out.println(local3);
|
||||
System.out.println(local4);
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
localhost/127.0.0.1
|
||||
localhost/127.0.0.1
|
||||
/127.0.0.1
|
||||
localhost/127.0.0.1
|
||||
*/
|
@ -1,29 +0,0 @@
|
||||
// network/MultiServer.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Uses concurrency to handle any number of clients.
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class MultiServer implements Runnable {
|
||||
private final int port;
|
||||
public MultiServer(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Server: Running");
|
||||
try (
|
||||
ServerSocket ss = new ServerSocket(port)
|
||||
) {
|
||||
System.out.println("Server: " + ss);
|
||||
for(int i = 0; i < 10; i++)
|
||||
CompletableFuture
|
||||
.runAsync(new SimpleServer(ss));
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
// network/SimpleClient.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Sends to the server, reads from the server
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
|
||||
public class SimpleClient implements Runnable {
|
||||
public final int port;
|
||||
private static AtomicInteger idcount =
|
||||
new AtomicInteger(0);
|
||||
private final int id = idcount.getAndIncrement();
|
||||
private InetAddress host;
|
||||
public SimpleClient(InetAddress host, int port) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Client[" + id + "]: ";
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(this + "Running");
|
||||
try(
|
||||
Socket socket = new Socket(host, port);
|
||||
BufferedReader in =
|
||||
new BufferedReader(
|
||||
new InputStreamReader(
|
||||
socket.getInputStream()));
|
||||
PrintWriter out =
|
||||
new PrintWriter(
|
||||
new BufferedWriter(
|
||||
new OutputStreamWriter(
|
||||
// Enable auto-flush:
|
||||
socket.getOutputStream())), true);
|
||||
) {
|
||||
System.out.println(this.toString() + socket);
|
||||
for(int i = 0; i < 10; i ++) {
|
||||
out.println(this + "(*" + i + "*)");
|
||||
System.out.println(this +
|
||||
"Received '" + in.readLine() + "'");
|
||||
}
|
||||
out.println("END");
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
// network/SimpleServer.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Echoes what the client sends.
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
public class SimpleServer implements Runnable {
|
||||
private ServerSocket ss;
|
||||
public SimpleServer(ServerSocket ss) {
|
||||
this.ss = ss;
|
||||
}
|
||||
@Override
|
||||
public String toString() { return "Server: "; }
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(this + "Running");
|
||||
try (
|
||||
// Blocks until a connection occurs:
|
||||
Socket socket = ss.accept();
|
||||
BufferedReader in =
|
||||
new BufferedReader(
|
||||
new InputStreamReader(
|
||||
socket.getInputStream()));
|
||||
PrintWriter out =
|
||||
new PrintWriter(
|
||||
new BufferedWriter(
|
||||
new OutputStreamWriter(
|
||||
// Boolean enables auto-flush:
|
||||
socket.getOutputStream())), true)
|
||||
) {
|
||||
System.out.println(this.toString() + socket);
|
||||
in.lines().anyMatch(message -> {
|
||||
if(message.equals("END")) {
|
||||
System.out.println(this +
|
||||
"Received END. Closing Socket.");
|
||||
return true;
|
||||
}
|
||||
out.println(this + message);
|
||||
return false;
|
||||
});
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
// network/TestChatter.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
import java.util.concurrent.*;
|
||||
import java.net.*;
|
||||
import onjava.Nap;
|
||||
|
||||
public class TestChatter {
|
||||
public static void main(String[] args) {
|
||||
CompletableFuture.runAsync(
|
||||
new ChatterServer());
|
||||
CompletableFuture.runAsync(
|
||||
new ChatterClient(Local.host()));
|
||||
new Nap(1);
|
||||
// No exceptions means success
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
// network/TestMultiServer.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
import java.net.*;
|
||||
import java.util.concurrent.*;
|
||||
import onjava.Nap;
|
||||
|
||||
public class TestMultiServer {
|
||||
public static final int port = 8080;
|
||||
public static void main(String[] args) {
|
||||
CompletableFuture.runAsync(
|
||||
new MultiServer(port));
|
||||
new Nap(1); // Let the server get started
|
||||
for(int i = 0; i < 10; i++) {
|
||||
CompletableFuture.runAsync(
|
||||
new SimpleClient(Local.host(), port));
|
||||
}
|
||||
new Nap(4);
|
||||
// No exceptions mean success
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
// network/TestSimpleServer.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.concurrent.*;
|
||||
import onjava.Nap;
|
||||
|
||||
public class TestSimpleServer {
|
||||
// Choose a port outside of the range 1-1024:
|
||||
public static final int port = 8080;
|
||||
public static void main(String[] args) {
|
||||
try (
|
||||
ServerSocket ss =
|
||||
new ServerSocket(port)
|
||||
) {
|
||||
CompletableFuture.runAsync(
|
||||
new SimpleServer(ss));
|
||||
CompletableFuture.runAsync(
|
||||
new SimpleClient(Local.host(), port));
|
||||
new Nap(1);
|
||||
// Success if no exceptions happen
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
// network/WhoAmI.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Discovers your machine name and network address.
|
||||
import java.net.*;
|
||||
|
||||
public class WhoAmI {
|
||||
public static void
|
||||
main(String[] args) throws UnknownHostException {
|
||||
InetAddress whoami = InetAddress.getLocalHost();
|
||||
System.out.println(whoami.getHostName());
|
||||
System.out.println(whoami.getHostAddress());
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
MindviewToshibaLaptop
|
||||
192.168.0.104
|
||||
*/
|
@ -230,6 +230,7 @@ public class HTMLColors {
|
||||
showInv(m, m.size());
|
||||
}
|
||||
public static void border() {
|
||||
System.out.println("------------------------------");
|
||||
System.out.println(
|
||||
"******************************");
|
||||
}
|
||||
}
|
||||
|
58
patterns/SingletonPattern.java
Normal file
58
patterns/SingletonPattern.java
Normal file
@ -0,0 +1,58 @@
|
||||
// patterns/SingletonPattern.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
|
||||
interface Resource {
|
||||
int getValue();
|
||||
void setValue(int x);
|
||||
}
|
||||
|
||||
// Since this isn't inherited from a Cloneable
|
||||
// base class and cloneability isn't added,
|
||||
// making it final prevents cloneability from
|
||||
// being added through inheritance. This also
|
||||
// implements thread-safe lazy initialization:
|
||||
|
||||
final class Singleton {
|
||||
private static class
|
||||
ResourceImpl implements Resource {
|
||||
private int i;
|
||||
private ResourceImpl(int i) {
|
||||
this.i = i;
|
||||
}
|
||||
public synchronized int getValue() {
|
||||
return i;
|
||||
}
|
||||
public synchronized void setValue(int x) {
|
||||
i = x;
|
||||
}
|
||||
}
|
||||
private static class ResourceHolder {
|
||||
private static Resource resource =
|
||||
new ResourceImpl(47);
|
||||
}
|
||||
public static Resource getResource() {
|
||||
return ResourceHolder.resource;
|
||||
}
|
||||
}
|
||||
|
||||
public class SingletonPattern {
|
||||
public static void main(String[] args) {
|
||||
Resource r = Singleton.getResource();
|
||||
System.out.println(r.getValue());
|
||||
Resource s2 = Singleton.getResource();
|
||||
s2.setValue(9);
|
||||
System.out.println(r.getValue());
|
||||
try {
|
||||
// Can't do this: compile-time error.
|
||||
// Singleton s3 = (Singleton)s2.clone();
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
47
|
||||
9
|
||||
*/
|
57
references/LocalCopy.java
Normal file
57
references/LocalCopy.java
Normal file
@ -0,0 +1,57 @@
|
||||
// references/LocalCopy.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Creating local copies with clone()
|
||||
|
||||
class Duplo implements Cloneable {
|
||||
private int n;
|
||||
public Duplo(int n) { this.n = n; }
|
||||
@Override
|
||||
public Duplo clone() { // [1]
|
||||
try {
|
||||
return (Duplo)super.clone();
|
||||
} catch(CloneNotSupportedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
public int getValue() { return n; }
|
||||
public void setValue(int n) { this.n = n; }
|
||||
public void increment() { n++; }
|
||||
@Override
|
||||
public String toString() {
|
||||
return Integer.toString(n);
|
||||
}
|
||||
}
|
||||
|
||||
public class LocalCopy {
|
||||
public static Duplo g(Duplo v) {
|
||||
// Passing a reference, modifies outside object:
|
||||
v.increment();
|
||||
return v;
|
||||
}
|
||||
public static Duplo f(Duplo v) {
|
||||
v = v.clone(); // [2] Local copy
|
||||
v.increment();
|
||||
return v;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Duplo a = new Duplo(11);
|
||||
Duplo b = g(a);
|
||||
// Reference equivalence, not object equivalence:
|
||||
System.out.println("a == b: " + (a == b) +
|
||||
"\na = " + a + "\nb = " + b);
|
||||
Duplo c = new Duplo(47);
|
||||
Duplo d = f(c);
|
||||
System.out.println("c == d: " + (c == d) +
|
||||
"\nc = " + c + "\nd = " + d);
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
a == b: true
|
||||
a = 12
|
||||
b = 12
|
||||
c == d: false
|
||||
c = 47
|
||||
d = 48
|
||||
*/
|
@ -1,21 +0,0 @@
|
||||
// remote/DisplayPerfectTime.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Uses remote object PerfectTime
|
||||
// {ValidateByHand}
|
||||
package remote;
|
||||
import java.rmi.registry.*;
|
||||
|
||||
public class DisplayPerfectTime {
|
||||
public static void
|
||||
main(String[] args) throws Exception {
|
||||
Registry reg =
|
||||
LocateRegistry.getRegistry("localhost");
|
||||
PerfectTime pt =
|
||||
(PerfectTime)reg.lookup("PerfectTime");
|
||||
for(int i = 0; i < 10; i++)
|
||||
System.out.println(
|
||||
"Time: "+ pt.getPerfectTime());
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
// remote/PerfectTime.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// The PerfectTime remote interface
|
||||
package remote;
|
||||
import java.rmi.*;
|
||||
|
||||
public interface PerfectTime extends Remote {
|
||||
long getPerfectTime() throws RemoteException;
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
// remote/PerfectTimeImpl.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Implementing the PerfectTime remote object
|
||||
package remote;
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
public class PerfectTimeImpl implements PerfectTime {
|
||||
public long getPerfectTime() throws RemoteException {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
// remote/PerfectTimeServer.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Server for the PerfectTime remote object
|
||||
// {ValidateByHand}
|
||||
package remote;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
public class PerfectTimeServer {
|
||||
public static void
|
||||
main(String[] args) throws RemoteException {
|
||||
PerfectTimeImpl pt = new PerfectTimeImpl();
|
||||
PerfectTime stub = (PerfectTime)
|
||||
UnicastRemoteObject.exportObject(pt, 0);
|
||||
Registry registry = LocateRegistry.getRegistry();
|
||||
registry.rebind("PerfectTime", stub);
|
||||
System.out.println("Ready to do time");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user