Skip to content

Commit

Permalink
fix #85
Browse files Browse the repository at this point in the history
refactor JacksonJodaFormat classes to make it possible to implement more factory methods.
  • Loading branch information
joxerTMD committed Jun 29, 2016
1 parent e8958dc commit 40abe93
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.fasterxml.jackson.datatype.joda.cfg;

import java.util.Locale;

/**
* Class that holds the state of {@link JacksonJodaFormatBase} object.
* @see JacksonJodaFormatBase#JacksonJodaFormatBase(BaseFormatSetup)
* @see JacksonJodaFormatBase#getSetup()
* @since 2.8
*/
public class BaseFormatSetup {

private Boolean _useTimestamp;

private Locale _locale;

public BaseFormatSetup(){

}

public BaseFormatSetup(BaseFormatSetup setup){
this._useTimestamp = setup.isUseTimestamp();
this._locale = setup._locale;
}

public Locale getLocale() {
return _locale;
}

public void setLocale(Locale locale) {
this._locale = locale;
}

public Boolean isUseTimestamp() {
return _useTimestamp;
}

public void setUseTimeStamp(Boolean useTimestamp) {
this._useTimestamp = useTimestamp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.fasterxml.jackson.datatype.joda.cfg;

import org.joda.time.format.DateTimeFormatter;

import java.util.TimeZone;

/**
* Class that holds the state of {@link JacksonJodaDateFormat} object.
* @see JacksonJodaDateFormat#JacksonJodaDateFormat(DateFormatSetup)
* @see JacksonJodaDateFormat#getSetup()
* @since 2.8
*/
public class DateFormatSetup extends BaseFormatSetup {

private DateTimeFormatter _formatter;

private TimeZone _timeZone;

public DateFormatSetup() {

}

public DateFormatSetup(BaseFormatSetup format) {
super(format);
}

public DateTimeFormatter getFormatter() {
return _formatter;
}

public void setFormatter(DateTimeFormatter formatter) {
this._formatter = formatter;
}

public TimeZone getTimeZone() {
return _timeZone;
}

public void setTimeZone(TimeZone timeZone) {
this._timeZone = timeZone;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@ public JacksonJodaDateFormat(JacksonJodaDateFormat base, Locale locale)
_explicitTimezone = base._explicitTimezone;
}

public JacksonJodaDateFormat(DateFormatSetup setup)
{
super(setup);
DateTimeFormatter formatter = setup.getFormatter();
if(_explicitLocale) {
formatter = formatter.withLocale(getLocale());
}
if(setup.getTimeZone() == null) {
_formatter = formatter;
DateTimeZone tz = _formatter.getZone();
_jdkTimezone = tz == null ? null : tz.toTimeZone();
_explicitTimezone = false;
} else {
_formatter = formatter.withZone(DateTimeZone.forTimeZone(setup.getTimeZone()));
_jdkTimezone = setup.getTimeZone();
_explicitTimezone = true;
}
}

@Override
protected DateFormatSetup getSetup() {
DateFormatSetup setup = new DateFormatSetup(super.getSetup());
setup.setFormatter(_formatter);
if (_explicitTimezone) {
setup.setTimeZone(_jdkTimezone);
}
return setup;
}

/*
/**********************************************************
/* Factory methods
Expand All @@ -79,7 +108,9 @@ public JacksonJodaDateFormat withUseTimestamp(Boolean useTimestamp) {
if (_useTimestamp != null && _useTimestamp.equals(useTimestamp)) {
return this;
}
return new JacksonJodaDateFormat(this, useTimestamp);
DateFormatSetup setup = getSetup();
setup.setUseTimeStamp(useTimestamp);
return new JacksonJodaDateFormat(setup);
}

public JacksonJodaDateFormat withFormat(String format) {
Expand All @@ -98,22 +129,27 @@ public JacksonJodaDateFormat withFormat(String format) {
}
// copy TimeZone from previous formatter
formatter = formatter.withZone(_formatter.getZone());

return new JacksonJodaDateFormat(this, formatter);
DateFormatSetup setup = getSetup();
setup.setFormatter(formatter);
return new JacksonJodaDateFormat(setup);
}

public JacksonJodaDateFormat withTimeZone(TimeZone tz) {
if ((tz == null) || (_jdkTimezone != null && _jdkTimezone.equals(tz))) {
return this;
}
return new JacksonJodaDateFormat(this, tz);
DateFormatSetup setup = getSetup();
setup.setTimeZone(tz);
return new JacksonJodaDateFormat(setup);
}

public JacksonJodaDateFormat withLocale(Locale locale) {
if ((locale == null) || (_locale != null && _locale.equals(locale))) {
return this;
}
return new JacksonJodaDateFormat(this, locale);
DateFormatSetup setup = getSetup();
setup.setLocale(locale);
return new JacksonJodaDateFormat(setup);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,66 @@ abstract class JacksonJodaFormatBase
protected final Locale _locale;

protected final boolean _explicitLocale;


@Deprecated
protected JacksonJodaFormatBase() {
_useTimestamp = null;
_locale = DEFAULT_LOCALE;
_explicitLocale = false;
}

@Deprecated
protected JacksonJodaFormatBase(JacksonJodaFormatBase base)
{
_useTimestamp = base._useTimestamp;
_locale = base._locale;
_explicitLocale = base._explicitLocale;
}


@Deprecated
protected JacksonJodaFormatBase(JacksonJodaFormatBase base, Boolean useTimestamp)
{
_useTimestamp = useTimestamp;
_locale = base._locale;
_explicitLocale = base._explicitLocale;
}

@Deprecated
protected JacksonJodaFormatBase(JacksonJodaFormatBase base, TimeZone jdkTimezone)
{
_useTimestamp = base._useTimestamp;
_locale = base._locale;
_explicitLocale = base._explicitLocale;
}

@Deprecated
protected JacksonJodaFormatBase(JacksonJodaFormatBase base, Locale locale)
{
_useTimestamp = base._useTimestamp;
_locale = locale;
_explicitLocale = true;
}

protected JacksonJodaFormatBase(BaseFormatSetup setup) {
_useTimestamp = setup.isUseTimestamp();
if (setup.getLocale() != null) {
_locale = setup.getLocale();
_explicitLocale = true;
} else {
_locale = DEFAULT_LOCALE;
_explicitLocale = false;
}
}

protected BaseFormatSetup getSetup() {
BaseFormatSetup setup = new BaseFormatSetup();
setup.setUseTimeStamp(_useTimestamp);
if (_explicitLocale) {
setup.setLocale(_locale);
}
return setup;
}

/*
/**********************************************************
/* Other public methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,50 @@ public class JacksonJodaPeriodFormat extends JacksonJodaFormatBase
{
protected final PeriodFormatter _formatter;

@Deprecated
public JacksonJodaPeriodFormat(PeriodFormatter defaultFormatter) {
super();
_formatter = defaultFormatter;
}

@Deprecated
public JacksonJodaPeriodFormat(JacksonJodaPeriodFormat base, Boolean useTimestamp)
{
super(base, useTimestamp);
_formatter = base._formatter;
}


@Deprecated
public JacksonJodaPeriodFormat(JacksonJodaPeriodFormat base,
PeriodFormatter formatter)
{
super(base);
_formatter = formatter;
}

@Deprecated
public JacksonJodaPeriodFormat(JacksonJodaPeriodFormat base, Locale locale)
{
super(base, locale);
_formatter = base._formatter.withLocale(locale);
}

public JacksonJodaPeriodFormat(PeriodFormatSetup setup) {
super(setup);
if(_explicitLocale) {
_formatter = setup.getFormatter().withLocale(_locale);
} else {
_formatter = setup.getFormatter();
}
}

@Override
protected PeriodFormatSetup getSetup() {
PeriodFormatSetup setup = new PeriodFormatSetup(super.getSetup());
setup.setFormatter(_formatter);
return setup;
}

/*
/**********************************************************
/* Factory methods
Expand All @@ -48,7 +68,9 @@ public JacksonJodaPeriodFormat withUseTimestamp(Boolean useTimestamp) {
if (_useTimestamp != null && _useTimestamp.equals(useTimestamp)) {
return this;
}
return new JacksonJodaPeriodFormat(this, useTimestamp);
PeriodFormatSetup setup = getSetup();
setup.setUseTimeStamp(useTimestamp);
return new JacksonJodaPeriodFormat(setup);
}

public JacksonJodaPeriodFormat withFormat(String format) {
Expand All @@ -63,7 +85,9 @@ public JacksonJodaPeriodFormat withLocale(Locale locale) {
if ((locale == null) || (_locale != null && _locale.equals(locale))) {
return this;
}
return new JacksonJodaPeriodFormat(this, locale);
PeriodFormatSetup setup = getSetup();
setup.setLocale(locale);
return new JacksonJodaPeriodFormat(setup);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fasterxml.jackson.datatype.joda.cfg;

import org.joda.time.format.PeriodFormatter;

/**
* Class that holds the state of {@link JacksonJodaPeriodFormat} object.
* @see JacksonJodaPeriodFormat#JacksonJodaPeriodFormat(PeriodFormatSetup)
* @see JacksonJodaPeriodFormat#getSetup()
* @since 2.8
*/
public class PeriodFormatSetup extends BaseFormatSetup {

private PeriodFormatter _formatter;

public PeriodFormatSetup() {

}

public PeriodFormatSetup(BaseFormatSetup setup) {
super(setup);
}

public PeriodFormatter getFormatter() {
return _formatter;
}

public void setFormatter(PeriodFormatter formatter) {
this._formatter = formatter;
}
}

0 comments on commit 40abe93

Please sign in to comment.