Custom HashCode Builder : HashCodeBuilder « Apache Common « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Apache Common » HashCodeBuilder 
37.16.4.Custom HashCode BuilderPrevious/Next
/* 
 * ========================================================================
 
 * Copyright 2005 Tim O'Brien.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 
 *   http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 
 * ========================================================================
 */
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

class TaxReturn {

  private String ssn;

  private int year;

  private String lastName;

  private BigDecimal taxableIncome;

  public TaxReturn() {
  }

  public TaxReturn(String pSsn, int pYear, String pLastName, BigDecimal pTaxableIncome) {
    setSsn(pSsn);
    setYear(pYear);
    setLastName(pLastName);
    setTaxableIncome(pTaxableIncome);
  }

  public String getSsn() {
    return ssn;
  }

  public void setSsn(String pSsn) {
    ssn = pSsn;
  }

  public int getYear() {
    return year;
  }

  public void setYear(int pYear) {
    year = pYear;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String pLastName) {
    lastName = pLastName;
  }

  public BigDecimal getTaxableIncome() {
    return taxableIncome;
  }

  public void setTaxableIncome(BigDecimal pTaxableIncome) {
    taxableIncome = pTaxableIncome;
  }

  public String toString() {
    return new ToStringBuilder(this).append("ssn", ssn).append("year", year).append("lastName",
        lastName).toString();
  }

  public int hashCode() {
    return new HashCodeBuilder(37).append(ssn).append(year).toHashCode();
  }

  public boolean equals(Object pObject) {
    boolean equals = false;
    if (pObject instanceof TaxReturn) {
      TaxReturn bean = (TaxReturnpObject;
      equals = (new EqualsBuilder().append(ssn, bean.ssn).append(year, bean.year)).isEquals();
    }
    return equals;
  }

  public int compareTo(Object pObject) {
    return CompareToBuilder.reflectionCompare(this, pObject);
  }

}

public class MainClass {

  public static void main(String[] pArgsthrows Exception {
     TaxReturn return1 = new TaxReturn("012-68-3242"1998"O'Brien"new BigDecimal(43000.00));
     TaxReturn return2 = new TaxReturn("012-68-3242"1999"O'Brien"new BigDecimal(45000.00));
     TaxReturn return3 = new TaxReturn("012-68-3242"1999"O'Brien"new BigDecimal(53222.00));

     System.out.println("HashCodeBuilder: " + return2.hashCode());
     Set set = new HashSet();
     set.add(return1);
     set.add(return2);
     set.add(return3);
     System.out.println(set);
  }
}
HashCodeBuilder: 219210998
[TaxReturn@7ffe01[ssn=012-68-3242,year=1998,lastName=O'Brien], TaxReturn@471e30[ssn=012-68-3242,year=1999,lastName=O'Brien]]
  Download:  CommonLangCustomHashCodeBuilder.zip( 202 k)
37.16.HashCodeBuilder
37.16.1.HashCode For String
37.16.2.HashCodeBuilder.reflectionHashCode
37.16.3.HashCode Builder
37.16.4.Custom HashCode Builder
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.