• Post Reply Bookmark Topic Watch Topic
  • New Topic

cross-referenced Map?  RSS feed

 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anybody know if this idea has already been done?
I want to create a cross-referenced table, basically a table that holds one kind of bean and can be accessed and sorted by its properties.
I've been looking around, and haven't found an object like this that already exists, so I'm halfway to creating my own. So far, I've checked just apache's common collections, but I don't see anything like it there.
Any suggestions before I go and roll my own?
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Phil
I am just trying to see if I understand what you are trying to do-
I believe that we have to create multiple Comparator objects for each property sorting (we can come up with some abstract classes and all though to make the implementation clean/compact) so when we say "Sort beans by Property-X" it will give us the sorted list accordingly, right?
(Basically use Array.sort() you know...)
Also, I am not aware of any such objects and I think I have got little confused here by the term "cross-referencing". What is really "corss-referencing" here if we are expecting the behavior I mentioned in the above paragraph.
Regards
Maulin
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it's like a table you see in programs - ok, imagine a table with 3 rows and 4 columns, and each row is a Person bean.
the header is index#, name, age, and salary
the rows are
1, Joe, 30, 50000
2, Amy, 23, 40000
3, Bob, 40, 70000
I want to then be able to access the beans sorted by index, name, age, or salary. So if I did something like getData ("age"), it will return the beans in the order of Amy, Joe, Bob. If I want them by "name", then it will return Amy, Bob, Joe.
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I've got it working if anybody wants to see it...
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah okay...
So you can use Array.sort() method and create various Comparator object for each type of sorting...thats the scalable way I guess than to include a method in the Bean itself or something...
e.g.
class ByNameSort extends Comparator {
public int compare(Object o1, Object o2) {
String name1 = ((MyBean)o1).getName();
String name2 = ((MyBean)o2).getName();
return name1.compareTo(name2);
}
}
class ByAgeSort extends Comparator {
public int compare(Object o1, Object o2) {
int age1 = ((MyBean)o1).getAge();
int age2 = ((MyBean)o2).getAge();
return age1-age2;
}
}
and then you can use like,
MyBean[] myBeans; // assume you get it from somewhere...
if ( ChosenSortMethod.equals("ByName")) {
myBeans = Array.sort(myBeans,new ByNameSort());
} else if ( ChosenSortMethod.equals("ByAge") ) {
myBeans = Array.sort(myBeans, new ByAgeSort());
}
etc you know....
though this is very rough code and you will have to think more before coding..
Regards
Maulin
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the way I came up with it:
I call it a CrossReferencedList.
Setup:
A CrossReferencedList is constructed for a given Class.
For each field, you add a mapping of field->bean property.
For each bean property, you create a PropertyComparator.
For each PropertyComparator, you create an PropertyAccessor.
The PropertyAccessor is constructed with the Class and property name, and is responsible for finding a way to access it (via getter or if its a public field).
Data:
Add data, checking that each bean is of the right type.
Accessing:
Call a method like CrossReferencedList.getDataByField(fieldname) that uses Arrays.sort (data, comparator), and returns an array of ValueAndBean, which contains the value of the field and a link to the bean.

So this version is pretty flexible in that you can add/remove fields/data with no problems, since all sorts are generated when needed.
I'm working on a faster version now that is less flexible, where you have to add all fields, then add all data, and then generate the sorts - it's a higher one-time cost, takes more memory, but should be much faster overall
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!