Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  why is "Reference" abstract??  (Read 1320 times)
0 Members and 1 Guest are viewing this topic.
Offline Valodim

Junior Member




*sigh*


« Posted 2005-03-26 22:45:48 »

*points to topic*

yeah, why?

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.

ideas/alternatives/opinions?
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #1 - Posted 2005-03-27 09:47:52 »

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...?

malloc will be first against the wall when the revolution comes...
Offline Valodim

Junior Member




*sigh*


« Reply #2 - Posted 2005-03-27 10:51:50 »

I suspect you're not reading my post carefully enough. Tongue

Quote
I'd like to give the option to make these weak.

my first thought was something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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.


now, if this isn't one? no need to be so aggressive, man... >_>
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #3 - Posted 2005-03-27 11:14:33 »

Quote
I suspect you're not reading my post carefully enough. Tongue


You are still not reading the docs Smiley. 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.

malloc will be first against the wall when the revolution comes...
Offline Valodim

Junior Member




*sigh*


« Reply #4 - Posted 2005-03-27 11:49:20 »

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.


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?"
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #5 - Posted 2005-03-27 12:50:20 »

Quote


What makes you think I didn't knew that? o-O



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

ok, I'll reformulate the question:

"why is there no non-weak/soft/phantom reference class available?"

[/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.

malloc will be first against the wall when the revolution comes...
Offline Valodim

Junior Member




*sigh*


« Reply #6 - Posted 2005-03-27 13:16:40 »

Quote

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.


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? Smiley

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?


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 >_>

like,
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
HashMap<String, Object> table; 
 
// setter

if(weak)
  table.put(name, new WeakReference<blah>(value);
else
  table.put(name, value);
 
// getter
Object value = table.get(name);
return (value instanceof Reference) ? ((Reference) value).get() : value;


I thought about this, but it seems somewhat not elegant to me :|
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #7 - Posted 2005-03-27 14:04:53 »

Quote

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
HashMap<String, Object> table; 
 
// setter

if(weak)
  table.put(name, new WeakReference<blah>(value);
else
  table.put(name, value);
 
// getter
Object value = table.get(name);
return (value instanceof Reference) ? ((Reference) value).get() : value;


I thought about this, but it seems somewhat not elegant to me :|


Instead, go for:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
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?)

malloc will be first against the wall when the revolution comes...
Offline Valodim

Junior Member




*sigh*


« Reply #8 - Posted 2005-03-28 14:29:09 »

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.

it's not exactly necessary but may come in handy...
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #9 - Posted 2005-03-28 16:34:48 »

That description was too brief I'm afraid; I can't understand why you'd want WR's there at all Huh

malloc will be first against the wall when the revolution comes...
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (24 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!