Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #29263 > unrolled thread
| Started by | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| First post | 2014-07-24 10:03 +0200 |
| Last post | 2014-07-25 21:12 -0400 |
| Articles | 7 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
generics array type check Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-07-24 10:03 +0200
Re: generics array type check Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-07-24 09:13 -0400
Re: generics array type check markspace <markspace@nospam.nospam> - 2014-07-24 08:24 -0700
Re: generics array type check Arne Vajhøj <arne@vajhoej.dk> - 2014-07-25 21:14 -0400
Re: generics array type check Mike Amling <mamling@chaff.us> - 2014-07-25 12:15 -0500
Re: generics array type check Mike Amling <mamling@chaff.us> - 2014-07-25 13:21 -0500
Re: generics array type check Arne Vajhøj <arne@vajhoej.dk> - 2014-07-25 21:12 -0400
| From | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| Date | 2014-07-24 10:03 +0200 |
| Subject | generics array type check |
| Message-ID | <lqqekm$mtq$1@ariadne.rz.tu-clausthal.de> |
Hello,
I have got this signature of a method private static <T> long[]
getSize( T p_data )
Depend on the type T the return value is changes e.g. on a non-array type
getSize( 5 ) => new long[1]{1}
getSize( "test" ) => new long[1]{1}
on an array it should return
double[] p_array
getSize( p_array ) => new long[1]{ p_data.length }
and on 2-dimensional array it should be
double[][] p_array
getSize( p_array ) => new long[2]{ p_array.length, p_array[0].length }
How can I check the generic data type in a correct way to detect the
array type?
Thanks
Phil
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2014-07-24 09:13 -0400 |
| Message-ID | <lqr0p1$ost$1@dont-email.me> |
| In reply to | #29263 |
On 7/24/2014 4:03 AM, Philipp Kraus wrote:
> Hello,
>
> I have got this signature of a method private static <T> long[] getSize(
> T p_data )
> Depend on the type T the return value is changes e.g. on a non-array type
>
> getSize( 5 ) => new long[1]{1}
> getSize( "test" ) => new long[1]{1}
>
> on an array it should return
>
> double[] p_array
> getSize( p_array ) => new long[1]{ p_data.length }
>
> and on 2-dimensional array it should be
> double[][] p_array
> getSize( p_array ) => new long[2]{ p_array.length, p_array[0].length }
>
> How can I check the generic data type in a correct way to detect the
> array type?
You can't, as far as I'm aware.
However, you can use p_data.getClass().getComponentType() to
query the reference's actual (not generic) identity. If it returns
null the reference is not to an array; if it's non-null you apply
getComponentType() recursively to descend through additional levels.
If p_data is null I think you're out of luck.
--
esosman@comcast-dot-net.invalid
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2014-07-24 08:24 -0700 |
| Message-ID | <lqr8fc$hs0$1@dont-email.me> |
| In reply to | #29263 |
On 7/24/2014 1:03 AM, Philipp Kraus wrote:
> How can I check the generic data type in a correct way to detect the
> array type?
You can't. Use overloading instead.
public static void main()
{
size( "test" );
size( new String[]{ "test" } );
size( new String[][]{ { "test" } } );
}
private static void size( Object test )
{
}
private static void size( Object[] test )
{
}
private static void size( Object[][] test )
{
}
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2014-07-25 21:14 -0400 |
| Message-ID | <53d300f4$0$305$14726298@news.sunsite.dk> |
| In reply to | #29273 |
On 7/24/2014 11:24 AM, markspace wrote:
> On 7/24/2014 1:03 AM, Philipp Kraus wrote:
>> How can I check the generic data type in a correct way to detect the
>> array type?
>
> You can't. Use overloading instead.
>
>
> public static void main()
> {
> size( "test" );
> size( new String[]{ "test" } );
> size( new String[][]{ { "test" } } );
> }
>
> private static void size( Object test )
> {
> }
>
> private static void size( Object[] test )
> {
> }
>
> private static void size( Object[][] test )
> {
> }
If one is willing to use reflection then something
is possible.
Arne
[toc] | [prev] | [next] | [standalone]
| From | Mike Amling <mamling@chaff.us> |
|---|---|
| Date | 2014-07-25 12:15 -0500 |
| Message-ID | <c3fhltFanmaU1@mid.individual.net> |
| In reply to | #29263 |
On 7/24/14 3:03 AM, Philipp Kraus wrote:
> Hello,
>
> I have got this signature of a method private static <T> long[] getSize(
> T p_data )
> Depend on the type T the return value is changes e.g. on a non-array type
>
> getSize( 5 ) => new long[1]{1}
> getSize( "test" ) => new long[1]{1}
>
> on an array it should return
>
> double[] p_array
> getSize( p_array ) => new long[1]{ p_data.length }
>
> and on 2-dimensional array it should be
> double[][] p_array
> getSize( p_array ) => new long[2]{ p_array.length, p_array[0].length }
>
> How can I check the generic data type in a correct way to detect the
> array type?
How about something like
static long[] getSize(Object subject) {
if (subject==null) {
return new long[0]; // Or whatever
}
int dimensions=0;
long[] answer={1};
Class klas=subject.getClass();
while (klas.isArray()) {
final long[] more=new long[dimensions+1];
System.arraycopy(answer, 0, more, 0, dimensions);
answer=more;
klas=klas.getComponentType();
if (klas.isPrimitive()) {
if (klas==Byte.TYPE) {
answer[dimensions]=((byte[])subject).length;
} else if (klas==Short.TYPE) {
answer[dimensions]=((short[])subject).length;
...
} else if (klas==Double.TYPE) {
answer[dimensions]=((double[])subject).length;
}
break;
}
final Object[] aray=(Object[])subject;
answer[dimensions++]=aray.length;
subject=aray[0]; // Assumed non-null
}
return answer;
}
and to cover the primitives,
static long[] getSize(final double whatever) {
return new long[]{1};
}
--Mike Amling
QW5kIHRoZXkgc2FpZCBpdCBjb3VsZG4ndCBiZSBkb25lLg==
[toc] | [prev] | [next] | [standalone]
| From | Mike Amling <mamling@chaff.us> |
|---|---|
| Date | 2014-07-25 13:21 -0500 |
| Message-ID | <c3flgvFbhkdU1@mid.individual.net> |
| In reply to | #29319 |
On 7/25/14 12:15 PM, Mike Amling wrote:
>
> static long[] getSize(Object subject) {
> if (subject==null) {
> ...
Or, using the generic signature,
static <T> long[] getSize(T puppet) {
Object subject=puppet;
if (subject==null) {
...
--Mike Amling
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2014-07-25 21:12 -0400 |
| Message-ID | <53d30099$0$305$14726298@news.sunsite.dk> |
| In reply to | #29263 |
On 7/24/2014 4:03 AM, Philipp Kraus wrote:
> I have got this signature of a method private static <T> long[] getSize(
> T p_data )
> Depend on the type T the return value is changes e.g. on a non-array type
>
> getSize( 5 ) => new long[1]{1}
> getSize( "test" ) => new long[1]{1}
>
> on an array it should return
>
> double[] p_array
> getSize( p_array ) => new long[1]{ p_data.length }
>
> and on 2-dimensional array it should be
> double[][] p_array
> getSize( p_array ) => new long[2]{ p_array.length, p_array[0].length }
>
> How can I check the generic data type in a correct way to detect the
> array type?
I don't think the generic type information provide any benefits - you
are stuck with reflection.
For inspiration:
public static List<Integer> getDim(Object obj) {
List<Integer> res = new ArrayList<>();
Object currobj = obj;
Class<?> clz = obj.getClass();
while(clz != null) {
if(clz.isArray()) {
res.add(Array.getLength(currobj));
currobj = Array.get(currobj, 0);
}
clz = clz.getComponentType();
}
return res;
}
Note that I have made 2 changes:
- return List<Integer> instead of int[]
- return a zero length collection for non arrays to enable to
distinguish between non arrays and arrays with 1 element
Arne
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web