Created
July 12, 2018 05:08
-
-
Save rodrik/2e2afabf8e8dce653113e253856339e0 to your computer and use it in GitHub Desktop.
Commons vs Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.rodrik.commons; | |
import java.time.LocalDate; | |
import java.util.Arrays; | |
import org.apache.commons.lang3.builder.CompareToBuilder; | |
import org.apache.commons.lang3.builder.EqualsBuilder; | |
import org.apache.commons.lang3.builder.HashCodeBuilder; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.Parameterized; | |
import org.junit.runners.Parameterized.Parameters; | |
@RunWith(Parameterized.class) | |
public class HashCodeTest { | |
private static long MAX_ITERATIONS = 100; | |
@Parameters() | |
public static Iterable<DateRange[]> data() { | |
LocalDate startDate = LocalDate.of(2018, 07, 12); | |
LocalDate endDate = LocalDate.of(2018, 07, 13); | |
return Arrays.asList(new DateRange[][]{ | |
{new ApacheCommonsDateRange(startDate, endDate), new ApacheCommonsDateRange(startDate, endDate)}, | |
{new NativeDateRange(startDate, endDate), new NativeDateRange(startDate, endDate)} | |
}); | |
} | |
private DateRange obj1; | |
private DateRange obj2; | |
public HashCodeTest(DateRange obj1, DateRange obj2) { | |
this.obj1 = obj1; | |
this.obj2 = obj2; | |
} | |
@Test | |
public void testCompareTo() { | |
for(long i=0; i<MAX_ITERATIONS; i++) { | |
obj1.compareTo(obj2); | |
} | |
} | |
@Test | |
public void testHashCode() { | |
for(long i=0; i<MAX_ITERATIONS; i++) { | |
obj1.hashCode(); | |
} | |
} | |
@Test | |
public void testEqualsObject() { | |
for(long i=0; i<MAX_ITERATIONS; i++) { | |
obj1.equals(obj2); | |
} | |
} | |
interface DateRange extends Comparable<DateRange> { | |
public LocalDate getStartDate(); | |
public LocalDate getEndDate(); | |
} | |
static class ApacheCommonsDateRange implements DateRange { | |
private final LocalDate startDate; | |
private final LocalDate endDate; | |
public ApacheCommonsDateRange(LocalDate startDate, LocalDate endDate) { | |
super(); | |
this.startDate = startDate; | |
this.endDate = endDate; | |
} | |
@Override | |
public LocalDate getStartDate() { | |
return startDate; | |
} | |
@Override | |
public LocalDate getEndDate() { | |
return endDate; | |
} | |
@Override | |
public int compareTo(DateRange obj) { | |
return CompareToBuilder.reflectionCompare(this, obj); | |
} | |
@Override | |
public int hashCode() { | |
return HashCodeBuilder.reflectionHashCode(this, false); | |
} | |
@Override | |
public boolean equals(Object obj) { | |
return EqualsBuilder.reflectionEquals(this, obj, false); | |
} | |
} | |
static class NativeDateRange implements DateRange { | |
private final LocalDate startDate; | |
private final LocalDate endDate; | |
public NativeDateRange(LocalDate startDate, LocalDate endDate) { | |
super(); | |
this.startDate = startDate; | |
this.endDate = endDate; | |
} | |
@Override | |
public LocalDate getStartDate() { | |
return startDate; | |
} | |
@Override | |
public LocalDate getEndDate() { | |
return endDate; | |
} | |
@Override | |
public int compareTo(DateRange obj) { | |
int compareStartDate = getStartDate().compareTo(obj.getStartDate()); | |
return compareStartDate != 0 ? compareStartDate : getEndDate().compareTo(obj.getEndDate()); | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((endDate == null) ? 0 : endDate.hashCode()); | |
result = prime * result + ((startDate == null) ? 0 : startDate.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
NativeDateRange other = (NativeDateRange) obj; | |
if (endDate == null) { | |
if (other.endDate != null) | |
return false; | |
} else if (!endDate.equals(other.endDate)) | |
return false; | |
if (startDate == null) { | |
if (other.startDate != null) | |
return false; | |
} else if (!startDate.equals(other.startDate)) | |
return false; | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment