How do I best implement a comparable parameterized class?

I am trying to implement a parameterized Wrapper class that I want to make Comparable, and the ordering is supposed to
be consistent with equals(). How can this be done? I'm kind of lost ...
My first approach looked like this:
class Wrapper<T> implements Comparable<Wrapper<T>> {
  private T theObject;
  public Wrapper(T arg) { theObject = arg; }
  public boolean equals(Object other) {
    if (this == other) return true;
    if (other == null) return false;
    if (getClass() != other.getClass()) return false;
    ... comparison of theObject based on T.equals() ...
  public int hashCode() { ... }
  public int compareTo(Wrapper<T> other) {
    if (this == other) return 0;
    if (other == null) throw new NullPointerException();
    ... comparison of theObject based on T.compareTo() ...
}This does not work, because I cannot invoke T.compareTo(), since T's bound it Object. Okay, it makes sense to perform
comparison only against wrappers around something that is comparable to T. Next try:
class Wrapper<T> implements Comparable<Wrapper<? extends Comparable<T>>> {
  public int compareTo(Wrapper<? extends Comparable<T>> other) {
    if (this == other) return 0;
    if (other == null) throw new NullPointerException();
    ... comparison of theObject based on T.compareTo() ...
}Now it compiles and I can compare a Wrapper<Integer> to a Wrapper<Integer>. But I cannot compare a Wrapper<Number> to
a Wrapper<Integer>, although the Wrapper<Number> may well contain a reference to an Integer object that is
comparable to the Integer in the other wrapper and the comparison is meaningful in such a case. Simply for reasons
of consistency with equals() I want to compare Wrapper<Integer> and Wrapper<Number>, but with the implementation above
I can't:
          Wrapper<Integer> p0 = new Wrapper<Integer>(10000);
          Wrapper<Number> p2 = new  Wrapper<Number>(10000);
          boolean equ = false;
          equ = p0.equals(p2);
          equ = p2.equals(p0);     
          int less = 0;
          less = p0.compareTo(p2);
          less = p2.compareTo(p0);The error messages say:
compareTo(Wrapper<? extends Comparable<Integer>>) in Wrapper<Integer> cannot be applied to (Wrapper<Number>)
          result = p0.compareTo(p2);
                           ^
compareTo(Wrapper<? extends Comparable<Number>>) in Wrapper<Number> cannot be applied to (Wrapper<Integer>)
          result = p2.compareTo(p0);
                           ^Okay, a Wrapper<Number> is not comparable to a Wrapper<Integer>. Obviously, the wildcard "? extends Comparable<T>" is
too restrictive. So I tried "? extends Comparable<?>", but then I cannot invoke T.compareTo(). Hence I use the raw
type Comparable:
class Wrapper<T> implements Comparable<Wrapper<? extends Comparable>> {
  public int compareTo(Wrapper<? extends Comparable> other) {
    if (this == other) return 0;
    if (other == null) throw new NullPointerException();
    ... comparison of theObject based on T.compareTo() ...
}Now, it is asymmetric; I can pass Wrapper<Integer> to Wrapper<Number>.compareTo(Wrapper<? extends Comparable>), but I
cannot pass Wrapper<Number> to Wrapper<Integer>.compareTo(Wrapper<? extends Comparable>). I see, Number is simply not
comparable to anything.
Is there a way to express that a Wrapper<T> shall be comparable to another Wrapper<U> where U is comparable to T or is
a supertype of something that is comparable to T? The wildcard Wrapper<? super Comparable<T>> isn't what I'm looking
for; it requires that Comparable<Integer> would be a subtype of Number, which it is not.
Eventually the best implementation I can come up with is this one:
class Wrapper<T> implements Comparable<Wrapper> {
  public int compareTo(Wrapper other) {
    if (this == other) return 0;
    if (other == null) throw new NullPointerException();
    ... comparison of theObject based on T.compareTo() plus a cast to Comparable<T> ...
}As soon as I try to be more specific and require that the wrapped object must be somehow comparable I cannot get
access to a comparable object through a non-comparable supertype any longer. I had somehow hoped that wildcards would
help expressing things like this, but that's a misconception, right?
Or does anybody have an idea how to express my intent in terms of wildcards or any other Java feature?

Have you tried the following declaration:
class Wrapper<T extends Comparable<T>> implements Comparable<Wrapper<T>> This should pass the restriction imposed on T upwards
to the super-interface.
Maybe that works as you expected?It's more restricted than I want because then all my wrappers would be wrappers around a comparable object and I cannot have a Wrapper<Number> any longer. Perhaps that's what I will resort to: a Wrapper, a ComparableWrapper, a CloneableWrapper, ...
>
BTW, what is this declaration of yours supposed to
mean:
class Wrapper<T> implements Comparable<Wrapper<? extends Comparable<T>>>
I think it is a wrapper that is comparable to another wrapper and that other wrapper contains an object that is comparable to T. It does not have to be a T, it suffices that it is comparable to T.
My gut feeling says that the use of wildcards in the
supertype of a derived class declaration should not be
legal, since it would render the supertype of
Wrapper<T> into a "family" of supertypes. Doesn't make
any sense to me, but if the compiler swallows it, then
it's probably just my limited understanding...The use of the wildcard in not in the supertype; it is in the type argument of the type argument of the supertype. A declaration such as ... implements Comparable<? extends Wrapper<T>> would be illegal, as far as I understand wildcards.

Similar Messages

  • ODI Supports ELT technology..How it is best when compared to ETL?

    In any Convensional ETL tool we are able to perform complex logics very easyly by using the componets inside the tool.
    I have not found any such components or transformations in ODI.
    But when I am reading the documents it says that ODI supports complex logics?
    Then How it is best when compared to any ETL tool in the market?

    Hi Harmeet,
    How are you?
    Seems like you are in fire...:-)
    Well, yes ODI is an E-LT tool and i guess this is one and only tool in market which follow E-LT architecture.
    Coming to comparison,
    An ETL tool needs three servers to move the source data to target means Source System, Transformation Engine and Data warehouse.( data is transformed twice in this approach)
    In E-LT, transformation engine/server and data warehouse server is combined as one and needs only 2 servers to do transforamtion. Because of this cost is very less and speed is very good.
    This is the one of the big advantage of using E-LT architecture.
    Experts comments are welcome...:-)
    Thanks,
    Guru

  • How to select multiple values from the Parameters in the concurrent program

    How to select multiple values from the Parameters defined in the concurrent program...and i believe multiple selection is not a direct feature of EBS, but is there any workaround solution to acheive mulitple selection?

    I think there's no way to do that using standard feature.
    Some workaround I use :
    1. If the number of selections are fixed, you could use multiple parameters for the same valueset. For example :
    Selection1 : <choose first selection>
    Selection2: <choose 2nd selection>
    ..etc.
    If you don't use it then leave it empty.
    2. Use text varchar valueset and enter it manually and separate by comma (or other value) , eg : selection1,selection2,selection3....etc.

  • How to export best quality gif animations in flash

    how to export best quality gif animations in flash

    GIF is a color restricted format with on/off support for transparency. There's really no settings you can tweak to output a better GIF. You have a drop-down during GIF sequence export to allow transparency, select the amount of colors (up to 256) and you can choose dither to save a little filespace. The only 2 things in there that really matter are dither and amount of colors used to export.
    If your content is very similar (e.g. not a lot of complex gradients/shadows/etc) and is limited in color use turning down the amount of colors used and dithering is your only option.
    Make sure you're exporting the GIF via "Publish Settings" so you get a single animated GIF and not file->export->movie because this creates a separate sequence of GIF images for each frame.

  • How can I pass more than one parameters in PDK-URL services?

    Hi all,
    How can I pass more than one parameters in PDK-URL service? All samples on Portal Center shows just one parameter passing.
    <inputParameter class="oracle.portal.provider.v1.URLPortletParameter">
    <name>csz</name>
    <isMandatory>false</isMandatory>
    <displayName>What location do you want a map for (City, State or Zip)?</displayName>
    </inputParameter>
    How can I write the privider.xml file for passing multiple parameters?

    I answer to my question. I've got the answer.
    I repeatly write down the <inputParameter> tags, and it works.

  • How to use the implementation class in propetty pallete

    Hi,
    I am using forms 10g....I have to insert horizontal scroll bar in text item..
    I have only class files instead of jar files ...how cani place the class file instead of jar file...
    How to use the implementation class in property palette to display the PJC
    Thanks,
    Ansaf.

    The Implementation Class must reflect the full name of the class. For instance, if the full class name is : xx.yyy.zz.class_name, then put this in the Implementation Class property of the corresponding item.
    Also, the class must be stored in the equivalent directory structure, so that, in my example: <DEV_HOME>/forms/java/xx/yyy/zz
    Francois

  • How is the best way to send data from local database into remote database ?

    Hi all,
    I need to send data from local Oracle database into remote oracle database. Both sites connected via 128 kbps frame relay connection.
    The sending need to be done once a week, can be done nightly.
    How is the best way to do this ?
    Thank you very much,
    xtanto

    How is the best way to do this ?How much are you planning to send?
    You can use COPY command. Ensure that you have valid database link between two databases.
    Available options are:
    create - creates a new table. errors out if the destination table exists.
    replace - drop the destination table and re-creates with data.
    insert - inserts data if the destination table exists.
    append– appends data into an existing table.
    use set arraysize 5000 -The arraysize specifies the number of rows that SQL*Plus will retrieve from the database at one time.
    copy from scott/tiger@ORCL to scott/tiger@ORCL92 create new_emp using select * from emp;

  • How is the best way to manage the stats table?

    Hello!
    I have the Integration 2.1 working with an Oracle 8.1.7 db. I noticed that the table
    STATS is growing pretty fast.
    How is the best way to manage this table?... I haven't found something related with
    this issue in the documentation, but at least I want to know how to safely delete
    records from this table.
    For example, if I know the minimal time I have to keep in the table, is quite simple
    to create a shell script and/or Oracle pl/sql job to trim the table.
    I hope somebody can help me!!!!
    Thank you!
    Ulises Sandoval

    Write an app people want to buy and rate highly.

  • How is the best way to remove something from a photo?

    How is the best way to remove something from a photo?

    This is difficult to answer without fully knowing what you are trying to do.
    That said, a few excellent and user friendly retouching tools include:  The Spot Heealing Brush Tool, Healing Brush Tool, Patch Tool, and the Cloning Stamp Tool.

  • How is the best way to backup and organize Adobe layered .PSD files on iMac OSX maverick?

    How is the best way to organize and backup Adobe layered PSD files on iMac desktop OSX maverick?
    I lost all my Adobe Photoshop files + everything else on my IMAC when it crashed. They reinstalled Operating System.  But now i must install programs such as MS Word & Adobe indesign suite.
    Please help me determine what is the bestway to backup Adobe layered  PSD files? I believe these are my choices to be assured this doesn't ever hapen again. Please comment.
    Buy Apple Airport Time capsule $280.  But I am not sure if it can store PSD layers. Can it or do you have to have Apple Apperture  software to act be sure layers are saved
    I had had trouble getting Adobe Cloud to sync to save files and so just trusted my computer to not ever crash.  Any hints on this procedure
    Are there any hints on PhotoShop image organization for a current IMac usser ?
    Please help.
    Teann  ucreateit

    My backup plan is to use Time Machine to make a backup to an external disk drive plus I also do a clone of my system disk. So far between the bootable clone and the Time Machine backup this plan has covered all of my needs.
    Allan

  • How is the best way to read data from an iphone if you lost your itunes data after a crash?

    How is the best way to read data from an iphone if you lost your itunes data after a crash?

    How is the best way to read data from an iphone if you lost your itunes data after a crash?

  • How is the best way to have Itunes notice your app or drive traffic to the app store?

    How is the best way to have Itunes notice your app or drive traffic to the app store? What are the best ways based on experiance that works to drive traffic to your app as a paid game for .99?

    Write an app people want to buy and rate highly.

  • My Macbook pro was stolen 09/12, the police just returned it to me. I want to remove all the data and start over. Format the drive's etc. I have windows 7 on 1 partion and mountain lion OSX on the apple partition. How is the best way to do this?

    My Macbook pro was stolen 09/12, the police just returned it to me. I want to remove all the data and start over. Format the drive's etc. I have windows 7 on 1 partion and mountain lion OSX on the apple partition. How is the best way to do this?

    Have a look here...
    what-to-do-when-your-hard-drive-is-full.html

  • I have an iphone4s and my two daughters have an iPod each. They both have used my apple id. How is the best way to change our their ids as I have been told we all need separate ids to be able to message each

    I have an iPhone and my two daughters both have iPods. They both use my apple Id and I would like to change their Id. . How is the best way to do this? My younger daughter had her iPod only last week and when she set it up, she ended up with all my phone contacts on Her iPod. If I change her apple Id will this remove the contacts from her iPod and leave mine intact on my phone?

    Anyone...anyone? Bueller...Bueller?

  • I need to install Internet Explorer 6 or greater on my iMac to work with a program.  How is the best way to do that?

    I need to install Internet Explorer 6 or greater on my iMac (intel processor) running MacOS 10.6.8 to work with a program.  How is the best way to accomplish this?

    There is no way.  IE has not been available for the Mac for years now.
    Your only option is to install native Windows and use that - either through a bootcamp dual-boot scenario, or use a virtual machine to run windows in (eg. Parallels or VMWare Fusion).  Either way, you will need a full retail install disk for Windows.
    P.S. the last version of IE for Mac was for PPC machines, and it was discontinued in 2003.

Maybe you are looking for