In my game engine, objects are refered to as “references”, which are stored in a hashmap, and I’d like to give the option to make these weak. I was rather surprised when I noticed I couldn’t just put references (or weakreferences) into the hashmap instead of the objects themselves.
Because there is no situation where you would ever want or be able to create a Reference. If you read the docs (RTFM!) you would see why; it’s the first thing written in the class doc - the situation described is the main use for the abstract keyword in java.
OTOH, WR is not abstract.
But this is all irrelevant to HashMap’s. There is no reason why either won’t go in a HashMap, and in fact IIRC I put some in one just last night. I suspect you’re just not reading the compiler error message carefully enough…?
I suspect you’re not reading my post carefully enough.
[quote]I’d like to give the option to make these weak.
[/quote]
my first thought was something like this:
HashMap<String, Reference<blah>> table;
// setter
if(weak)
table.put(name, new WeakReference<blah>(value);
else
table.put(name, new Reference<blah>(value);
// getter
return table.get(name).get();
go figure.
[quote]Because there is no situation where you would ever want or be able to create a Reference.
[/quote]
now, if this isn’t one? no need to be so aggressive, man… >_>
[quote]I suspect you’re not reading my post carefully enough.
[/quote]
You are still not reading the docs :). I think you might be ignoring the word “abstract” wihtout asking yourself what it might mean?
Think of abstract as meaning “incomplete” - an abstract class is one that needs you to write some extra code to make it into a “real” class (a non-abstract one). If you read the docs, and read the first few sentences, you will see why: this is a “base class” that only contains SOME of the code needed to make various types of reference class; you need to implement the rest of the code to make an actual reference class.
It is impossible to instantiate (to call new blah()) on any abstract class; this is hard-coded into the core of the java language. You just cannot do it.
[quote]Think of abstract as meaning “incomplete” - an abstract class is one that needs you to write some extra code to make it into a “real” class (a non-abstract one). If you read the docs, and read the first few sentences, you will see why: this is a “base class” that only contains SOME of the code needed to make various types of reference class; you need to implement the rest of the code to make an actual reference class.
[/quote]
What makes you think I didn’t knew that? o-O
ok, I’ll reformulate the question:
“why is there no non-weak/soft/phantom reference class available?”
Um, because I’d pointed you at the docs, pointed out it was abstract, and you complained it still didn’t work, and seemed annoyed at me for telling you to read the docs. And because I wasn’t sure what in particular you meant by the title.
So, I guessed maybe you simply didn’t know what abstract meant.
[/quote]
Ah, I think I’m beggining to understand. You’re want to use the crap^H^H^H^H"advanced feature that we all love" in java 5 to make - in one/two lines of source - a generified data structure that either stores weak references or real ones?
Why not have two different classes, one which uses references, and one which does not. You can then wrap them in a class which does not use refs, and which instantiates one or the other depending upon it’s ctr paramters. It switches depending upon which mode it’s in, and has the type itself of being generified HM on the type you’re stroing, rather than on refs.
This may be more sensible in the long run, since you can modify each of the different classes separately, and they are likely to pick up new behaviours that are proprietary to each, since the different types of reference (or none at all) have very different usage semantics - i.e their usage is likely to diverge over time.
hadn’t I known what “abstract” was, I’d have complained about the errors when trying to instantiate it, rather than about it being abstract, no?
yes. this would be line of code if there was a non-weak/soft/phantom reference class available (and I still wonder, why isn’t there?? doesn’t make much sense to me…), but I guess I’ll have to workaround >_>
HashMap<String, Object> table;
// an enum, don't use j5, dont' know syntax off top of head
enum modes = [ NORMAL, WEAK, PHANTOM, GHOST ];
int mode;
// setter
switch( mode )
case modes.NORMAL:
table.put(name, value);
case modes.WEAK:
table.put(name, new WeakReference<blah>(value);
// getter
Object value = table.get(name);
switch( mode )
case modes.NORMAL:
return value;
default:
return ((Reference) value).get()
// default handles all Reference subclasses...
It’s extensible and lets you easily play with switching back and forth between different types (if that’s of any use).
Personally, I struggle to see why you would EVER want a DS that was either weak or not - you only ever use WR because you are doing something very specific which absolutely requires it.
I can think of cases where you might want it, but they all seem so contrived I can’t imagine they’re really going to come up. What are you doing that means you want to do this?
(maybe this is why there’s no “normal ref”: they couldn’t think of a situation where you’d want to switch between normal and weak?)
I’m developing a 2d fighting game engine which uses a state-type scripting language, which uses objects. Every instance (which consists of multiple components, like, spritesheet, animation, etc, etc) has a referencetable (-> hashmap<string, reference>), which is used to store the references for later access.
this works well with a single character, but, to make throw-like moves or other actions which influence other instances (-> characters) work, you may want to store the reference to the target. now, if some other instance prevents one of the objects from being unloaded this may become veeery memory-intensive. that’s why I want to give the option to make references weak.