    001    /*
    002     @license.text@
    003     */
    004    package com.hammurapi.reasoning.tutorial.objectmodel;
    005
    006
    007    public class Person implements Comparable<Person> {
    008
    009            private String name;
    010
    011            private boolean isMale;
    012
    013            private int age;
    014
    015            public Person(String name, int age, boolean isMale) {
    016                    super();
    017                    this.name = name;
    018                    this.age = age;
    019                    this.isMale = isMale;
    020            }
    021
    022            public String toString() {
    023                    return name + " ["+(isMale ? "male " : "female ")+age+" "+identity()+"]";
    024            }
    025
    026            private String identity() {
    027                    return ""; //Integer.toString(System.identityHashCode(this), Character.MAX_RADIX);
    028            }
    029
    030            @Override
    031            public int hashCode() {
    032                    final int prime = 31;
    033                    int result = 1;
    034                    result = prime * result + age;
    035                    result = prime * result + (isMale ? 1231 : 1237);
    036                    result = prime * result + ((name == null) ? 0 : name.hashCode());
    037                    return result;
    038            }
    039
    040            @Override
    041            public boolean equals(Object obj) {
    042                    if (this == obj) {
    043                            return true;
    044                    }
    045                    if (obj == null) {
    046                            return false;
    047                    }
    048                    if (getClass() != obj.getClass()) {
    049                            return false;
    050                    }
    051                    Person other = (Person) obj;
    052                    if (age != other.age) {
    053                            return false;
    054                    }
    055                    if (isMale != other.isMale) {
    056                            return false;
    057                    }
    058                    if (name == null) {
    059                            if (other.name != null) {
    060                                    return false;
    061                            }
    062                    } else if (!name.equals(other.name)) {
    063                            return false;
    064                    }
    065                    return true;
    066            }
    067
    068            public boolean isMale() {
    069                    return isMale;
    070            }
    071
    072            public int getAge() {
    073                    return age;
    074            }
    075
    076            public String getName() {
    077                    return name;
    078            }
    079
    080            @Override
    081            public int compareTo(Person o) {
    082                    if (this==o || this.equals(o)) {
    083                            return 0;
    084                    }
    085
    086                    int nameCompare = getName().compareTo(o.getName());
    087                    if (nameCompare!=0) {
    088                            return nameCompare;
    089                    }
    090
    091                    int ageCompare = getAge() - o.getAge();
    092
    093                    if (ageCompare!=0) {
    094                            return ageCompare;
    095                    }
    096
    097                    if (isMale()) {
    098                            if (!o.isMale()) {
    099                                    return -1;
    100                            }
    101                    } else {
    102                            if (o.isMale()) {
    103                                    return 1;
    104                            }
    105                    }
    106                    return hashCode()- o.hashCode();
    107            }
    108
    109
    110    }