A task deque accepts normal work at the tail and urgent work at the head, then removes the next pending item from the head.
Java task-deque project: command parsing, endpoint order and rejected work
Java 8+. This is a complete program using JDK classes.
Make each command a small boundary
The command grammar supports add, urgent, next and list. add and urgent require a nonempty task description; next and list take no argument. An unknown command or extra argument is rejected rather than silently interpreted as a task.
Two equal descriptions remain distinct items. This contract has no task identifiers or deduplication. Urgent work can delay earlier normal work indefinitely, so it is an ordering rule rather than a fairness guarantee.
The implementation depends on Deque<String> but stores a LinkedList. Swapping the implementation to ArrayDeque preserves these operations for the validated non-null input. A concurrent or durable queue needs different coordination and storage decisions.
Keep input ownership outside the queue
The demonstration executes a fixed sequence so it can be compiled and checked without waiting for stdin. A CLI loop can pass each complete line to execute and decide how to display rejected inputs. It should not close a shared System.in reader merely because one command completed.
Working program
import java.util.Deque;
import java.util.LinkedList;
public class TaskDequeCommands {
final Deque<String> pending=new LinkedList<>();
String execute(String line){
if(line==null||line.trim().isEmpty())throw new IllegalArgumentException("Missing command");
String[] parts=line.trim().split("\\s+",2);String command=parts[0];String argument=parts.length==2?parts[1].trim():"";
if(command.equals("add")||command.equals("urgent")){
if(argument.isEmpty())throw new IllegalArgumentException("Missing task");
if(command.equals("add"))pending.addLast(argument);else pending.addFirst(argument);
return "accepted";
}
if(!argument.isEmpty())throw new IllegalArgumentException("Unexpected argument");
if(command.equals("next")){String task=pending.pollFirst();return task==null?"empty":task;}
if(command.equals("list"))return pending.toString();
throw new IllegalArgumentException("Unknown command");
}
public static void main(String[] args){
TaskDequeCommands queue=new TaskDequeCommands();
queue.execute("add Audit stock");queue.execute("add Print label");queue.execute("urgent Verify address");
System.out.println(queue.execute("list"));System.out.println(queue.execute("next"));
System.out.println(queue.execute("list"));
try{queue.execute("add");}catch(IllegalArgumentException rejected){System.out.println("Empty task rejected");}
}
}Output
[Verify address, Audit stock, Print label]
Verify address
[Audit stock, Print label]
Empty task rejectedCosts and boundaries
Endpoint insertion and removal use O(1) linked-node work; parsing visits the command characters and list rendering visits O(n) tasks. The queue retains descriptions until removed. It is in memory only and has no bounded admission policy; the calling CLI must limit admitted work if input is not trusted to stay small.
Common Mistakes
- Do not promise persistence across restart.
- Urgent-at-head ordering can starve normal work.
- A Deque interface does not make a LinkedList thread-safe.
