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.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(100);
   private static final Cache<String, BareJid> BAREJID_CACHE = new LruCache(100);
   private static final Cache<String, FullJid> FULLJID_CACHE = new LruCache(100);
   private static final Cache<String, DomainBareJid> DOMAINJID_CACHE = new LruCache(100);
   private static final Cache<String, DomainFullJid> DOMAINRESOURCEJID_CACHE = new LruCache(100);

   public static Jid from(CharSequence localpart, CharSequence domainpart, CharSequence resource) throws XmppStringprepException {
      return 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)JID_CACHE.get(jidString);
      if (jid != null) {
         return jid;
      } else {
         Object 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) {
               throw new IllegalArgumentException("Not a valid combination of localpart, domainpart and resource");
            }

            jid = new DomainAndResourcepartJid(domainpart, resource);
         }

         JID_CACHE.put(jidString, jid);
         return (Jid)jid;
      }
   }

   public static Jid from(CharSequence jid) throws XmppStringprepException {
      return 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 from(localpart, domainpart, resource);
      } catch (XmppStringprepException var5) {
         throw new XmppStringprepException(jidString, var5);
      }
   }

   public static Jid fromUnescaped(CharSequence unescapedJid) throws XmppStringprepException {
      return 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 from(localpart, domainpart, resource);
      } catch (XmppStringprepException var5) {
         throw new XmppStringprepException(unescapedJidString, var5);
      }
   }

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

   public static BareJid bareFrom(String jid) throws XmppStringprepException {
      BareJid bareJid = (BareJid)BAREJID_CACHE.get(jid);
      if (bareJid != null) {
         return bareJid;
      } else {
         String localpart = XmppStringUtils.parseLocalpart(jid);
         String domainpart = XmppStringUtils.parseDomain(jid);

         LocalAndDomainpartJid bareJid;
         try {
            bareJid = new LocalAndDomainpartJid(localpart, domainpart);
         } catch (XmppStringprepException var5) {
            throw new XmppStringprepException(jid, var5);
         }

         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 fullFrom(jid.toString());
   }

   public static FullJid fullFrom(String jid) throws XmppStringprepException {
      FullJid fullJid = (FullJid)FULLJID_CACHE.get(jid);
      if (fullJid != null) {
         return fullJid;
      } else {
         String localpart = XmppStringUtils.parseLocalpart(jid);
         String domainpart = XmppStringUtils.parseDomain(jid);
         String resource = XmppStringUtils.parseResource(jid);

         try {
            fullJid = fullFrom(localpart, domainpart, resource);
         } catch (XmppStringprepException var6) {
            throw new XmppStringprepException(jid, var6);
         }

         FULLJID_CACHE.put(jid, fullJid);
         return fullJid;
      }
   }

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

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

   /** @deprecated */
   @Deprecated
   public static DomainBareJid serverBareFrom(String jid) throws XmppStringprepException {
      return domainBareFrom(jid);
   }

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

   public static DomainBareJid domainBareFrom(String jid) throws XmppStringprepException {
      DomainBareJid domainJid = (DomainBareJid)DOMAINJID_CACHE.get(jid);
      if (domainJid != null) {
         return domainJid;
      } else {
         String domain = XmppStringUtils.parseDomain(jid);

         DomainpartJid domainJid;
         try {
            domainJid = new DomainpartJid(domain);
         } catch (XmppStringprepException var4) {
            throw new XmppStringprepException(jid, var4);
         }

         DOMAINJID_CACHE.put(jid, domainJid);
         return domainJid;
      }
   }

   /** @deprecated */
   @Deprecated
   public static DomainFullJid serverFullFrom(String jid) throws XmppStringprepException {
      return donmainFullFrom(jid);
   }

   /** @deprecated */
   @Deprecated
   public static DomainFullJid donmainFullFrom(String jid) throws XmppStringprepException {
      return domainFullFrom(jid);
   }

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

   public static DomainFullJid domainFullFrom(String jid) throws XmppStringprepException {
      DomainFullJid domainResourceJid = (DomainFullJid)DOMAINRESOURCEJID_CACHE.get(jid);
      if (domainResourceJid != null) {
         return domainResourceJid;
      } else {
         String domain = XmppStringUtils.parseDomain(jid);
         String resource = XmppStringUtils.parseResource(jid);

         DomainAndResourcepartJid domainResourceJid;
         try {
            domainResourceJid = new DomainAndResourcepartJid(domain, resource);
         } catch (XmppStringprepException var5) {
            throw new XmppStringprepException(jid, var5);
         }

         DOMAINRESOURCEJID_CACHE.put(jid, domainResourceJid);
         return domainResourceJid;
      }
   }
}
