/*
 * Decompiled with CFR 0.152.
 */
package org.jxmpp.jid.impl;

import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.DomainFullJid;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.DomainAndResourcepartJid;
import org.jxmpp.jid.impl.DomainpartJid;
import org.jxmpp.jid.impl.LocalAndDomainpartJid;
import org.jxmpp.jid.impl.LocalDomainAndResourcepartJid;
import org.jxmpp.jid.parts.Domainpart;
import org.jxmpp.jid.parts.Localpart;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.stringprep.XmppStringprepException;
import org.jxmpp.util.XmppStringUtils;
import org.jxmpp.util.cache.Cache;
import org.jxmpp.util.cache.LruCache;

public class JidCreate {
    private static final Cache<String, Jid> JID_CACHE = new LruCache<String, Jid>(100);
    private static final Cache<String, BareJid> BAREJID_CACHE = new LruCache<String, BareJid>(100);
    private static final Cache<String, FullJid> FULLJID_CACHE = new LruCache<String, FullJid>(100);
    private static final Cache<String, DomainBareJid> DOMAINJID_CACHE = new LruCache<String, DomainBareJid>(100);
    private static final Cache<String, DomainFullJid> DOMAINRESOURCEJID_CACHE = new LruCache<String, DomainFullJid>(100);

    public static Jid from(CharSequence localpart, CharSequence domainpart, CharSequence resource) throws XmppStringprepException {
        return JidCreate.from(localpart.toString(), domainpart.toString(), resource.toString());
    }

    public static Jid from(String localpart, String domainpart, String resource) throws XmppStringprepException {
        String jidString = XmppStringUtils.completeJidFrom(localpart, domainpart, resource);
        Jid jid = JID_CACHE.get(jidString);
        if (jid != null) {
            return jid;
        }
        if (localpart.length() > 0 && domainpart.length() > 0 && resource.length() > 0) {
            jid = new LocalDomainAndResourcepartJid(localpart, domainpart, resource);
        } else if (localpart.length() > 0 && domainpart.length() > 0 && resource.length() == 0) {
            jid = new LocalAndDomainpartJid(localpart, domainpart);
        } else if (localpart.length() == 0 && domainpart.length() > 0 && resource.length() == 0) {
            jid = new DomainpartJid(domainpart);
        } else if (localpart.length() == 0 && domainpart.length() > 0 && resource.length() > 0) {
            jid = new DomainAndResourcepartJid(domainpart, resource);
        } else {
            throw new IllegalArgumentException("Not a valid combination of localpart, domainpart and resource");
        }
        JID_CACHE.put(jidString, jid);
        return jid;
    }

    public static Jid from(CharSequence jid) throws XmppStringprepException {
        return JidCreate.from(jid.toString());
    }

    public static Jid from(String jidString) throws XmppStringprepException {
        String localpart = XmppStringUtils.parseLocalpart(jidString);
        String domainpart = XmppStringUtils.parseDomain(jidString);
        String resource = XmppStringUtils.parseResource(jidString);
        try {
            return JidCreate.from(localpart, domainpart, resource);
        }
        catch (XmppStringprepException e) {
            throw new XmppStringprepException(jidString, e);
        }
    }

    public static Jid fromUnescaped(CharSequence unescapedJid) throws XmppStringprepException {
        return JidCreate.fromUnescaped(unescapedJid.toString());
    }

    public static Jid fromUnescaped(String unescapedJidString) throws XmppStringprepException {
        String localpart = XmppStringUtils.parseLocalpart(unescapedJidString);
        localpart = XmppStringUtils.escapeLocalpart(localpart);
        String domainpart = XmppStringUtils.parseDomain(unescapedJidString);
        String resource = XmppStringUtils.parseResource(unescapedJidString);
        try {
            return JidCreate.from(localpart, domainpart, resource);
        }
        catch (XmppStringprepException e) {
            throw new XmppStringprepException(unescapedJidString, e);
        }
    }

    public static BareJid bareFrom(CharSequence jid) throws XmppStringprepException {
        return JidCreate.bareFrom(jid.toString());
    }

