In: Computer Science
how do I write this function to check if it is close?
below is checking if the function is open. I want to write another if then statement to check when that function is closed and when the queue is empty. You can use any variables or declaration as a example to show me how it is done.
if (tx.isSenderStreamOpen())
{
}
/**
* Top level class available for the Client. In this example Main
class is the client
*/
import java.util.Queue;
import java.util.LinkedList;;
public class Transaction {
/**
* Data member of type SenderStream
*/
private SenderStream senderStream;
/**
* Opens the SenderStream with empty queue
*/
public void open(){
senderStream = new
SenderStream();
}
/**
* Opens the SenderStream with Queue elements
* @param queue
*/
public void open(Queue queue){
senderStream = new
SenderStream(queue);
}
/**
* Closes the SenderStream
*/
public void close(){
senderStream = null;
}
/**
* Checks whether a SenderStream is open or not
* @return true or false
*/
public boolean isSenderStreamOpen(){
if(senderStream!=null)
return
true;
return false;
}
/**
* Checks whether the queue of empty or not. If
SenderStream is closed, throws SenderStreamException
* @return true or false
* @throws SenderStreamException
*/
public boolean isSenderStreamEmpty() throws
SenderStreamException{
if(senderStream==null)
throw new
SenderStreamException("Sender Stream is closed.");
if(senderStream.getQueue().size()==0)
return
true;
return false;
}
}
/**
* SenderStream class with Queue
*
*/
class SenderStream{
/**
* This queue for the element
*/
private Queue queue;
/**
* Queue constructor
* @param queue
*/
public SenderStream(Queue queue) {
this.queue = queue;
}
/**
* Queue default constructor
*/
public SenderStream() {
this.queue = new
LinkedList<>();
}
/**
* Retrieve the SendStream Queue
* @return
*/
public Queue getQueue() {
return queue;
}
/**
* Sets the SendStream Queue
* @param queue
*/
public void setQueue(Queue queue) {
this.queue = queue;
}
}
/**
* Exception class for throwing ClosedSenderStream exception
*
*/
class SenderStreamException extends Exception{
public SenderStreamException(String message)
{
super(message);
}
}
//Main class
public class Main {
public static void main(String[] args) {
Transaction tx = new
Transaction();
//Without opening the
SenderStream
if(tx.isSenderStreamOpen())
System.out.println("Sender Stream is open");
else
System.out.println("Sender Stream is closed");
//Output => Sender Stream is
closed
try {
if(tx.isSenderStreamEmpty()){
System.out.println("Sender Stream is
empty");
}
} catch (SenderStreamException e1)
{
System.out.println("Sender Stream is closed");
e1.printStackTrace();
}
//Outptut => Sender Stream is
closed, followed by Exception
//Openning the Sender Stream
tx.open();
if(tx.isSenderStreamOpen())
System.out.println("Sender Stream is open");
else
System.out.println("Sender Stream is closed");
//Output => Sender Stream is
open
try {
if(tx.isSenderStreamEmpty()){
System.out.println("Sender Stream is
empty");
}
} catch (SenderStreamException e1)
{
System.out.println("Sender Stream is closed");
e1.printStackTrace();
}
//Output => Sender Stream is
empty
tx.close();
}
}
//Output
Sender Stream is closed
Sender Stream is closed
com.sandeep.SenderStreamException: Sender Stream is closed.
at
com.sandeep.Transaction.isSenderStreamEmpty(Transaction.java:54)
at
com.sandeep.TransactionMain.main(TransactionMain.java:16)
Sender Stream is open
Sender Stream is empty