All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fedy2.weather.binding.adapter.SpeedUnitAdapter Maven / Gradle / Ivy

The newest version!
/**
 * 
 */
package com.github.fedy2.weather.binding.adapter;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.fedy2.weather.data.unit.SpeedUnit;

/**
 * @author "Federico De Faveri [email protected]"
 *
 */
public class SpeedUnitAdapter extends XmlAdapter {
	
	/**
	 * Official documentation says kph but the service returns km/h.
	 */
	private static final String KMH = "km/h";
	private static final String MPH = "mph";
	private Logger logger = LoggerFactory.getLogger(SpeedUnitAdapter.class);

	/**
	 * {@inheritDoc}
	 */
	@Override
	public SpeedUnit unmarshal(String v) throws Exception {
		if (MPH.equalsIgnoreCase(v)) return SpeedUnit.MPH;
		if (KMH.equalsIgnoreCase(v)) return SpeedUnit.KMH;
		logger.warn("Unknown speed unit \"{}\"", v);
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String marshal(SpeedUnit v) throws Exception {
		switch (v) {
			case KMH: return KMH;
			case MPH: return MPH;
			default: return "";
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy