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

org.aksw.commons.collections.generator.Converters Maven / Gradle / Ivy

There is a newer version: 0.9.9
Show newest version
package org.aksw.commons.collections.generator;

import java.util.function.Function;

import com.google.common.base.Converter;

public class Converters {
	public static Converter prefixIntToStr(String prefix) {
		return prefix(prefix, Integer::parseInt, x -> Integer.toString(x));
	}

	public static  Converter prefix(
			String prefix,
			Function parse,
			Function unparse) {
		
		int n = prefix.length();

		return new Converter() {

			@Override
			protected String doForward(T a) {
				String str = unparse.apply(a);
				return prefix + str;
			}

			@Override
			protected T doBackward(String b) {
				T result;
				if(b.startsWith(prefix)) {
					String substr = b.substring(n, b.length());
					result = parse.apply(substr);
				} else {
					// TODO Raise exception?
					result = null;
				}

				return result;
			}
			
		};
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy