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


Groups > comp.lang.java.programmer > #8904

generics puzzle

From blmblm@myrealbox.com <blmblm.myrealbox@gmail.com>
Newsgroups comp.lang.java.programmer
Subject generics puzzle
Date 2011-10-17 10:41 +0000
Organization None
Message-ID <9g2f24Fi0vU1@mid.individual.net> (permalink)

Show all headers | View raw


I'm having a bit of trouble with generics, and while I've come
up with ways of getting done what I want to get done, I wonder
whether there's some nice solution that isn't occurring to me.

The basic situation is this:

I have an abstract generic class (call it GThing) with type
parameter T, which has a method (call it set()) that takes a
parameter of type T and another method (call it modified()) that
returns a result of type T.  I also have some classes that extend
GThing and specify its type parameter, and code that is meant
to operate on lists of instances of these various subclasses.
What I would *like* to be able to do is construct a list of
GThing<?> objects and then do to each element of it something
like the following:

  element.set(element.modified())

but the compiler objects to that, which I find reasonable enough,
though I'm not enough of a generics expert to really articulate
what the problem is.  (I have a feeling that this "type erasure"
thing, which I understand dimly at best, comes into it.  :-)? )

One fix is to just introduce a method setFromModified() in GThing,
but that doesn't appeal to me.  An ugly-hack fix is to add to
GThing a set() method that takes an Object and typecasts it,
but -- yuck, right?  

In the same code I also have another generic class that "wraps"
GThing (call it GThingWrapper), and a class that among other
things contains a list of objects of the wrapper class (call
it GThingWrapperList).  One thing I want to do is add to that
list of objects a new GThingWrapper constructed from a GThing,
with the type parameter of the GThingWrapper matching the type
parameter of the GThing.  Initially that didn't work either,
which also seems reasonable enough, *but* I discovered (by trial
and error) a way to make it work using a generic method.  Now I'm
wondering whether somehow a generic method could be used to solve
the first problem, but I'm not finding a way to do it.

SSCCEs follow, one for the first situation (with a comment quoting
the error message I get from Oracle's "javac" compiler, version
1.6.0_21 if that matters), and one for the second.

I'd be glad of advice from anyone knowledgeable about generics
and willing to wade through my code ....

==== SSCCE #1 ====

import java.util.ArrayList;
import java.util.List;

public class GenericsProblem {

    public static void main(String[] args) { 
        List<GThing<?>> things = new ArrayList<GThing<?>>();
        things.add(new IntegerThing(1));
        things.add(new IntegerThing(2));
        things.add(new StringThing("hello"));
        things.add(new StringThing("bye"));
        for (GThing<?> t : things) {
            System.out.println(t.get());
        }
        for (GThing<?> t : things) {
            /* 
            t.set(t.modified());
               does not compile:
               set(capture#591 of ?) in 
               GenericsProblem.GThing<capture#591 of ?>
               cannot be applied to (java.lang.Object)
            */
        }
        for (GThing<?> t : things) {
            /* ugly hack!? */
            t.setG(t.modified());
        }
        for (GThing<?> t : things) {
            System.out.println(t.get());
        }
    }

    public static abstract class GThing<T> {
        private T val;
        protected GThing(T v) { val = v; }
        public T get() { return val; }
        public void set(T v) { val = v; }
        /* ugly hack!? */
        @SuppressWarnings("unchecked")
        public void setG(Object v) { val = (T) v; }
        abstract T modified();
    }

    public static class IntegerThing extends GThing<Integer> { 
        public IntegerThing(Integer i) { super(i); }
        @Override
        public Integer modified() { return get() * 2; }
    }
    public static class StringThing extends GThing<String> { 
        public StringThing(String s) { super(s); }
        @Override
        public String modified() { return get() + get(); }
    }
}


==== SSCCE #2 ====

import java.util.ArrayList;
import java.util.List;

public class GenericsProblemSoln {

    public static void main(String[] args) { 
        GThingWrapperList thingWrappers = new GThingWrapperList();
        thingWrappers.add(new IntegerThing(1));
        thingWrappers.add(new IntegerThing(2));
        thingWrappers.add(new StringThing("hello"));
        thingWrappers.add(new StringThing("bye"));
        for (GThingWrapper<?> tw : thingWrappers.elements()) {
            System.out.println(tw.getThing().get());
        }
        for (GThingWrapper<?> tw : thingWrappers.elements()) {
            tw.setModified();
        }
        for (GThingWrapper<?> tw : thingWrappers.elements()) {
            System.out.println(tw.getThing().get());
        }
    }

