patternjavaMinor
Immutable Queue in Java using an Immutable Stack
Viewed 0 times
stackjavaimmutableusingqueue
Problem
I am new to this Immutability concept and I have some coding experience in JAVA.
Recently as a part of internship program, they gave me a 5-day task, which was to implement Immutable Queue. After some research I came with a solution which was given below.
Can some review the code.
[Technically your are not helping me in my internship process as it was over a while ago and I didn't got selected for the next round. So as per the rules of it there is no restriction on publishing the code after the process completion.]
ImmutableQueue.java
```
import java.util.NoSuchElementException;
public class ImmutableQueue {
private final class ImmutableEmptyQueue extends ImmutableQueue{ //sub class for Empty queue
public ImmutableEmptyQueue(){ //Constructor for the ImmutableEmptyQueue
super(null, null);
}
public ImmutableQueue enqueue(E e){ //Enqueue operation on EmptyQueue
if(e==null){
throw new IllegalArgumentException(); //if no argument passed throws exception
}
return new ImmutableQueue(forward , backward.push(e));
}
public ImmutableQueue dequeue(){ //Dequeue operation on EmptyQueue
throw new NoSuchElementException(); //throws exception
}
public E peek(){ //returns the first object
if(this.IsEmpty()){ //if empty throws exception
throw new IllegalArgumentException();
}
return null;
}
public int size(){ //return size zero
return 0;
}
public boolean IsEmpty(){ //Check if empty or not
return true;
}
}
private final Immutabl
Recently as a part of internship program, they gave me a 5-day task, which was to implement Immutable Queue. After some research I came with a solution which was given below.
Can some review the code.
[Technically your are not helping me in my internship process as it was over a while ago and I didn't got selected for the next round. So as per the rules of it there is no restriction on publishing the code after the process completion.]
ImmutableQueue.java
```
import java.util.NoSuchElementException;
public class ImmutableQueue {
private final class ImmutableEmptyQueue extends ImmutableQueue{ //sub class for Empty queue
public ImmutableEmptyQueue(){ //Constructor for the ImmutableEmptyQueue
super(null, null);
}
public ImmutableQueue enqueue(E e){ //Enqueue operation on EmptyQueue
if(e==null){
throw new IllegalArgumentException(); //if no argument passed throws exception
}
return new ImmutableQueue(forward , backward.push(e));
}
public ImmutableQueue dequeue(){ //Dequeue operation on EmptyQueue
throw new NoSuchElementException(); //throws exception
}
public E peek(){ //returns the first object
if(this.IsEmpty()){ //if empty throws exception
throw new IllegalArgumentException();
}
return null;
}
public int size(){ //return size zero
return 0;
}
public boolean IsEmpty(){ //Check if empty or not
return true;
}
}
private final Immutabl
Solution
-
This code assumes that all empty stacks are instances of ImmutableEmptyStack, but the public constructor allows code to create a new ImmutableStack that happens to be empty, so it would be better to use
-
It would be better to make the empty values static members, so you'd need to make the nested classes static too. This will save memory and also prevent code changes from accessing the surrounding
-
Java coding conventions say to start method names with a lower case letter, so
-
If you've made the
This code assumes that all empty stacks are instances of ImmutableEmptyStack, but the public constructor allows code to create a new ImmutableStack that happens to be empty, so it would be better to use
temp.IsEmpty()while(!(temp instanceof ImmutableStack.ImmutableEmptyStack)){-
It would be better to make the empty values static members, so you'd need to make the nested classes static too. This will save memory and also prevent code changes from accessing the surrounding
ImmutableQueue / ImmutableStack fields. It would be even better to put the empty classes each in their own file, i.e. not a nested class.private static final class ImmutableEmptyQueue extends ImmutableQueue{
private static final ImmutableEmptyQueue empty = new ImmutableEmptyQueue();
private final class ImmutableEmptyStack extends ImmutableStack {
private static final ImmutableEmptyStack empty = new ImmutableEmptyStack();-
Java coding conventions say to start method names with a lower case letter, so
isEmpty() instead of IsEmpty().-
If you've made the
empty values static, then you can even make the constructors private: then to use this class you always start with e.g. ImmutableQueue.empty and build it up using calls to enqueue(value).Code Snippets
while(!(temp instanceof ImmutableStack.ImmutableEmptyStack)){private static final class ImmutableEmptyQueue extends ImmutableQueue<E>{
private static final ImmutableEmptyQueue empty = new ImmutableEmptyQueue();
private final class ImmutableEmptyStack extends ImmutableStack<E> {
private static final ImmutableEmptyStack empty = new ImmutableEmptyStack();Context
StackExchange Code Review Q#78773, answer score: 5
Revisions (0)
No revisions yet.