0

I want to write a generic class that is a First-in-First-out queue with the basic Push and Pop operations, here is the class:

class queue<E>
{
protected final class elem
{
    public E val;
    public elem next=null;
    public elem(){}
    public elem(E v)
    {
        val=v;
    }
}
protected elem head=null,tail=null;
public queue()
{
}
public queue(E v)
{
    head=new elem(v);
}
public void push(E v)
{
    if(head==null)
        head=tail=new elem(v);
    else if(head==tail)
    {
        tail=new elem(v);
        head.next=tail;
    }
    else
    {
        elem t=new elem(v);
        tail.next=t;
        tail=t;
        t=null;
    }
}
public final E peek()
{
    return ((tail!=null)?tail.val:null);
}
public E pop()
{
    if(head==null)
        return null;
    E i=head.val;
    if(head!=tail)
        head=head.next;
    else
        head=tail=null;
    return i;
}
}

The problem is in the elem constructor here:

public elem(E v)
{
   val=v;
}

I don't want to assign v to val but I want to clone it(shallow copy).
here the things I tried:
1- the clone method: well because it's protected in Object class, I can't access it from an E variable.

2- queue<E extends Cloneable>: didn't solve the problem, actually Cloneable is an empty interface, it doesn't add any methods.

3- using the priorityqueue: that may be easier than writing a queue class on your own but I don't want priority, just Fifo structure.

4- implementing the queue interface: in which you must implement many methods that most of them I don't need, also I still have to clone by myself.

So, what to try next ?

niceman
  • 2,653
  • 29
  • 57
  • @Tala thanks , this is the first time i post code . – niceman Aug 27 '13 at 12:35
  • Meta-comment: it's not going to be easy to do this, because it's not idiomatic Java to make a copy in a situation like this. Why do you want a copy in the first place, rather than just storing the provided reference? – jacobm Aug 27 '13 at 14:24
  • storing reference may expose object attached to it for change and I don't want that. – niceman Dec 21 '14 at 22:36

1 Answers1

1

You can create interface CustomCloneable:

interface CustomCloneable {
    public CustomCloneable clone();
}

And use it in your situation. Note that you will have to provide clone implementation for your classes or use methods/libraries described below.

class Queue<E extends CustomCloneable>

And after that you can call clone method on your

public Elem(E v) {
    val = (E) v.clone();
}

On the other hand you might be looking for sth else. Refer here for other options and why you should avoid cloning.

Instead of that use some other options, like apache-commons [SerializationUtils][1] (deep-clone) or BeanUtils (shallow-clone), or simply use a copy-constructor.

Also please read java code conventions

Community
  • 1
  • 1
Tala
  • 8,888
  • 5
  • 34
  • 38
  • I can't find the clone method even after this change , am I missing something ? – niceman Aug 27 '13 at 12:42
  • this method is declared in Object class. See example : http://www.tutorialspoint.com/java/lang/object_clone.htm http://www.javadb.com/how-to-clone-objects http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/ – Tala Aug 27 '13 at 12:55
  • @Tala: `Object.clone()` is protected. That means it can only be called on `E` within `E` or a subclass of `E`. It cannot be called on `E` in `Elem`. – newacct Aug 27 '13 at 21:02