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

org.loom.appengine.converter.GeoPtConverter Maven / Gradle / Ivy

The newest version!
package org.loom.appengine.converter;

import javax.inject.Singleton;

import org.loom.converter.AbstractSimpleConverter;
import org.loom.i18n.MessagesRepository;
import org.loom.util.StringUtils;

import com.google.appengine.api.datastore.GeoPt;

@Singleton
public class GeoPtConverter extends AbstractSimpleConverter {

	/** the character used as separator */
	public static final String separator = ":";
	
	public GeoPtConverter() {
		super(GeoPt.class);
	}

	@Override
	public Object getAsObject(String value) {
		try {
			if (StringUtils.isEmpty(value)) {
				return null;
			}
			String[] parts = StringUtils.split(value, separator);
			if (parts.length != 2) {
				throw new NumberFormatException();
			}
			return new GeoPt(Float.parseFloat(parts[0]), Float.parseFloat(parts[1]));
		} catch (NumberFormatException e) {
			throw new IllegalArgumentException("Malformed String that cannot be converted to GeoPt: " + value, e);
		}
	}

	@Override
	protected String getAsTextImpl(Object value, MessagesRepository repository) {
		GeoPt point = (GeoPt) value;
		return point.getLatitude() + separator + point.getLongitude();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy