开发者

Comparing objects

开发者 https://www.devze.com 2023-03-11 19:46 出处:网络
I have a form that let user enter values about address information. I want to compare the values I get from user, with information stored into ADDRESS table in the database.

I have a form that let user enter values about address information. I want to compare the values I get from user, with information stored into ADDRESS table in the database. I have an entity class

public class Address {
  private String gevernate;
  private int homeNo;
  private String neighborhood;
  private String street;
} 

which is represented as a table in the database called ADDR开发者_C百科ESS.

and I have a view object for this class which return all address values from db tabel

public static Address getAddress(Connection Con, long stdID) {
// select stamatment and result set object.
}

The problem I face is that the form may NOT contain all values of Address object, it may contains only 2 or three values, it's specified at run time. How can I compare two objects ?


add an equals method to the class like this:

public class Address {
    private String gevernate;
    private int homeNo;
    private String neighborhood;
    private String street;

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Address other = (Address) obj;
    if (gevernate == null) {
        if (other.gevernate != null)
            return false;
    } else if (!gevernate.equals(other.gevernate))
        return false;
    if (homeNo != other.homeNo)
        return false;
    if (neighborhood == null) {
        if (other.neighborhood != null)
            return false;
    } else if (!neighborhood.equals(other.neighborhood))
        return false;
    if (street == null) {
        if (other.street != null)
            return false;
    } else if (!street.equals(other.street))
        return false;
    return true;
}
}


Define/override equals() and hashcode() for the Address object. Compare as follows:

address1.equals(address2)


I would create an entirely separate class to do the comparisons, because they probably involve special logic and run-time configuration. As your application grows, it might involve some very complex stuff like matching states (NY versus New York), etc.

Do not use equals() for this, which should instead be whatever implementation makes sense for Java collections and general-purpose "is this object exactly the same or not" questions.

Basic idea:

int studentId = /* something */;
Connection conn = /* something */;
AddressForm form = /* something */;
Address userEnteredAddress = form.getEnteredAddress();
Address storedAddress = Address.getAddress(connection,studentId);
MyAddressComparer comp = new MyAddressComparer(form);
boolean similarEnough = comp.doMyVagueComparison(storedAddress,userEnteredAddress);

It's up to the code in AddressComparer to figure out what rules it needs to apply based on what the form was configured to do, and to do all those little special purpose tricks.


I'll just call this pseudocode, because I don't know Java:

if (this.gevernate != other.gevernate && this.gevernate != null && other.gevernate != null)
{
    // If both "gevernate" members are valid, but unequal...
    return false;
}

else if (this.homeNo != other.homeNo && this.homeNo != null && other.homeNo != null)
{
    // Repeat for "homeNo" values.
    return false;
}

// Repeat for the other two members.

else return true;
0

精彩评论

暂无评论...
验证码 换一张
取 消