Hello
I noticed that the latest change to the hashCode for standard duration
fields introduces collisions in PeriodType. Because the hashCode of a
PeriodType is the sum of the hashCodes of its components, a PeriodType
with a Week and Month will have the same hashCode as one with Seconds,
and so on.
The following patch makes the hashCode a bitmask, eliminating the
collisions. A test is included.
diff -urp joda-time-2.0.orig/src/main/java/org/joda/time/DurationFieldType.java
joda-time-2.0/src/main/java/org/joda/time/DurationFieldType.java
--- joda-time-2.0.orig/src/main/java/org/joda/time/DurationFieldType.java
2011-12-06
05:03:22.193579179 -0500
+++ joda-time-2.0/src/main/java/org/joda/time/DurationFieldType.java
2011-12-06
05:46:07.713317900 -0500
@@ -275,7 +275,7 @@ public abstract class DurationFieldType
/** @inheritdoc */
@Override
public int hashCode() {
- return iOrdinal;
+ return (1 << iOrdinal);
}
public DurationField getField(Chronology chronology) {
diff -urp joda-time-2.0.orig/src/test/java/org/joda/time/TestPeriodType.java
joda-time-2.0/src/test/java/org/joda/time/TestPeriodType.java
--- joda-time-2.0.orig/src/test/java/org/joda/time/TestPeriodType.java
2011-12-06
01:56:23.390957347 -0500
+++ joda-time-2.0/src/test/java/org/joda/time/TestPeriodType.java
2011-12-06
05:48:17.881963372 -0500
@@ -593,6 +593,21 @@ public class TestPeriodType extends Test
}
}
+ public void testForFields7() throws Exception {
+ DurationFieldType[] types = new DurationFieldType[] {
+ DurationFieldType.weeks(),
+ DurationFieldType.months(),
+ };
+ DurationFieldType[] types2 = new DurationFieldType[] {
+ DurationFieldType.seconds(),
+ };
+ PeriodType type = PeriodType.forFields(types);
+ PeriodType type2 = PeriodType.forFields(types2);
+ assertEquals(false, type == type2);
+ assertEquals(false, type.equals(type2);
+ assertEquals(false, type.hashCode() == type2.hashCode());
+ }
+
//-----------------------------------------------------------------------
public void testMaskYears() throws Exception {
PeriodType type = PeriodType.standard().withYearsRemoved();
------------------------------------------------------------------------------
Cloud Services Checklist: Pricing and Packaging Optimization
This white paper is intended to serve as a reference, checklist and point of
discussion for anyone considering optimizing the pricing and packaging model
of a cloud services business. Read Now!
http://www.accelacomm.com/jaw/sfnl/114/51491232/
_______________________________________________
Joda-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/joda-interest