• Post Reply Bookmark Topic Watch Topic
  • New Topic

Backing into a many to one reference  RSS feed

 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, is there any quick way to back a new object into the place of an older object that is the focus of a many to one relationship? I'm thinking specifically of an array of objects that have been deserialized and all reference a single object. That single object travels with them and is reconstituted just as the array is. Now is there any easy way to substitute a new object of the same type for the old one without going through every object in the array and changing the reference to the new one?

I'v considered making the original object(the "old" one) as a wrapper that could be wrapped around the new object with all method calls to the original object passing through to the new one. I was just wondering if there was a way to do this without having to use the extra wrapper class.

Thanks for any ideas,
Chris
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really see what's so "writer-cramped" about just making a for loop like this:

and why do you want so many references ?
Maybe an ArrayList would help.
I'm not sure what your exact problem is, but I hope this helps
 
Allion Salvador
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOPS! Sorry, I didn't understand your question!
Here's the revised code snippet!
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Shepherd:

I'v considered making the original object(the "old" one) as a wrapper that could be wrapped around the new object with all method calls to the original object passing through to the new one.


That's the canonical solution. There's an old saying in computer science to the effect that there ain't no problem what can't be solved by addin' a layer of indirection
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input guys.

Allion:
The reason I was hoping to back the new object into place was that I have to be able to quickly pull in many serialized object arrays(from different files) that all should have pointers to the exact same object. I wanted to do this as quickly as possible and figured that backing an object in would be faster than looping through thousands or millions of records per file.

To be honest, I was hoping for a magic bullet like System.arrayCopy() is for dealing with arrays.

Guess I'm stuck with traditional methods. If EFH doesn't know a trick for something like this, I guess I'm out of luck.
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much of the time when people are trying to do something "as quickly as possible" they may be wasting time worrying about things that don't actually matter a whole lot. Have you tried writing the code to just replace all the references, and timing it to see how quick it actually is? As Allion noted, it's not very difficult to write - why not try it and see how well it works? If it's fast enough for you, problem solved. If not, you have a starting point for reference, so when you make changes in the future you can tell whether those changes made any difference or not - and how much.

OK, so you've got multiple serialized object files containing references to bjects which are supposed to be the same object, but they're not? That's consistent with the way serialization normally works. By default each file would contain its own copy of the object, and when deserialized you'd get a bunch of identical objects - which now you're trying to fix? I wonder if the thing to do here might be to add a readResolve() method to the class you're trying to replace values of. This gives you a mechanism to insert a reference to the correct object as you deserialize. It's hard to say if this will work for you though. We probably need more info about what you're doing.
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim. When I get to actually writing the code, I may try the replacement method just to see what the timeing's like. I will deffinately look into the readResolve() to see how it works.

I'm currently trying to noodle out how to make my data structure work fluidly when it has to deal with 5-6 layer data that has portions both in memory and saved out to multiple files. figuring this one issue out is about the last hump to get over. We wanted to be sure that we had a path that would work before we committed to changing tons of code.

In case you are curious, we are coming out from a database to work totally from memory and files as the data set gets bigger. When we originally started the program about 4-5 years ago, hardware constraints of the time forced us into a DB or we would have gone this route to begin with. Now we have a solid product out and we're looking at improvements and this was one that would have the most affect, albeit with the biggest code changes.

Thanks for your suggestions,
Chris
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if your wrapper code is too tedious to bear writing, there's alway dynamic proxy waiting to tart up your code:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!