COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Type of a generic class? #4

• Email
• Follow

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

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 ;
}
}

+ Origin: Prism bbs (1:261/38)
Time Warp of the Future BBS - telnet://time.synchro.net:24

+ Origin: Prism bbs (1:261/38)
Time Warp of the Future BBS - telnet://time.synchro.net:24

* Origin: Prism bbs (1:261/38)
Time Warp of the Future BBS - telnet://time.synchro.net:24

 0

See related articles to this posting

  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.

+ Origin: Prism bbs (1:261/38)
Time Warp of the Future BBS - telnet://time.synchro.net:24

+ Origin: Prism bbs (1:261/38)
Time Warp of the Future BBS - telnet://time.synchro.net:24

* Origin: Prism bbs (1:261/38)
Time Warp of the Future BBS - telnet://time.synchro.net:24

 0

  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

--
Lew

+ Origin: Prism bbs (1:261/38)
Time Warp of the Future BBS - telnet://time.synchro.net:24

+ Origin: Prism bbs (1:261/38)
Time Warp of the Future BBS - telnet://time.synchro.net:24

* Origin: Prism bbs (1:261/38)
Time Warp of the Future BBS - telnet://time.synchro.net:24

 0

2 Replies
27 Views

Similar Articles

12/6/2013 9:12:24 AM
page loaded in 32542 ms. (0)

Similar Artilces:

abstract class #3 #2
Why can't we use abstract class in place of Interface? On 16 Apr., 14:00, ashutosh.singh.ku...@gmail.com wrote: > Why can't we use abstract class in place of Interface? Abstract class is a class. Class has only ONE superclass. Class can implement many interfaces. Is this you problem? Example: class A extends B implements C,D,E,F,(...),Z{ } A:class B:superclass C - Z: interfaces Janush http://kathorncity.no-ip.info/en/ ashutosh.singh.kumar@gmail.com wrote: > Why can't we use abstract class in place of Interface? We can, under the right circumstances. Please be more specific: What characteristics of abstract classes and interfaces are you having trouble with? -- Eric Sosman esosman@ieee-dot-org.invalid Because you can only extends 1 class. You use a abstract class when you fell that it would not make sense for an instance of that class to be created, but you want to add more than what you can provide with an interface. On Wed, 16 Apr 2008 05:00:18 -0700 (PDT), ashutosh.singh.kumar@gmail.com wrote, quoted or indirectly quoted someone who said : >Why can't we use abstract class in place of Interface? you can. For when to use each, see

generic microprocessor interface?
, and the wait signal needs to be either active low or active high. The MC68000 is a pretty strange beast, but it is more or less obsolete. Maybe the Coldfire has the same, never checked. > Thanks, > yingchunh@yahoo.com wrote: > I am told to design a generic microprocessor interface for a ASIC chip. > Since I am not familiar with what types/modes of microprocessor are > most popular, I don't know how generic I can reach. For a class? > I am aware of there are synchronous/asychronous modes, separate or mux > address/data modes. But I notice there are so many different families > and series. Would somebody point me some typical families of > Microprocessor or even its datasheet? If it's for a class, ask your instructor. If it's for a job, run away quick. On 2005-02-09, Jim Stewart <jstewart@jkmicro.com> wrote: > >> I am told to design a generic microprocessor interface for a >> ASIC chip. Since I am not familiar with what types/modes of >> microprocessor are most popular, I don't know how generic I >> can reach. > > For a class? > >> I am aware of there are synchronous

Quiz
http://www.autodesk.com/eng/etc/fakeorfoto/quiz.html

Brush
Hello, i created a brush. But it's only black. How can i use other colors? Thanks. Andreas Hi, On Sat, 03 Jul 2004 09:52:06 +0200, Andreas Nenninger said: > i created a brush. But it's only black. > How can i use other colors? When using the paint tools, the foreground colour should be taken as the brush colour. Cheers, Dave. -- David Neary, E-Mail: bolsh at gimp dot org Work e-mail: d dot neary at phenix dot fr CV: http://dneary.free.fr/CV/ Hi Andreas! Before saving your brush, be sure to flatten the image and convert it to grayscale. Pascal &q

