Princeton1/common/src/main/java/com/hithomelabs/princeton1/common/Apple.java

47 lines
1.1 KiB
Java

package com.hithomelabs.princeton1.common;
import java.util.Comparator;
import java.util.Objects;
public class Apple implements Comparable<Apple> {
private int size;
public static Comparator<Apple> COMPARE_BY_SIZE = new Comparator<Apple>() {
@Override
public int compare(Apple a1, Apple a2) {
if(a1.size < a2.size) return -1;
else if (a1.size == (a2.size)) return 0;
else return 1;
}
};
public Apple(int size) {
this.size = size;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if (obj == null || this.getClass() != obj.getClass())
return false;
Apple that = (Apple) obj;
return Objects.equals(this.size, that.size);
}
@Override
public int hashCode() {
return Objects.hash(size);
}
@Override
public String toString() {
return "An apple of size " + size;
}
@Override
public int compareTo(Apple that) {
if (this.size < that.size) return -1;
else if (this.size == that.size) return 0;
else return 1;
}
}