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

com.wwm.postcode.PostcodeConvertor Maven / Gradle / Ivy

There is a newer version: 1.1.0.RELEASE
Show newest version
/******************************************************************************
 * Copyright (c) 2005-2008 Whirlwind Match Limited. All rights reserved.
 *
 * This is open source software; you can use, redistribute and/or modify
 * it under the terms of the Open Software Licence v 3.0 as published by the 
 * Open Source Initiative.
 *
 * You should have received a copy of the Open Software Licence along with this
 * application. if not, contact the Open Source Initiative (www.opensource.org)
 *****************************************************************************/
package com.wwm.postcode;

import org.slf4j.Logger;

import com.wwm.db.core.LogFactory;
import com.wwm.util.StringUtils;

/**
 * Rather piggy-in-the-middle implementation which sits bridging static instances and OSGi service objects.
 * 
 * FIXME: This is dangerous as it assumes that only one instance of PostcodeConverter is created, and
 * therefore uses the DAO object supplied for that.
 *
 */
public class PostcodeConvertor {


	private static Logger log = LogFactory.getLogger(PostcodeConvertor.class);
	static private PostcodeService service;
			
	private final JibbleConvertor jibble;

	public static class LostDbConnection extends Exception {
		private static final long serialVersionUID = 5523931674418224181L;
	}
	
	public PostcodeConvertor() {
		jibble = new JibbleConvertor(log);
	}
	
	public static void setService(PostcodeService service) {
		PostcodeConvertor.service = service;
	}
	
	public PostcodeResult lookupShort(String prefix) {
		return jibble.lookupShort(prefix);
	}

	/**Look up a full postcode.

WARNING - EXECUTING THIS FUNCTION COSTS 1p!!

* @param postcode A full postcode to lookup, any caps, any spaces * @return A PostcodeResult if the postcode is valid, otherwise null * @throws LostDbConnection */ public synchronized PostcodeResult lookupFull(String postcode) { // If no service for full postcode, then try short somehow if (service != null){ return service.lookupFull(postcode); } log.debug("No PostcodeService present, falling back to jibble for {}", postcode); postcode = StringUtils.stripSpaces(postcode); int trimmedLength = postcode.length() - 3; // strip off tail if (trimmedLength <2 || trimmedLength > 4) return null; postcode = postcode.substring(0, trimmedLength); return jibble.lookupShort(postcode); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy