How to write a method that takes two arguments of same type

Hi,
I seldom do anything "advanced" in generics, but I've done my reading, and thought that I had an understanding of the basics, but I'm stuck on this. How do I write a method that takes two arguments that must be of exact same type.
I've tried something like:
public class GenericsTest1 {
    public static <T> void compare(T first, T second) {
}But the compare method can in this case be called with e.g.
String arg1 = null;
Long arg2 = null;
compare(arg1, arg2);I can restrict the call to something like GenericsTest1.<Long>compare(arg1, arg2), but I don't want to do it in the location that calls the method, and the method can still be invoked with subclasses of the specified type. E.g. this is valid (but I don't want it to be valid):
Number arg1 = null;
Long arg2 = null;
GenericsTest1.<Number>compare(arg1, arg2);So I then changed the method into something like this:
public class GenericsTest2 {
    public static <T, E extends T> void compare(T first, E second) {
}It feels like I'm half way there now. Compare can't be called with e.g:
Long arg1 = null;
Number arg2 = null;
compare(arg1, arg2);But this is still valid:
Number arg1 = null;
Long arg2 = null;
compare(arg1, arg2);Hmm... So what to do? I only want the method to be callable when arg1 and arg2 is of exact same type. Everything else should give a compilation error.
Kaj
Ps. No, I don't have a need for this, I'm just curious.

dannyyates wrote:
I haven't gone through everything you've written, but I would expect that in the first instance, it's inferred <T> to be Object. This is a good reason to parameterise the class rather than the method wherever that makes sense.Thanks for you reply, but as I said this isn't related to anything that I'm going to use. I just want to know if it ca ben done with a static method and generics.
Kaj

Similar Messages

  • Bind ADF table to EJB method that takes a argument (operation)

    Hi Oracles
    I can bind a ADF table to a EJB method that takes no arguments, and then JDeveloper calls the method a accessor. I would like to bind another ADF table to another EJB method that takes a argument, but there I have some problems. First of all JDeveloper will not allow this, can I work around that? Then obviously I have to pass the argument to this EJB method some how..?
    Do you guys have any way of fixing this? Is the only way to not bind the ADF table, and do it in code in the background, and how do I do this?
    Best regards
    Søren

