Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #17177 > unrolled thread

Type of a generic class?

Started by"Donkey Hottie" <donkey.hottie@1:261/38.remove-gby-this>
First post2012-08-04 19:43 +0000
Last post2012-08-04 19:44 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Type of a generic class? "Donkey Hottie" <donkey.hottie@1:261/38.remove-gby-this> - 2012-08-04 19:43 +0000
    Re: Type of a generic class? "markspace" <markspace@1:261/38.remove-gby-this> - 2012-08-04 19:43 +0000
    Re: Type of a generic class? "Lew" <lew@1:261/38.remove-gby-this> - 2012-08-04 19:44 +0000

#17177 — Type of a generic class?

From"Donkey Hottie" <donkey.hottie@1:261/38.remove-gby-this>
Date2012-08-04 19:43 +0000
SubjectType of a generic class?
Message-ID<501D725E.56136.calajapr@time.synchro.net>
From: "Donkey Hottie" <donkey.hottie@1:261/38.remove-5qr-this>

From: "Donkey Hottie" <donkey.hottie@1:261/38.remove-yy0-this>

From: Donkey Hottie <donkey@fredriksson.dy.fi>


I have this class called Global. It is trying to be a simplistic simulation of 
global as in MUMPS/M language. It is a persistent variable, that is accessible 
everywhere, and retains it's value over time. I store them in a database.

First problem I have is to translate the type to a lower level application API 
call. I can not leave the cast or type conversion to compiler only.

For that I figured out that I may need a variable of Class<T>, I'm using the 
variables isAssignableFrom(Class) to find out the correct API call. Could there 
be a simpler way?

the final Class<T> as a member variable. Is that really needed? How could I use 
some typeinfo (reflection API?) instead?

If I could use serialization and store the objects that way maybe into BLOBs 
there would not be problems, but currently I can not do that.

I would like to get rid of that "klass" argument for the Global<T>. Any ideas?

Class is a simple version containg only the important parts.

public class Global<T extends Object> {
    final String name ;
    final Connection conn ;
    final Class<T> klass;
    public Global(String name, Connection conn, Class<T> klass)
    {
        this.name = name ;
        this.conn = conn ;
        this.klass = klass;
    }

    @SuppressWarnings("unchecked")
    public T get() throws Exception
    {
        T rc = null;
        if (klass.isAssignableFrom(Boolean.class))
        {
            rc = (T)SystemProperties.getSystemBoolean(name, conn);
        }
        else if(klass.isAssignableFrom(Date.class))
        {
            rc = (T)SystemProperties.getSystemDate(name, conn);
        }
        else if (klass.isAssignableFrom(Long.class))
        {
            rc = (T)SystemProperties.getSystemLong(name, conn);
        }
        else if (klass.isAssignableFrom(Integer.class))
        {
            rc = (T)SystemProperties.getSystemInt(name, conn);
        }
        else if (klass.isAssignableFrom(String.class))
        {
            rc = (T)SystemProperties.getSystemString(name, conn);
        }
        return rc ;
    }
}

-+- BBBS/Li6 v4.10 Dada-1
 + Origin: Prism bbs (1:261/38)
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
 + Origin: Prism bbs (1:261/38)
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#17194

From"markspace" <markspace@1:261/38.remove-gby-this>
Date2012-08-04 19:43 +0000
Message-ID<501D7260.56153.calajapr@time.synchro.net>
In reply to#17177
  To: Donkey Hottie
From: "markspace" <markspace@1:261/38.remove-5qr-this>

  To: Donkey Hottie
From: "markspace" <markspace@1:261/38.remove-yy0-this>

  To: Donkey Hottie
From: markspace <-@.>

On 8/2/2012 12:04 PM, Donkey Hottie wrote:

> First problem I have is to translate the type to a lower level
> application API call. I can not leave the cast or type conversion to
> compiler only.


I'm going to ignore other obvious problems and simply focus on the big picture 
here.  You idea how to accomplish this looks *TERRIBLE*.  Why aren't you using 
some kind of ORM?  At least use a light-weight library for translating data 
base entities into objects.

<http://commons.apache.org/dbutils/>

