Reputation: 28880
I have a design dilemma I want to share with you guys: I have a class with various member variables that represents a person. One of the members is the home location of the person in coordinates. This class is persisted in a MySQL database.
The issue I have is that I want to have a list of people that are within a certain radius from the recorded distance from the specified person.
I successfuly created an SQL query that returns a result set with the person's details, and its distance from the pin point. But now is the problem: what's the best way to save this data in Java? Saving the distance inside the person class's members is not good, because the distance is not relevant to the person object. I thought about creating a two dimenesional array which holds the person in the first column and the data in the other. Another option I thought is to create a container object with two values, the person and the distance.
What do you think is the most efficient and "object oriented way" of doing that?
Upvotes: 5
Views: 226
Reputation: 12700
You should create a separate object called Pin or Marker. Then you need a many-to-many table/object in the middle between the Person and Marker. It could be called PersonToPin and would have attributes person, pin and distance.
You can then have a Set called pins on the Person and a set called persons on the Pin.
Doing all this stuff yourself can be a real pain. I would recommend using something like Hibernate or another persistence library which allows you to concentrate on the Java objects and the library will take care of creating the tables and updating the database.
Upvotes: 0
Reputation: 596
Why not create a PinPoint class which has all the relevant variables and a data structure such as a simple array of the persons nearby populated by your query.
You could then put instances of your PinPoint object into what ever data structure you find most useful to the task at hand.
Upvotes: 0
Reputation: 124
You could have a PersonGeoInfo class which would hold a reference to a Person and to whatever other geographical information you see fit.
Upvotes: 0
Reputation: 44215
I would provide a connection class that holds the distance and person information.
class PinPoint
{
private double x; // Assuming
private double y;
public List<Connection> getConnections(double radius)
{
// Return a list of connections with the person and distance information
}
}
class Connection
{
private double distance;
private Person person;
}
That way you can add more things in the connection class later on, too. If you don't need it a simple map might suffice, too.
Upvotes: 2
Reputation: 1138
You could store the results of the search as an object containing all the Person
s and your pinpoint, and you'll be able to determine the distance after having gotten the result set.
Upvotes: 0
Reputation: 32949
I would suggest a Map<Person, Integer>
where the Person
is the key and the distance is the value.
Another option might be Guava's Table class: Table<Person, Point, Integer>
. This would be if you want to hold the distances to multiple points.
Upvotes: 1
Reputation: 12354
If you make an object that holds a Person, Address, and distance from the pinpoint (assuming it's fixed), you could implement Comparable on that object's distance. Then you could easily get your list of objects under a certain distance from the pinpoint.
Upvotes: -1