    public static abstract class GThing<T> {
        private T val;
        protected GThing(T v) { val = v; }
        public T get() { return val; }
        public void set(T v) { val = v; }
        abstract T modified();
    }

   public static class IntegerThing extends GThing<Integer> { 
        public IntegerThing(Integer i) { super(i); }
        @Override
        public Integer modified() { return get() * 2; }
    }
    public static class StringThing extends GThing<String> { 
        public StringThing(String s) { super(s); }
        @Override
        public String modified() { return get() + get(); }
    }

    public static class GThingWrapper<T> {
        private GThing<T> thing;
        public GThingWrapper(GThing<T> t) { thing = t; }
        public GThing getThing() { return thing; }
        public void setModified() {
            thing.set(thing.modified());
        }
    }

    public static class GThingWrapperList {
        private List<GThingWrapper<?>> thingWrappers = 
            new ArrayList<GThingWrapper<?>>();
        public <T> void add(GThing<T> t) {
            thingWrappers.add(new GThingWrapper<T>(t));
        }
        public Iterable<GThingWrapper<?>> elements() {
            return thingWrappers;
        }
    }
}


-- 
B. L. Massingill
ObDisclaimer:  I don't speak for my employers; they return the favor.

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar


Thread

generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-17 10:41 +0000
  Re: generics puzzle Steven Simpson <ss@domain.invalid> - 2011-10-17 13:14 +0100
    Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-17 15:14 +0100
    Re: generics puzzle markspace <-@.> - 2011-10-17 07:33 -0700
      Re: generics puzzle Steven Simpson <ss@domain.invalid> - 2011-10-17 16:26 +0100
        Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-18 14:48 +0000
      Re: generics puzzle Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-10-17 15:36 +0000
    Re: generics puzzle Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-17 08:58 -0700
      Re: generics puzzle Steven Simpson <ss@domain.invalid> - 2011-10-18 10:45 +0100
        Re: generics puzzle Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-18 09:42 -0700
          Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-19 13:25 +0000
            Re: generics puzzle Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-19 10:04 -0700
              Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-20 14:14 +0000
                Re: generics puzzle Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-20 11:11 -0700
  Re: generics puzzle Robert Klemme <shortcutter@googlemail.com> - 2011-10-17 09:12 -0700
    Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-18 14:49 +0000
      Re: generics puzzle Robert Klemme <shortcutter@googlemail.com> - 2011-10-18 18:27 +0200
        Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-18 17:45 +0000
          Re: generics puzzle Robert Klemme <shortcutter@googlemail.com> - 2011-10-18 22:15 +0200
          Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-18 18:59 -0700
            Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-19 13:28 +0000
              Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-20 17:21 -0700
                Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-21 16:27 +0000
                Re: generics puzzle Robert Klemme <shortcutter@googlemail.com> - 2011-10-21 20:34 +0200
                Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-22 18:50 +0000
                Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-22 21:02 +0100
                Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-25 07:04 +0000
                Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-25 23:25 -0700
                Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-26 21:56 +0100
                Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-27 08:59 +0000
                eclipse shortcuts again (was Re: generics puzzle) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-29 17:05 +0000
                Re: eclipse shortcuts again (was Re: generics puzzle) Four of Seventeen <fseventeen@gmail.com> - 2011-10-29 19:49 -0700
                Re: eclipse shortcuts again (was Re: generics puzzle) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-31 11:17 +0000
                Re: eclipse shortcuts again (was Re: generics puzzle) Four of Seventeen <fseventeen@gmail.com> - 2011-10-31 05:39 -0700
                Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-23 01:30 -0700
                Re: generics puzzle Lew <lewbloch@gmail.com> - 2011-10-23 08:56 -0700
                Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-24 02:46 -0700
                Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-25 07:05 +0000
                Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-25 23:29 -0700
                Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-31 11:14 +0000
                Re: generics puzzle Four of Seventeen <fseventeen@gmail.com> - 2011-10-31 05:34 -0700
          Re: generics puzzle markspace <-@.> - 2011-10-18 21:21 -0700
            Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-19 13:29 +0000
              Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-20 17:22 -0700
                Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-21 16:28 +0000
              Re: generics puzzle Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-10-21 06:22 -0300
                Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-21 16:29 +0000
                Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-23 01:20 -0700
                Re: generics puzzle Martin Gregorie <martin@address-in-sig.invalid> - 2011-10-23 09:51 +0000
                Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-23 03:28 -0700
                Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-23 15:59 +0100
                Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-24 02:46 -0700
                Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-23 15:55 +0100
            Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-20 21:00 +0100
          Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-19 13:26 +0000
          Re: generics puzzle Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-10-21 05:57 -0300
            Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-21 16:28 +0000

csiph-web