Package au.net.causal.projo.prefs.jodatime

Source Code of au.net.causal.projo.prefs.jodatime.MutablePeriodToStringTransformer

package au.net.causal.projo.prefs.jodatime;

import org.joda.time.MutablePeriod;
import org.joda.time.format.ISOPeriodFormat;
import org.joda.time.format.PeriodFormatter;

import au.net.causal.projo.prefs.PreferencesException;
import au.net.causal.projo.prefs.transform.GenericToStringTransformer;

public class MutablePeriodToStringTransformer extends GenericToStringTransformer<MutablePeriod>
{
  private final PeriodFormatter formatter;
 
  public MutablePeriodToStringTransformer(PeriodFormatter formatter)
  {
    super(MutablePeriod.class);
   
    if (formatter == null)
      throw new NullPointerException("formatter == null");
   
    this.formatter = formatter;
  }
 
  public MutablePeriodToStringTransformer()
  {
    this(ISOPeriodFormat.standard());
  }

  @Override
  protected String valueToString(MutablePeriod value) throws PreferencesException
  {
    if (value == null)
      return(null);
   
    return(value.toString(formatter));
  }

  @Override
  protected MutablePeriod stringToValue(String s) throws PreferencesException
  {
    if (s == null)
      return(null);
   
    try
    {
      return(MutablePeriod.parse(s, formatter));
    }
    catch (IllegalArgumentException e)
    {
      //Thrown when parsing fails
      throw new PreferencesException("Could not parse period '" + s + "'.", e);
    }
  }
}
TOP

Related Classes of au.net.causal.projo.prefs.jodatime.MutablePeriodToStringTransformer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.