| 1 | /* |
| 2 | @license.text@ |
| 3 | */ |
| 4 | package com.hammurapi.eventbus.tests.familyties.model; |
| 5 | |
| 6 | |
| 7 | public class Person implements Comparable<Person> { |
| 8 | |
| 9 | private String name; |
| 10 | |
| 11 | private boolean isMale; |
| 12 | |
| 13 | private int age; |
| 14 | |
| 15 | public Person(String name, int age, boolean isMale) { |
| 16 | super(); |
| 17 | this.name = name; |
| 18 | this.age = age; |
| 19 | this.isMale = isMale; |
| 20 | } |
| 21 | |
| 22 | public String toString() { |
| 23 | return name + " ["+(isMale ? "male " : "female ")+age+" "+identity()+"]"; |
| 24 | } |
| 25 | |
| 26 | private String identity() { |
| 27 | return ""; //Integer.toString(System.identityHashCode(this), Character.MAX_RADIX); |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public int hashCode() { |
| 32 | final int prime = 31; |
| 33 | int result = 1; |
| 34 | result = prime * result + age; |
| 35 | result = prime * result + (isMale ? 1231 : 1237); |
| 36 | result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| 37 | return result; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public boolean equals(Object obj) { |
| 42 | if (this == obj) { |
| 43 | return true; |
| 44 | } |
| 45 | if (obj == null) { |
| 46 | return false; |
| 47 | } |
| 48 | if (getClass() != obj.getClass()) { |
| 49 | return false; |
| 50 | } |
| 51 | Person other = (Person) obj; |
| 52 | if (age != other.age) { |
| 53 | return false; |
| 54 | } |
| 55 | if (isMale != other.isMale) { |
| 56 | return false; |
| 57 | } |
| 58 | if (name == null) { |
| 59 | if (other.name != null) { |
| 60 | return false; |
| 61 | } |
| 62 | } else if (!name.equals(other.name)) { |
| 63 | return false; |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | public boolean isMale() { |
| 69 | return isMale; |
| 70 | } |
| 71 | |
| 72 | public int getAge() { |
| 73 | return age; |
| 74 | } |
| 75 | |
| 76 | public String getName() { |
| 77 | return name; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int compareTo(Person o) { |
| 82 | if (this==o || this.equals(o)) { |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | int nameCompare = getName().compareTo(o.getName()); |
| 87 | if (nameCompare!=0) { |
| 88 | return nameCompare; |
| 89 | } |
| 90 | |
| 91 | int ageCompare = getAge() - o.getAge(); |
| 92 | |
| 93 | if (ageCompare!=0) { |
| 94 | return ageCompare; |
| 95 | } |
| 96 | |
| 97 | if (isMale()) { |
| 98 | if (!o.isMale()) { |
| 99 | return -1; |
| 100 | } |
| 101 | } else { |
| 102 | if (o.isMale()) { |
| 103 | return 1; |
| 104 | } |
| 105 | } |
| 106 | return hashCode()- o.hashCode(); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | } |