All Packages  Class Hierarchy  This Package  Previous  Next  Index

Interface jcsp.lang.ChannelInput

public interface ChannelInput

Description

ChannelInput defines the interface for reading from the Java version of occam Channels. The interface contains only one method read(), which reads from the Channel. Upon invoking the read() method the next Object from the Channel will be returned, if no data is currently in the Channel the method will block until data becomes available, this data will then be returned.

The ChannelInput interface should be used to hold references to Channels when they are only going to be used for inputting data from. By using the ChannelInput interface any attempt to write to the Channel using the reference to a ChannelInput type will generate a compile time error. The following code fragment would not compile.

 void doWrite(ChannelInput c, Object o) {
   c.write(o); // illegal
 
This interface should be used to pass Channels around where processes only require read access to the Channel.

The Object returned can be discarded or cast into the actual Class the reader process expects. If the reader can handle more than one type of Object (similar to tagged protocols in occam) if statements can be used to check the type of the Object before casting. If the Object returned is cast without checking the type of the Class first a ClassCastException will be thrown if the Object sent is of a different type.

Example

Discard data

 void doRead(ChannelInput c) {
   c.read();
 }
 

Cast data to expected type

 void doRead(ChannelInput c) {
   Boolean b = (Boolean)c.read(); // will cause a ClassCastException
                                  // if read does not return a Boolean
 }
 

Cast data after checking type

 void doRead(ChannelInput c) {
   Object o = c.read();
   if (o instanceof Boolean) {
     System.out.println("Boolean: " + (Boolean)o);
   }
   else if (o instanceof Integer) {
     System.out.println("Integer: " + (Integer)o);
   }
   else {
     System.out.println("Unexpected Class: " + o);
   }
 }
 

Author:
P.D.Austin

Method Index

 o read()
Reads an Object from the Channel.

Methods

 o read
 public abstract Object read()
Reads an Object from the Channel.

Returns:
The object returned from the Channel.

All Packages  Class Hierarchy  This Package  Previous  Next  Index