ApplicationDispatcher.requestURI starting with two slashes
Hi, we have written a litter filter that rewrites incoming request, as well as by using a response Wrapper object that will rewrite the encodeUrl Methods so that outgoing urls will also be rewritten. So the code looks something like this: String rewriteURL = createRewriteURL(request); RequestDispatcher forwardDispatcher = request.getRequestDispatcher(rewriteURL); forwardDispatcher.forward(request, toRewriteResponse(request, new ResponseWrapperClass(request, response))); This code works perfectly under some context path, lets say "server:port/example/... However, when run on the context root "/", it fails. I also found a / or "the" bug, in the forwardDispatcher object, when debugging, the private requestURI attribute starts with two "/" slashes instead of just one. Example: "//MyPage.html" --------- request.getRequestDispatcher(rewriteURL) is handled internally by Tomcat code, but by manually adding some of it's source code, I was able to debug into the following lines of code that get called when request.getRequestDispatcher(rewriteURL) is called: public class ApplicationContext implements ServletContext

Thinking in Java 4th edition
Trying to solve this exercise: (Chapter Innerclasses, Exercise 6, page 353) Create an interface with at least one method, in its own package. Create a class in a separate package. Add a protected inner class that implements the interface. In a third package, inherit from your class and inside a method, return an object of the protected inner class, upcasting to the interface during the return. The following solution compiles and runs, but I had to use a public method to instantiate the protected class in the 2nd package. Is there a solution using the protected class directly? Shouldn't a child class have access to protected members, including inner class members, of parent class from another package. _____________________________ /* // in first package: * public interface Ex6Interface { * String say(); * } * * // and in second package: * public class Ex6Base { * protected class Ex6BaseInner implements Ex6Interface { * public String say() { return "Hi"; } * } * public Ex6BaseInner getEx6BaseInner() { * return new Ex6BaseInner(); * } * } */ // in third package: import innerclasses.ex6Interface.*; import innerclasses.ex6Base.*; public class Ex6 extends

comparator
Just want to experiment here and would like some help getting started. I want to use a collection/map of some type and change the comparator method (which might mean I have to change the equals method too?). Here's what I want to do. (Yes, I know it been done before and with better ideas.) I want collect my directory tree and sort by the directory name first. So, if I had these /usr/lab/alpha /usr/lab/beta /local/bin/alpha /local/bin/beta it would iterate /local/bin/alpha /usr/lab/alpha /local/bin/beta /usr/lab/beta Sorts the directory first, then a stable sort on the path second.

amsbook and spanish
Hi all, In my documnent with this preamble: \documentclass{amsbook} \usepackage[spanish]{babel} appears the word CAP�TULO (lower accented case letter i), instead of CAP�TULO (capital accented case letter i). I fix the problem changing in the file spanish.ldf \def\chaptername{Cap\'{\i}tulo}% by \def\chaptername{Cap\'{i}tulo}% How can I solve this problem without changing this file? Thanks in advance for any help Jos� A. Facenda "Jos� A. Facenda Aguirre" <facenda@us.es> wrote: > \usepackage[spanish]{babel} > appears the word CAP�TULO (lower accented case let

Extending Arrays
I wonder what would would happen if u would create a class extending an array. class CustomArray extends Object[ ] { } Chase Preuninger wrote: > I wonder what would would happen if u would create a class extending > an array. > > class CustomArray extends Object[ ] > { > } It would be never more useful than implementing List. class CustomArray implements List { } -- Andrea Francia http://www.andreafrancia.it/ i know, just wondering if it would work Chase Preuninger wrote: > i know, just wondering if it would work Probably you could override these two methods... quoted someone who said : >I wonder what would would happen if u would create a class extending >an array. You have two problems: the name of the class is no accessible in ordinary java. You would have to write byte code. The class is almost certainly final. Consider writing an HAS-A rather than IS-A implementation, the way ArrayList does. -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com I am not took good at reflection, but would it be possible the change the size of an array with reflection. On Thu, 17 Apr 2008 19:01:49 -0700 (PDT) Chase Preuninger