Also JPA will do some kinds of automatic instantiation for you:

<http://openjpa.apache.org/builds/1.0.4/apache-openjpa-1.0.4/docs/manual/jpa_ov
erview_mapping_inher.html>

I didn't check to see if those are the most recent docs (I don't use this sort 
of feature).  Google for "JPA table inheritance" and check through the results 
carefully.


> Class is a simple version containg only the important parts.


Honestly, while we all appreciate the attempt, you example is far from 
complete.  We can guess at a few things, but you should think much more 
carefully at what the real problem is and design something to illustrate it.

-+- BBBS/Li6 v4.10 Dada-1
 + Origin: Prism bbs (1:261/38)
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
 + Origin: Prism bbs (1:261/38)
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#17196

From"Lew" <lew@1:261/38.remove-gby-this>
Date2012-08-04 19:44 +0000
Message-ID<501D7261.56154.calajapr@time.synchro.net>
In reply to#17177
  To: Donkey Hottie
From: "Lew" <lew@1:261/38.remove-5qr-this>

  To: Donkey Hottie
From: "Lew" <lew@1:261/38.remove-yy0-this>

  To: Donkey Hottie
From: Lew <lewbloch@gmail.com>

Donkey Hottie wrote:
> I have this class called Global. It is trying to be a simplistic
> simulation of global as in MUMPS/M language. It is a persistent
> variable, that is accessible everywhere, and retains it's value over
> time. I store them in a database.
>
> First problem I have is to translate the type to a lower level
> application API call. I can not leave the cast or type conversion to
> compiler only.
>
> For that I figured out that I may need a variable of Class<T>, I'm using
> the variables isAssignableFrom(Class) to find out the correct API call.

Not good.

> Could there be a simpler way?

Store a 'Class<T>' reference (matching the generic type) as a final variable. 
This is a "run-time type token" (RTTT).

> the final Class<T> as a member variable. Is that really needed? How

Yes.

> could I use some typeinfo (reflection API?) instead?

You mean a different reflection API. The 'Class' methods are part of 
reflection.

> If I could use serialization and store the objects that way maybe into
> BLOBs there would not be problems, but currently I can not do that.

How would a more complex, I/O-based solution be better?

> I would like to get rid of that "klass" argument for the Global<T>. Any
> ideas?

Why do you want to get rid of it?

It's the right way to do what you want.

> Class is a simple version containg only the important parts.
>
> public class Global<T extends Object>
> {
>     final String name ;
>     final Connection conn ;
>     final Class<T> klass;
>
>     public Global(String name, Connection conn, Class<T> klass)
>     {
>         this.name = name ;
>         this.conn = conn ;
>         this.klass = klass;
>     }
>
>     @SuppressWarnings("unchecked")

DON'T DO THAT!

You don't need it. If you did, you should comment why the expression is type 
safe despite the suppression.

And you should annotate the declaration of the variable, not the method.

>     public T get() throws Exception
>     {
>         T rc = null;
>
>         if (klass.isAssignableFrom(Boolean.class))

This is an antipattern.

>         {
>             rc = (T)SystemProperties.getSystemBoolean(name, conn);
>         }
>         else if(klass.isAssignableFrom(Date.class))
>         {
>             rc = (T)SystemProperties.getSystemDate(name, conn);
>         }
>         else if (klass.isAssignableFrom(Long.class))
>         {
>             rc = (T)SystemProperties.getSystemLong(name, conn);
>         }
>         else if (klass.isAssignableFrom(Integer.class))
>         {
>             rc = (T)SystemProperties.getSystemInt(name, conn);
>         }
>         else if (klass.isAssignableFrom(String.class))
>         {
>             rc = (T)SystemProperties.getSystemString(name, conn);
>         }
>         return rc ;
>     }
> }

You should override 'get()' in type-specific subtypes of your 'Global'. 
If-chains
of reflection are a reliable indicator of a bad architecture. Use polymorphism 
instead.

--
Lew

-+- BBBS/Li6 v4.10 Dada-1
 + Origin: Prism bbs (1:261/38)
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
 + Origin: Prism bbs (1:261/38)
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web