    Thank you!
    I found it, and if I drag it to my JSP, it gives me these options:
    - Create Methods
    - ADF Link
    - ADF Button
    - ADF Menu Item
    - ADF Toolbar Button
    - ADF Image Link
    - Create Parameters
    - ADF Parameter Form
    I have one ADF table that is bound to a data control, and based on the selection on this table, I would like to show data in another table based on a EJB method that takes a id from the selected object from the first table. Either on the same JSP or on another JSP, whatever is easiests.
    Can you help me do this? Then I hope I learn something in the process :)
    Best regards
    Søren

  • How to Call a method that takes in a parameter from JSF Fragment

    Hi,
    When we want to call the getter method of a backingbean field in a JSF Fragment we merely do a #{bean.fieldName} which will ensure that a getFieldName () will be invoked on the bean....and the value is returned.
    Now Suppose i have a method say
    String method1(String) and i wnt use this method in the JSFF. How woudl i do that because this method is taking a parameter so i cannot do a plain #{bean.fieldName}
    Any ideas
    Thanks
    Karthik

    Hello Karthik,
    You can achieve it using an EL hack. If the managed bean implements map, then an expression like #{bean[myParameter]} will call bean.get(myParameter) and you can then redispatch to your other method.
    Regards,
    ~ Simon

  • How to write logout method

    hi
    how to write logout method..... i am new to programming
    protected void logOut(){
    asap

    When I posted my first reply, I was thinking of writing to an output log. Do you mean "logout" like in web applications???
    You don't need to write any special code for that. At the most, you would want to clean up the resources like connections, sockets,... created by the application. If you have some mechanism which retains some "login" information, you might reset it to some default value.
    Why don't you be specific with your question instead of writing in such a vague manner?

  • Need to write a query that takes more than 2 minutes to execute

    Hi ,
    I am using Oracle 10g as my DataBase.
    I am writing a small program for Testing purpose , in that my requirement is to write a query that takes more than 2 minutes to execute .Right now i have only a small Table called as "Users" with very less data .
    Please let me know how can i achieve this thing ??
    Thanks .

    So please tell me , how can i achieve this . Thanks in advance .P. Forstman's example above will probably be more reliable, but here's an example of my idea - harder to control timing (untested)
    select count(*)
      from dba_objects o, dba_tables t, dba_tab_columns tc
    where o.object_name||'' = t.table_name||''
       and o.owner||'' = t.owner||''
      and t.table_name||'' = tc.table_name
      and t.owner||'' = tc.owner||''

  • How to code a method that returns string for class object

    I have a class named Address.
    public class Addresss{   
    private String street;   
    private String city;            // instance variables  
    private String zipcode;}I have been asked to write a method that returns a string for Address object. I dont understand how to create address object.Can anyone pls help me understand.
    do I have to write (for creating object)
    Address addressObject  = new Address(); ( coding method that returns a string for address object) ---> I really dont understand this part.

    looks almost right. The problem is that your method name AddressBookEntry does not match the name of your class AddressBook and therefore is not a constructor.
    Furthermore, you probably need to make a distinction between a single entry into the address book which consists of a single name and a single address, (which is just what you have done) and an address book itself which is probably a list of AddressBookEntries
    So for your code I would change the class that you called "AddressBook" to "AddressBookEntry" and have a third class that represents the collection of AddressBookEntries.
    Now to be honest, I don't know why you keep the name split apart from the street address portion of the address. I would be more inclined to keep the name as a field of the address itself, but it's your code, you carve it up the way you like.

  • Is it possible to create a component that takes two inputs?

    Hi Experts,
    Is it possible to create a custom JSF component that takes two input values and assigns them to a managed bean?
    For example, a component that displays two input boxes?
    The tag would look something like this:
    <my:component value1="#{bean.value1}" value2="#{bean.value2}" />I've tried, but I can't get the values to update the managed bean.
    I'm starting to doubt that it is possible, as their is only 1 setSubmittedValue method :(
    Thank you,
    Ristretto

    Hi Prosun Bondopadhyay  ,
                   Component controller is the base of a wda component. its can be considered as the base class. all the other views can be considered as the sub classes of component  controller. so without the base class there is no existence for sub class rt?
    like we can use all the methods and attributes of a component controller(main class) across all the view(sub class) and the viceversa is not possible.
    hence we cannot create a wda without component controller.
    Regards
    Sarath

  • IComparable (CompareTo takes one argument) and IComparer ( Compare takes two arguments)

    I was looking at the MSDN article
    https://support.microsoft.com/en-us/kb/320727 on IComparable and IComparer. IComparable provides default sort order and IComparer provides custom sort order.
    ICompareable interface requires that CompareTo method and takes one argument
    IComparer interface requires that Compare method be implemented and takes two arguments
    My question is : I can understand comparing two arguments, because we are comparing two objects / strings / int whatever the case be. But I don't understand IComparable Compare method which takes one argument, what is being compared with what? Below is the
    code from the article.
    int IComparable.CompareTo(object obj)
    car c = (car)obj;
    return String.Compare(this.make, c.make);
    The this instance of the class is compared with the object obj based on the make. I guess I don't understand where are the two entries picked up from and in what order. Because this means the current instantiated class holding the current entry, then
    what is the object? This is probably implementation detail on how the Sort works on the array. But I for some reason feel that the method with two arguments is intuitive as in IComparable Compare method

    When there is a comparison job, it can be performed in two ways. You ask the object itself (i.e. the comparable) for the result. It then compares itself with the compared object and gives the result. Another way is to employ a judge. You give him the two
    objects being compared together and ask him for the result.
    When implementing IComparable<T> in a class, you want the class itself give you the result. The advantage of this approach is that, you don't need another object to compare the result as a judge. Comparison is done implicitly. However, the limitation
    of this approach is that, you can implement the interface only once in a class.
    The second approach has more flexibility, because you can change the judge and employ any judge as you want to give you the proper result. However, you need to create an instance of the IComparer<T> and use it explicitly to perform the comparison.

  • How to call java method having array as argument from c++ ?

    Hello sir,
    how to call java method having array as arguments from c++;
    here is java code which is called from c++
    class PQR {
         public void xyz(int[] ia) {
         System.out.println("hi");
              for (int i = 0; i < ia.length; i++)
                   System.out.println(ia);
    suppose all jvm invocation is done...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    For someone well versed in java, C++ and JNI although tedious that should be obvious.
    For someone not well versed in all three it is going to be very difficult.
    Even for someone that does have knowledge in all of those areas coming up with a C++ interface that reflects that functionality in a dynamic way such that anyone is will to use it is going to be quite an adventure.
    At any rate to start building it you do exactly the same thing that you would in java.
    1. Extract everything in the jar via the zip package
    2. For each found instance extract all of the methods, return types, parameters, etc and build a description tree for each class.
    Doing all of that in C++ is going to take a LOT of code. If someone wanted an estimate from me it would take me 6 months to do it. And before I would even attempt it I would get them to explain to me in detail exactly how they thought they were going to use it when I was done because I can't see any reasonable way to do that.
    I left out the description tree itself. I suppose you could duplicate the entire reflection api in C++.
    Now perhaps if it was much, much more constrained, like to only those classes that implement a single interface then that would be more reasonable.

  • EDiscovery_SharePoint 2013_After a set of content is placed on hold in a location, how do I make sure that anything added to the same location

    Hi everyone,
    After a set of content is placed on hold in a location, how do I make sure that anything added to the same location,that meets the filter (e.g. keyword) gets added to the hold automatically ?
    Right now the only way I can do this is to take off the hold ,re-apply the filter to the source and then enable the hold. That isn't sustainable.

    @arcycapa,
    If you have a Mac, you can download iDentify2 (http://identify2.arrmihardies.com/). It is free. You can use that change or set your contentRating. If you are on Windows, there is a commercial tool called MetaX. or download a free command line tool called AtomicParsley from (https://bitbucket.org/wez/atomicparsley). Unzip the file and run this command (AtomicParsley "C:/Users/Home Videos/name of the file.mp4" --contentRating R) from the DOS prompt without the parenthesis. Please keep in mind the forward slash in the path name listed above. This would generate a temp file. You can rename the temp file to the original file name. Just a word of caution, please make a copy of your original file somewhere. Just in case.
    Shekhar.

  • How to write the method?

    How do i write the method to generate the following pattern...
    ++++++++++ 1. method name: drawPattern
    -+++++++++ 2. method takes an int as parameter that determines
    --++++++++      the pattern width/height
    ---+++++++ 3. method returns nothing
    ----++++++
    -----+++++
    ------++++
    -------+++
    --------++
    ---------+
    So far i got :
    private static void drawPattern (int width, int height) {
    what else should i include?
    Thanks!

    what else should i include?how about a method body???
    Sorry couldn't resist.
    Big hint: Look at for loops (I did it with 3).
    I would give you my code but this is a homework problem, so my conciousness prevents me from just giving you the solution.
    M

  • How to invoke AM method that accepts parameter other than string

    Hi
    I need to pass 2 date parameters to my AM method.
    I checked the jdev doc and found the below method that can be used for any of the parameter other than String
    public Serializable invokeMethod(String methodName,
    Serializable[] methodParams,
    Class[] methodParamTypes)
    one thing i am not able to understand is how to pass multiple dates in a single Class parameter.
    can anyone tell me the invoke method syntax for passing 2 dates.

    Hi,
    Suppose you have a string and two date parameters
    String test;
    Date date1;
    Date date2;
    then pass it like this
    OAApplicationModule am = pageContext.getApplicationModule(webBean);
    Serializable parameters[] = {test,date1,date2};
    Class paramTypes[] = {String.class,Date.class,Date.class};
    am.invokeMethod("initSummary", parameters, paramTypes);
    Thanks,
    Gaurav

  • How to write a program that runs on a port like a deamon or service?

    hi,
    how to write a program in java that runs on a port like a deamon or service, accepts requests from client, process the request and gives responce.
    for ex. tomcat runs on 8080 port as deamon or service.
    is it socket programming? if yes please give me a simple program which runs on a specific port.
    ex. a program running on a port talking two integers and return the total.
    thanks and regards,
    moses.

    I suggest you read
    [http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html]
    For more
    [http://www.google.co.uk/search?q=serversocket+tutorial]

  • Help with Creating a method that takes a string of arrays?

    i just begin my intro to programming and im a little stuck we had to write a program yesterday and i managed fine then today we had to work with arrays and i dont know much about arrays so im hoping someone here can point me in the right direction. This is what he wants *"Create a method called "printReceipts" that takes a String array of names (first names only) and prints out a message to customers using their name and telling them what customer number they are. The method should work with an array of any size."*
    Im not asking you to do it for me just i gues point out what methods i need to change thanks
    package edu.nmhu.cs245;
    public class Assignment1 {
         private String messagePrefix1 = "Hello ";
         private String messagePrefix2 = ", \n";
         public String[] getName() {
              String[] names = new String[] {"Lonny", "Gil", "Hosein"};
              return names;
         public String getCompleteMessage(String name, String message) {
              String completeMessage = messagePrefix1 + name + messagePrefix2 + message ;
              return completeMessage;
          * @param args
         public static void main(String[] args) {
              Assignment1 a = new Assignment1();
              String[] names = a.getName();
              for (int i = 0; i < names.length; i++) {
                   String printReciepts = a.getCompleteMessage(names,"You are customer number#" +(i+1));
                   System.out.println(printReciepts);
                   System.out.println();

    Focus! You should know what every single line of code is supposed to do! And they should all add up to a larger goal.
    Don't just put in stuff that seems like it's part of a relevant example.
         public String[] getName() {
              String[] names = new String[] {"Lonny", "Gil", "Hosein","Sam"};
              return names;
         }Are you supposed to do it that way? Or did it come with the assignment? It's (probably) fine for a homework assignment, but it's hardcoded data, which in practice one normally doesn't do.
              String completeMessage = messagePrefix1 + names + messagePrefix2 + messageSuffix ;Why do this? You're supposed to produce a receipt per use, not for all users at one.
              for (int i = 0; i < names.length; i++) OK, good. You could have used a foreach loop but this is fine.
                   String printReciepts = a.getCompleteMessage(names,"You are customer number#" +(i+1));What is "getCompleteMessage"?  You didn't define it.  Do you think that it is created implicitly because of the local String variable you created called "completeMessage"?  It doesn't.           String2 a = new String2();Don't call your class "String2".  It doesn't mean anything.  Name it after the assignment or something.           a.printReceipts(names);"names" isn't defined.  There's a local variable called "names" in the getNames method, but it doesn't exist when you call this and it's unavailable anyway.  You probably meant to do           a.printReceipts(a.getName());                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Possible to write a function that takes any type as parameter ?

    I need to write a function that will take 2 parameters ( of any type - VARCHAR etc ) and return a boolean.
    ( Im trying to write a function that will do NULL processing.
    something like..
    if ( P1=P2 OR ( P1 IS NULL AND P2 IS NULL ) ) then
    return true ;
    end if;
    return false ;
    the function simply compares its parameters P1 & P2 and returns true if they are equal - including if both are NULL
    Is this possible to write at all.
    My code looks very ugly without this being wrapped in a function.

    You may be able to use SYS.AnyData for this. However, dealing with the data would be confusing, so overloading the FUNCTION would most likely be a better idea.
    This doesn't work, but it looked like fun, so i tried:
    CREATE OR REPLACE FUNCTION Equal_Or_NULL(A SYS.AnyData, B SYS.AnyData)
    RETURN PLS_INTEGER
    AS
    Local_A     VARCHAR2(4000);
    Local_B     VARCHAR2(4000);
    Junk          PLS_INTEGER;
    BEGIN
    IF A IS NULL AND B IS NULL THEN RETURN 1; END IF;
    CASE A.GetTypeName
      WHEN 'SYS.NUMBER'     THEN Junk := A.GetNumber(Local_A); Junk := B.GetNumber(Local_B);
      WHEN 'SYS.DATE'     THEN Junk := A.GetDate(Local_A); Junk := B.GetDate(Local_B);
      WHEN 'SYS.CHAR'     THEN Junk := A.GetCHAR(Local_A); Junk := B.GetCHAR(Local_B);
      WHEN 'SYS.VARCHAR'     THEN Junk := A.GetVARCHAR(Local_A); Junk := B.GetVARCHAR(Local_B);
      WHEN 'SYS.VARCHAR2'     THEN Junk := A.GetVARCHAR2(Local_A); Junk := B.GetVARCHAR2(Local_B);
    END CASE;
    RETURN CASE WHEN Local_A = Local_B THEN 1 ELSE 0 END;
    END Equal_Or_NULL;
    /Overloading would be much simpler, as the arguments could be directly compared.
    IMO, Do not do this. Hiding logic in a FUNCTION it for cosmetic purposes is a bad idea.

Maybe you are looking for

  • If you have a Slingbox and an Xbox 360, this will solve 360 connection prob

    I got my TC on Wednesday of this week and starting hooking everything up. I had a PS3, Wii, Xbox 360, Printer, and Slingbox Pro that I wanted to hook up. Slingbox, PS3, Wii, and Printer all work fine, but the Xbox would not cooperate as many of you h

  • How to copy function group from one SAP system to another

    dear all, our company will set up a sub-company currently,and the sub-company want to copy some programs from our SAP system. how to pack and copy function group? pls help me,tks!

  • Won't boot to Win XP off HDD but will load

    Just built system, all new, Windows XP installed and activated after flashing mobo and updating all drivers.  Boot order in BIOS is CD-HDD-Floppy, have tried HDD-CD-Floppy, no difference.  HDD is sole drive master on IDE 1 (yes, the two opticals on I

  • Quicktime 7.1.2 wont go away

    HI. I too have fallen into the QT 7 trap - kicking myself now - as Ive been reading around for 2 days wading through the massive number of people who have also been snagged by this horrible update -with still no solution to my problem except an entir

  • Function start up error

    HI FRIENDS iam getting fucntion_startup_error (fucntion=ed^npp^excl) iam calling standard 'enter maintain form' using custom function ( calling standard form but with these parameters) => HR_CUSTOMIZATION="ED NPP EXCL" WORKFLOW_NAME="UK HR TASKFLOW"