generic constructor call with concret type

P

Philipp Kraus

Hello,
I have th following proble:

abstract baseclass<T extends Number> {

abstract T dosomething();
}

class runclass<T extends Number> implements baseclass<T>{}

Now I would only implementate the runclass<T> for the datatype Double
and Float on other Number-types the compiler
should create an error.

At next I use a inner interface and class like

class runclass<T extends Number> implements baseclass<T>{

private interface innerintface<L extends Number>
{
public L doanother();
}

// and implement the interface with inner classes
private class floatrun implements innerintface<Float>
{
}

private class doublerun implements innerintface<Double>
{
}



// use a private member to store the object
private innerintface<T> obj = null

public runclass() {
// here is my problem to decide if I need create the obj = new
floatrun() or obj = new doublerun();
}

}

My runclass<T> should be only work with Double or Float datatypes at
the moment. In C++ template
I would create the conrect implement types and a genereal type that
creates an assert. Can I do anything
like
class runclass<T only Double>?

Thanks

Phil
 
R

Robert Klemme

Hello,
I have th following proble:

abstract baseclass<T extends Number> {

        abstract T dosomething();

}

class runclass<T extends Number> implements baseclass<T>{}

Now I would only implementate the runclass<T> for the datatype Double
and Float on other Number-types the compiler
should create an error.

At next I use a inner interface and class like

class runclass<T extends Number> implements baseclass<T>{

    private interface innerintface<L extends Number>
    {
          public L doanother();
    }

    // and implement the interface with inner classes
    private class floatrun implements innerintface<Float>
    {
    }

    private class doublerun implements innerintface<Double>
    {
    }

   // use a private member to store the object
   private innerintface<T> obj = null

   public runclass() {
       // here is my problem to decide if I need create the obj = new
floatrun() or obj = new doublerun();
  }

}

My runclass<T> should be only work with Double or Float datatypes at
the moment. In C++ template
I would create the conrect implement types and a genereal type that
creates an assert. Can I do anything
like
class runclass<T only Double>?

No, C++ templates != Java generics!

You could do this:

public class Run<T extends Number> implements Base<T> {

private interface Inner<T extends Number> {
T doAnother();
}

private static final class DoFloat implements Inner<Float> {
@Override
public Float doAnother() {
return 123f;
}
}

private static final class DoDouble implements Inner<Double> {
@Override
public Double doAnother() {
return 456d;
}
}

private final Inner<T> helper;

@SuppressWarnings("unchecked")
public Run(Class<? extends T> cl) {
if (cl == Float.class) {
helper = (Inner<T>) new DoFloat();
} else if (cl == Double.class) {
helper = (Inner<T>) new DoDouble();
} else {
throw new IllegalArgumentException("Invalid type: " + cl);
}
}

@Override
public T doSomething() {
return helper.doAnother();
}

// Test
public static void main(String[] args) {
final Base<Double> rd = new Run<Double>(Double.class);
final Base<Float> rf = new Run<Float>(Float.class);
System.out.println("Double: " + rd.doSomething());
System.out.println("Float: " + rf.doSomething());

final Base<Integer> ri = new Run<Integer>(Integer.class);
System.out.println("Integer: " + ri.doSomething());

// final Base<Integer> wontCompile = new Run<Integer>(Double.class);
}
}

Btw, what do you need that for?

Kind regards

robert
 
P

Philipp Kraus

@SuppressWarnings("unchecked")
public Run(Class<? extends T> cl) {
if (cl == Float.class) {
helper = (Inner<T>) new DoFloat();
} else if (cl == Double.class) {
helper = (Inner<T>) new DoDouble();
} else {
throw new IllegalArgumentException("Invalid type: " + cl);
}
}

@Override
public T doSomething() {
return helper.doAnother();
}

// Test
public static void main(String[] args) {
final Base<Double> rd = new Run<Double>(Double.class);
final Base<Float> rf = new Run<Float>(Float.class);
System.out.println("Double: " + rd.doSomething());
System.out.println("Float: " + rf.doSomething());

final Base<Integer> ri = new Run<Integer>(Integer.class);
System.out.println("Integer: " + ri.doSomething());

// final Base<Integer> wontCompile = new Run<Integer>(Double.class);
}
}

so I need a parameter for the type
Btw, what do you need that for?

See my posting some days ago. I use a JNI call with C++ templates so I
must set the template
parameter on compile-time. Within the JNI call I can't determine the
generic argument of the
instantiated java object (because it is set on java-compile-time). So I
create dual JNI calls one
for float and one for double, but I must decide on the java ctor call
if I instantiate the float or double
object, you do this in the with if(cl == Float.Class), in my source
code the DoFloat is the JNI call
for the C++ template float (otherwise the double).
 
R

Robert Klemme

@SuppressWarnings("unchecked")
public Run(Class<? extends T> cl) {
if (cl == Float.class) {
helper = (Inner<T>) new DoFloat();
} else if (cl == Double.class) {
helper = (Inner<T>) new DoDouble();
} else {
throw new IllegalArgumentException("Invalid type: " + cl);
}
}

@Override
public T doSomething() {
return helper.doAnother();
}

// Test
public static void main(String[] args) {
final Base<Double> rd = new Run<Double>(Double.class);
final Base<Float> rf = new Run<Float>(Float.class);
System.out.println("Double: " + rd.doSomething());
System.out.println("Float: " + rf.doSomething());

final Base<Integer> ri = new Run<Integer>(Integer.class);
System.out.println("Integer: " + ri.doSomething());

// final Base<Integer> wontCompile = new Run<Integer>(Double.class);
}
}

so I need a parameter for the type
Btw, what do you need that for?

See my posting some days ago. I use a JNI call with C++ templates so I
must set the template
parameter on compile-time. Within the JNI call I can't determine the
generic argument of the
instantiated java object (because it is set on java-compile-time). So I
create dual JNI calls one
for float and one for double, but I must decide on the java ctor call if
I instantiate the float or double
object, you do this in the with if(cl == Float.Class), in my source code
the DoFloat is the JNI call
for the C++ template float (otherwise the double).

A simpler solution would be

public abstract class Base<T extends Number> {
public static Base<Double> createDoubleHandler() { return new
RunDouble(); }
public static Base<Float> createFloatHandler() { ... }

public T anylogic(T x) {
//
return x == null ? null : specific(x);
}

protected abstract T specific(T abc);
}

final class RunDouble extends Base<Double> {
protected Double specific(Double x) {
return 123d + x;
}
}
....

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top