    public static BareJid bareFrom(String jid) throws XmppStringprepException {
        BareJid bareJid = BAREJID_CACHE.get(jid);
        if (bareJid != null) {
            return bareJid;
        }
        String localpart = XmppStringUtils.parseLocalpart(jid);
        String domainpart = XmppStringUtils.parseDomain(jid);
        try {
            bareJid = new LocalAndDomainpartJid(localpart, domainpart);
        }
        catch (XmppStringprepException e) {
            throw new XmppStringprepException(jid, e);
        }
        BAREJID_CACHE.put(jid, bareJid);
        return bareJid;
    }

    public static BareJid bareFrom(Localpart localpart, Domainpart domain) {
        return new LocalAndDomainpartJid(localpart, domain);
    }

    public static FullJid fullFrom(CharSequence jid) throws XmppStringprepException {
        return JidCreate.fullFrom(jid.toString());
    }

    public static FullJid fullFrom(String jid) throws XmppStringprepException {
        FullJid fullJid = FULLJID_CACHE.get(jid);
        if (fullJid != null) {
            return fullJid;
        }
        String localpart = XmppStringUtils.parseLocalpart(jid);
        String domainpart = XmppStringUtils.parseDomain(jid);
        String resource = XmppStringUtils.parseResource(jid);
        try {
            fullJid = JidCreate.fullFrom(localpart, domainpart, resource);
        }
        catch (XmppStringprepException e) {
            throw new XmppStringprepException(jid, e);
        }
        FULLJID_CACHE.put(jid, fullJid);
        return fullJid;
    }

    public static FullJid fullFrom(String localpart, String domainpart, String resource) throws XmppStringprepException {
        LocalDomainAndResourcepartJid fullJid;
        try {
            fullJid = new LocalDomainAndResourcepartJid(localpart, domainpart, resource);
        }
        catch (XmppStringprepException e) {
            throw new XmppStringprepException(localpart + '@' + domainpart + '/' + resource, e);
        }
        return fullJid;
    }

    public static FullJid fullFrom(BareJid bareJid, Resourcepart resource) {
        return new LocalDomainAndResourcepartJid(bareJid, resource);
    }

    @Deprecated
    public static DomainBareJid serverBareFrom(String jid) throws XmppStringprepException {
        return JidCreate.domainBareFrom(jid);
    }

    public static DomainBareJid domainBareFrom(CharSequence jid) throws XmppStringprepException {
        return JidCreate.domainBareFrom(jid.toString());
    }

    public static DomainBareJid domainBareFrom(String jid) throws XmppStringprepException {
        DomainBareJid domainJid = DOMAINJID_CACHE.get(jid);
        if (domainJid != null) {
            return domainJid;
        }
        String domain = XmppStringUtils.parseDomain(jid);
        try {
            domainJid = new DomainpartJid(domain);
        }
        catch (XmppStringprepException e) {
            throw new XmppStringprepException(jid, e);
        }
        DOMAINJID_CACHE.put(jid, domainJid);
        return domainJid;
    }

    @Deprecated
    public static DomainFullJid serverFullFrom(String jid) throws XmppStringprepException {
        return JidCreate.donmainFullFrom(jid);
    }

    @Deprecated
    public static DomainFullJid donmainFullFrom(String jid) throws XmppStringprepException {
        return JidCreate.domainFullFrom(jid);
    }

    public static DomainFullJid domainFullFrom(CharSequence jid) throws XmppStringprepException {
        return JidCreate.domainFullFrom(jid.toString());
    }

    public static DomainFullJid domainFullFrom(String jid) throws XmppStringprepException {
        DomainFullJid domainResourceJid = DOMAINRESOURCEJID_CACHE.get(jid);
        if (domainResourceJid != null) {
            return domainResourceJid;
        }
        String domain = XmppStringUtils.parseDomain(jid);
        String resource = XmppStringUtils.parseResource(jid);
        try {
            domainResourceJid = new DomainAndResourcepartJid(domain, resource);
        }
        catch (XmppStringprepException e) {
            throw new XmppStringprepException(jid, e);
        }
        DOMAINRESOURCEJID_CACHE.put(jid, domainResourceJid);
        return domainResourceJid;
    }
}
