3 of 3
added 7 characters in body
Simon Forsberg
  • 58.9k
  • 9
  • 153
  • 307

It seems like what you are trying to accomplish here is to make your Queue class immutable. That in itself is good, but there are a couple of issues with your approach:

  • Creating a new class for this is not needed. There already exists classes for that. Use Collections.unmodifiableList to create one.
  • Your class does not implement the List interface (or the Queue interface for that matter). I guess there is a way to access the embedded List<T> such as getList hidden among "some constructors and helper functions", but if all that does is to return this.body; then your entire list is still accessible and modifiable.
  • The List<T> body field could be, and should be, declared final.

Instead of your entire Queue class, you might want to use this static method:

public static <E> List<E> addToList(List<E> oldList, E newElement) {
    List<E> temp = new ArrayList<E>(oldList);
    temp.add(newElement);
    return Collections.unmodifiableList(temp);
}

I should add however, that there is a difference between an unmodifiable list and what I understood about your approach. An unmodifiable list in Java does not allow any change to the list at all, such as list.set(index, obj).

Simon Forsberg
  • 58.9k
  • 9
  • 153
  • 307