Generic method without arguments

Let's say I have a method like this:

static class Example { public static <N extends Number> Number getOddBits(N type) { if (type instanceof Byte) return (byte)0xAA; else if (type instanceof Short) return (short)0xAAAA; else if (type instanceof Integer) return 0xAAAAAAAA; else if (type instanceof Float) return Float.intBitsToFloat(0xAAAAAAAA); else if (type instanceof Long) return 0xAAAAAAAAAAAAAAAAL; else if (type instanceof Double) return Double.longBitsToDouble(0xAAAAAAAAAAAAAAAAL); throw new IllegalArgumentException(); } } 

The actual specificity of the method is not very important. However, to call this method, we use:

 Example.<Float>getOddBits(0f); 

My question is whether it is possible to write such a method without the usual parameters. Without overload, and ultimately without boxing.

Ideally called:

 Example.<Byte>getOddBits(); 
+7
source share
3 answers

As an improvement to the KennyTM proposal, you can combine the Class argument with generic methods to return a specialized type:

 @SuppressWarnings("unchecked") public static <N extends Number> N getOddBits(Class<N> cls) { Number out; if (cls == Byte.class) { out = (byte)0xAA; } else if (cls == Short.class) { out = (short)0xAAAA; } else if (cls == Integer.class) { out = 0xAAAAAAAA; } else if (cls == Float.class) { out = Float.intBitsToFloat(0xAAAAAAAA); } else if (cls == Long.class) { out = 0xAAAAAAAAAAAAAAAAL; } else if (cls == Double.class) { out = Double.longBitsToDouble(0xAAAAAAAAAAAAAAAAL); } else { throw new IllegalArgumentException(); } return (N)out; } 

This will allow you to assign the following and avoid casting to each call:

  float result = Example.getOddBits(Float.class); 
+1
source

How to just take .class ?

 public static Number getOddBits(Class<? extends Number> cls) { if (cls == Byte.class) { return (byte)0xAA; } else if (cls == Short.class) { return (short)0xAAAA; } else if (cls == Integer.class) { return 0xAAAAAAAA; } else if (cls == Float.class) { return Float.intBitsToFloat(0xAAAAAAAA); } else if (cls == Long.class) { return 0xAAAAAAAAAAAAAAAAL; } else if (cls == Double.class) { return Double.longBitsToDouble(0xAAAAAAAAAAAAAAAAL); } throw new IllegalArgumentException(); } ... Example.getOddBits(Float.class); 
+7
source

I'm not sure if this will help you, but I used it in the past to deserialize and return an object of the appropriate type.

 public <T> T deserialize(String xml){ T object=null; ... //pull type information from method param ... return object=(T)type.newInstance(); //helper to instantiate class } 

However, I'm not quite sure what you need to do. A simpler and clearer way to do this may be to create a converter interface for the data types you need and which classes should use it for implementation. Then what you need can be called directly on the object itself. eg:

 inerface Convertor<T>{ T convert(); void set(T value); } class Something implements Converter<Long,ByteArray>{ ... public ByteArray convert(){...} public void set(ByteArray value){...} } 
0
source

All Articles