Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to make Active Objects Comparable?

grundic
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 5, 2012

Hello.

I got bunch of active objects, which I request via my service. Then I would like to put them exactly in that order to map (as keys).

I tryed HashMap and LinkedHashMap - both throw me an exception

java.lang.ClassCastException: $Proxy1473 cannot be cast to java.lang.Comparable

I tryed to implement my entity's interface (@Implementation(OptionEntityImpl.class)) and add compareTo method, but it didn't helped.

How can I make my active objects Comparable?

Thanks in advance.

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
jhinch (Atlassian)
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 9, 2012

I would think that provided your Entity interface extends Comparable it would work. Something like this

@Implementation(OptionEntityImpl.class)
public interface OptionEntity extends Entity, Comparable<OptionEntity> {

}

class OptionEntityImpl implements Comparable<OptionEntity> {
  private final OptionEntity delegate;
  public OptionEntityImpl(OptionEntity delegate) {
    this.delegate = delegate;
  }
  public int compareTo(OptionEntity delegate) {
    // add compareTo implementation here
  }
}

Carl Myers
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 2, 2012

This doesn't seem to work for me - I don't understand how, if I implement that interface, it will make AO use my implementation - it uses dynamic proxies.

I tried having my interface implement comparable as well as entity and now I get NPEs from AO when it initializes:

Interface looks like:

public interface AttachmentInfo extends Entity, Comparable<AttachmentInfo> {

Exceptions look like:

java.lang.NullPointerException

at java.lang.Class.isAssignableFrom(Native Method)

at net.java.ao.types.TypeManager.getType(TypeManager.java:51)

at net.java.ao.types.TypeManager.getType(TypeManager.java:45)

at net.java.ao.Common.getValueFields(Common.java:486)

at net.java.ao.EntityManager.create(EntityManager.java:425)

at com.atlassian.activeobjects.internal.EntityManagedActiveObjects.create(EntityManagedActiveObjects.java:89)

at <Method where I try to create an object>

Carl Myers
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 2, 2012

FWIW, I figured out that I probably don't want that anyways. The objects didn't really have a natural sorting. I wanted to preserve the order I inserted them, but also deduplicate them (using set semantics). I don't think TreeSet is the right thing for that here, and I'm not aware of a data structure good for that, so I used two. I kept a list to preserve order I wanted, and a set to dedupe. I consulted the set first, to see if the object was "already had", then inserted it into both if it wasn't seen before, so the list kept order preserved.

TAGS
AUG Leaders

Atlassian Community Events