google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

Need an asArrayList for GWT #415

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Currently, asList returns a generic List, which is not so good for GWT RPC, 
since it generates lots of extra code for all the possible List 
implementations.  So, you can do this ugly hack:

Query<MyObject> q = mongoDS.createQuery(MyObject.class).filter("templateId = ", 
o.getId());
    List<MyObject> mylist = q.asList();
    if (logs instanceof ArrayList<?>) {
      return (ArrayList<MyObject>) mylist;
    } else {
      ArrayList<MyObject> l = new ArrayList<MyObject>();
      l.addAll(mylist);
      return l;
    }

Ideally, there should be a simple: asArrayList() that would return an 
ArrayList<MyObject> instead of a generic List<MyObject>

I know it's not ideal from an OO standpoint, but given that GWT is such a 
popular use-case for Morphia, I think it would make a lot of sense.

Original issue reported on code.google.com by eli...@espoc.com on 25 Jun 2012 at 3:28

GoogleCodeExporter commented 9 years ago
Wouldn't a utility method similar to the code you posted above suffice?

public static <E> ArrayList<E> toArrayList(List<E> list) {
  if (list instanceof ArrayList) {
    return (ArrayList<E>) list;
  } else {
    return new ArrayList<E>(list);
  }
}

Original comment by sebastia...@otto.de on 3 Jul 2012 at 8:27

GoogleCodeExporter commented 9 years ago
Yes.  But that relies on an instanceof ArrayList, which might end up doing a 
whole re-creation of the entire list.  I was hoping that an asArrayList() 
function would guarantee that the output would be an ArrayList and also 
guarantee that it wouldn't rebuild the entire list if later on Morphia decided 
to return LinkedLists or some other List from asList().

Original comment by eli...@espoc.com on 3 Jul 2012 at 8:31