Skip to content

Commit

Permalink
Refactored code by solving design smells present in the code
Browse files Browse the repository at this point in the history
  • Loading branch information
mkamani committed Nov 29, 2024
1 parent ddf0074 commit a3cd5ce
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/main/java/org/joda/time/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,14 @@ public static DateTime now(Chronology chronology) {
*/
@FromString
public static DateTime parse(String str) {
return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed());
return ISODateTimeFormat.dateTimeParser().parseDateTime(str);
}

public static DateTime parse(String str, DateTimeFormatter formatter) {
if (formatter == null) {
throw new IllegalArgumentException("DateTimeFormatter must not be null");
}
return formatter.parseDateTime(str);
}

/**
Expand All @@ -158,7 +165,7 @@ public static DateTime parse(String str) {
* @return the parsed date-time, not null
* @since 2.0
*/
public static DateTime parse(String str, DateTimeFormatter formatter) {
private static DateTime parseWithFormatter(String str, DateTimeFormatter formatter) {
return formatter.parseDateTime(str);
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/joda/time/LocalDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,11 @@ public int getDayOfMonth() {
* @return the day of week
*/
public int getDayOfWeek() {
return getChronology().dayOfWeek().get(getLocalMillis());
return getDayOfWeekField().get(getLocalMillis());
}

private DateTimeField getDayOfWeekField() {
return getChronology().dayOfWeek();
}

//-----------------------------------------------------------------------
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/joda/time/YearMonth.java
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,14 @@ public static class Property extends AbstractPartialFieldProperty implements Ser
*/
@Override
public DateTimeField getField() {
return iBase.getField(iFieldIndex);
switch (iFieldIndex) {
case YearMonth.YEAR:
return iBase.getChronology().year();
case YearMonth.MONTH_OF_YEAR:
return iBase.getChronology().monthOfYear();
default:
throw new IndexOutOfBoundsException("Invalid index: " + iFieldIndex);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/joda/time/base/BasePartial.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Locale;

import org.joda.time.Chronology;
import org.joda.time.YearMonth;
import org.joda.time.DateTimeField;
import org.joda.time.DateTimeUtils;
import org.joda.time.ReadablePartial;
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/org/joda/time/TestDateTime_Constructors.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.joda.time.DateTime;
import org.joda.time.chrono.GregorianChronology;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.convert.ConverterManager;
Expand Down Expand Up @@ -171,6 +172,8 @@ public void testParse_noFormatter_vs_constructor_noOffset() throws Throwable {
public void testParse_noFormatter_vs_constructor_correctOffset() throws Throwable {
DateTime parsed = DateTime.parse("2010-06-30T01:20+01:00");
DateTime constructed = new DateTime("2010-06-30T01:20+01:00");
DateTime parsedNormalized = parsed.withZoneRetainFields(DateTimeZone.UTC);
DateTime constructedNormalized = constructed.withZoneRetainFields(DateTimeZone.UTC);
assertEquals(DateTimeZone.getDefault(), constructed.getZone());
assertEquals(DateTimeZone.forOffsetHours(1), parsed.getZone());
}
Expand Down

0 comments on commit a3cd5ce

Please sign in to comment.