Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #16972 > unrolled thread
| Started by | Donkey Hottie <donkey@fredriksson.dy.fi> |
|---|---|
| First post | 2012-08-02 22:04 +0300 |
| Last post | 2012-08-04 13:46 +0300 |
| Articles | 8 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
Type of a generic class? Donkey Hottie <donkey@fredriksson.dy.fi> - 2012-08-02 22:04 +0300
Re: Type of a generic class? Lew <lewbloch@gmail.com> - 2012-08-02 14:01 -0700
Re: Type of a generic class? markspace <-@.> - 2012-08-02 14:08 -0700
Re: Type of a generic class? Donkey Hottie <donkey@fredriksson.dy.fi> - 2012-08-03 02:27 +0300
Re: Type of a generic class? Lew <lewbloch@gmail.com> - 2012-08-02 16:51 -0700
Re: Type of a generic class? markspace <-@.> - 2012-08-02 20:08 -0700
Re: Type of a generic class? Donkey Hottie <donkey@fredriksson.dy.fi> - 2012-08-03 17:25 +0300
Re: Type of a generic class? Donkey Hottie <donkey@fredriksson.dy.fi> - 2012-08-04 13:46 +0300
| From | Donkey Hottie <donkey@fredriksson.dy.fi> |
|---|---|
| Date | 2012-08-02 22:04 +0300 |
| Subject | Type of a generic class? |
| Message-ID | <qivqe9-k9.ln1@whirlwind.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 ;
}
}
[toc] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-08-02 14:01 -0700 |
| Message-ID | <796b9a91-bdd7-45bd-9965-0b8ed640e51f@googlegroups.com> |
| In reply to | #16972 |
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
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-08-02 14:08 -0700 |
| Message-ID | <jveq7j$2mo$1@dont-email.me> |
| In reply to | #16972 |
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_overview_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.
[toc] | [prev] | [next] | [standalone]
| From | Donkey Hottie <donkey@fredriksson.dy.fi> |
|---|---|
| Date | 2012-08-03 02:27 +0300 |
| Message-ID | <j0fre9-61u.ln1@whirlwind.fredriksson.dy.fi> |
| In reply to | #17004 |
03.08.2012 00:08, markspace kirjoitti:
> 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.
>
I am not trying to translate database tables to objects. I have an ORM
for that.
I am trying to convert a simple { String name, String value } pair in
database to a Global<T>(name) so that it will convert the Java simple
datatypes to a string and back. This is for system parameters,
configuration parameters.
It is indeed kind of spooky and strange idea, after all what is so wrong
about simple
String value = SystemProperties.getString("ParamName") ;
It is OK, but I'm kind of toying with idea of strong typing of System
Parameters: A Date can not be stored as Long or Double etc..
The back end of the system parameters is simply
CREATE TABLE SystemParameters
(
name varchar(64) not null primary_key,
value varchar(512)
) ;
That's it. That is have the table is given to me. But I want to make the
Java code bit more pedantic on the types of the parameters it uses and
maintains.
This idea just occurred to me when I stubled against MUMPS language when
learning InterSystems Ensemble. It is based on their Caché NoSQL/SQL
hybrid database, and while being an ancient language, MUMPS/M has lots
of interesting features, including threading and database persistence in
the old Programming Language Core!
I just thought I might as well copy their Global variable idea and
include it in my toolbox of Java commons. It is not an ORM. I do not
know about dbutils, but JPA I am very familiar with. It maps Java
Objects to Relational Database records. There one attribute can have
just one type. I want many types to my value attribute, but only for
Java code. Just to force the apps to store at least correct type of
values for properties, so that compiler stops my fellow programmer if he
is about to generate a runtime error later somewhere, for example if he
is trying to store 'NOW' for a value that is needed as Date.
Not a big deal, but this is my idea here.
> <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_overview_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.
>
>
>
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-08-02 16:51 -0700 |
| Message-ID | <7828b4d7-9c03-484f-8853-7ab541c4395e@googlegroups.com> |
| In reply to | #17012 |
Donkey Hottie wrote:
> markspace kirjoitti:
>> ...
> I am trying to convert a simple { String name, String value } pair in
How about a Map<String, Global<?>>
Also, you should move that if-chain reflection out of your app and
use polymorphism instead.
> database to a Global<T>(name) so that it will convert the Java simple
> datatypes to a string and back. This is for system parameters,
> configuration parameters.
>
>> It is indeed kind of spooky and strange idea, after all what is so wrong
>> about simple
> String value = SystemProperties.getString("ParamName") ;
>
> It is OK, but I'm kind of toying with idea of strong typing of System
> Parameters: A Date can not be stored as Long or Double etc..
Using strings to look up types is not strong typing.
>
> The back end of the system parameters is simply
>
>
>
> CREATE TABLE SystemParameters
> (
> name varchar(64) not null primary_key,
> value varchar(512)
> ) ;
>
>
> That's it. That is have the table is given to me. But I want to make the
> Java code bit more pedantic on the types of the parameters it uses and
> maintains.
JPA does that for you, too.
Anyway, your use of reflection is anti-pedantic.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-08-02 20:08 -0700 |
| Message-ID | <jvffbg$mvl$1@dont-email.me> |
| In reply to | #17012 |
On 8/2/2012 4:27 PM, Donkey Hottie wrote:
> It is indeed kind of spooky and strange idea, after all what is so wrong
> about simple
>
> String value = SystemProperties.getString("ParamName") ;
>
> It is OK, but I'm kind of toying with idea of strong typing of System
> Parameters: A Date can not be stored as Long or Double etc..
To do something like this, I'd arrange some implementation for the
parameter:
class Parameter {
public void processParam( String param ) {
double d = Double.parse( param ); // or whatever...
...
}
}
And then use a Map as Lew suggests or some other method of binding the
Parameter class to the string "ParamName".
(You could, for example use Class.forName():
Parameter proc = Class.forName(
"my.project.parameter."+"ParamName").newInstance();
or you could use annotations:
@Parameter("ParamName")
class Parameter {...
or any other crazy thing you like. It would probably be better than
what you have now.)
> The back end of the system parameters is simply
>
> CREATE TABLE SystemParameters
> (
> name varchar(64) not null primary_key,
> value varchar(512)
> ) ;
I've written this table definition. In fact it was 23 odd years ago, as
in intern while still in college. I don't know if it's good or bad
practice, but it seems a common idea.
[toc] | [prev] | [next] | [standalone]
| From | Donkey Hottie <donkey@fredriksson.dy.fi> |
|---|---|
| Date | 2012-08-03 17:25 +0300 |
| Message-ID | <aj3te9-acn.ln1@whirlwind.fredriksson.dy.fi> |
| In reply to | #17023 |
03.08.2012 06:08, markspace kirjoitti: > >> The back end of the system parameters is simply >> >> CREATE TABLE SystemParameters >> ( >> name varchar(64) not null primary_key, >> value varchar(512) >> ) ; > > > I've written this table definition. In fact it was 23 odd years ago, as > in intern while still in college. I don't know if it's good or bad > practice, but it seems a common idea. > Not much worse that a property file common in Java. But a database table allows automatic parameters per a SaaS tenant.
[toc] | [prev] | [next] | [standalone]
| From | Donkey Hottie <donkey@fredriksson.dy.fi> |
|---|---|
| Date | 2012-08-04 13:46 +0300 |
| Message-ID | <l5bve9-s1b.ln1@whirlwind.fredriksson.dy.fi> |
| In reply to | #16972 |
02.08.2012 22:04, Donkey Hottie kirjoitti:
>
> 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.
Actually I renamed this to a SystemProperty, and will implement the
Global<T extends java.io.Serializable> later. It will act just like
MUMPS Global and does not need any clutterin oddities. Any serializable
objects just serialized to a BLOB.
I have a separate use case for that too.
public class Global<T extends java.io.Serializable>
{
private static final Log LOG = LogFactoryUtil.getLog(Global.class);
final String name;
final Connection conn;
public Global(String name, Connection conn)
{
this.name = name;
this.conn = conn;
}
public T get() throws Exception
{
T rc = null;
LOG.info(String.format("Returning Global value %s=%s ",
this.name, String.valueOf(rc)));
throw new Exception("Not yet implemented!");
}
public void set(T value) throws Exception
{
LOG.info(String.format("Setting Global value %s=%s ",
this.name, String.valueOf(value)));
throw new Exception("Not yet implemented!");
}
}
>
> 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 ;
> }
> }
>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web