Princeton1/common/src/main/java/com/hithomelabs/princeton1/common/Apple.java
hitanshu310 8c47ac248c Adding benchmarking code (#17)
Reviewed-on: Hithomelabs/Princeton1#17
Reviewed-by: kruti <krutis0201@gmail.com>
Co-authored-by: hitanshu310 <hitanshu98@gmail.com>
Co-committed-by: hitanshu310 <hitanshu98@gmail.com>
2025-02-19 19:53:59 +00:00

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;
}
}