/**
 *
 * Copyright © 2014 Florian Schmaus
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jxmpp.stringprep;

import org.jxmpp.util.cache.Cache;
import org.jxmpp.util.cache.LruCache;

public class XmppStringPrepUtil {

	private static final Cache<String, String> NODEPREP_CACHE = new LruCache<String, String>(100);
	private static final Cache<String, String> DOMAINPREP_CACHE = new LruCache<String, String>(100);
	private static final Cache<String, String> RESOURCEPREP_CACHE = new LruCache<String, String>(100);

	private static XmppStringprep xmppStringprep;

	public static void setXmppStringprep(XmppStringprep xmppStringprep) {
		XmppStringPrepUtil.xmppStringprep = xmppStringprep;
	}

	public static String localprep(String string) throws XmppStringprepException {
		if (xmppStringprep == null) {
			return string;
		}
		String res = NODEPREP_CACHE.get(string);
		if (res != null) {
			return res;
		}
		res = xmppStringprep.localprep(string);
		NODEPREP_CACHE.put(string, res);
		return res;
	}

	public static String domainprep(String string) throws XmppStringprepException {
		if (xmppStringprep == null) {
			return string;
		}
		String res = DOMAINPREP_CACHE.get(string);
		if (res != null) {
			return res;
		}
		res = xmppStringprep.domainprep(string);
		DOMAINPREP_CACHE.put(string, res);
		return res;
	}

	public static String resourceprep(String string) throws XmppStringprepException {
		if (xmppStringprep == null) {
			return string;
		}
		String res = RESOURCEPREP_CACHE.get(string);
		if (res != null) {
			return res;
		}
		res = xmppStringprep.resourceprep(string);
		RESOURCEPREP_CACHE.put(string, res);
		return res;
	}

	public static void setMaxCacheSizes(int size) {
		NODEPREP_CACHE.setMaxCacheSize(size);
		DOMAINPREP_CACHE.setMaxCacheSize(size);
		RESOURCEPREP_CACHE.setMaxCacheSize(size);
	}
}

@@ -1336,32 +1336,116 @@
 urn string;%0A%09%09%7D%0A
+%09%09// Avoid cache lookup if string is the empty string%0A%09%09throwIfEmptyString(string);%0A
 %09%09String res = N
@@ -1463,32 +1463,32 @@
 HE.get(string);%0A
-
 %09%09if (res != nul
@@ -1733,32 +1733,116 @@
 urn string;%0A%09%09%7D%0A
+%09%09// Avoid cache lookup if string is the empty string%0A%09%09throwIfEmptyString(string);%0A
 %09%09String res = D
@@ -2145,16 +2145,100 @@
 ng;%0A%09%09%7D%0A
+%09%09// Avoid cache lookup if string is the empty string%0A%09%09throwIfEmptyString(string);%0A
 %09%09String
@@ -2549,16 +2549,16 @@
 (size);%0A
-
 %09%09RESOUR
@@ -2595,11 +2595,441 @@
 ize);%0A%09%7D
+%0A%0A%09/**%0A%09 * Throws a XMPP Stringprep exception if string is the empty string.%0A%09 *%0A%09 * @param string%0A%09 * @throws XmppStringprepException%0A%09 */%0A%09private static void throwIfEmptyString(String string) throws XmppStringprepException %7B%0A%09%09// TODO replace with string.isEmpty() once Smack's min Android SDK level is %3E 8%0A%09%09if (string.length() == 0) %7B%0A%09%09%09throw new XmppStringprepException(string, %22Argument can't be the empty string%22);%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Add empty String shortcut to XmppStringPrepUtil