package org.jxmpp.jid.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.DomainFullJid;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import org.jxmpp.util.XmppStringUtils;

public class JidUtil {
   public static boolean isValidBareJid(CharSequence jid) {
      try {
         validateBareJid(jid);
         return true;
      } catch (XmppStringprepException | JidUtil.NotABareJidStringException var2) {
         return false;
      }
   }

   public static BareJid validateBareJid(CharSequence jidcs) throws JidUtil.NotABareJidStringException, XmppStringprepException {
      String jid = jidcs.toString();
      int atIndex = jid.indexOf(64);
      if (atIndex == -1) {
         throw new JidUtil.NotABareJidStringException("'" + jid + "' does not contain a '@' character");
      } else if (jid.indexOf(64, atIndex + 1) != -1) {
         throw new JidUtil.NotABareJidStringException("'" + jid + "' contains multiple '@' characters");
      } else {
         String localpart = XmppStringUtils.parseLocalpart(jid);
         if (localpart.length() == 0) {
            throw new JidUtil.NotABareJidStringException("'" + jid + "' has empty localpart");
         } else {
            String domainpart = XmppStringUtils.parseDomain(jid);
            if (domainpart.length() == 0) {
               throw new JidUtil.NotABareJidStringException("'" + jid + "' has empty domainpart");
            } else {
               return JidCreate.bareFrom(jid);
            }
         }
      }
   }

   public static void filterBareJid(Collection<? extends Jid> in, Collection<BareJid> out) {
      Iterator i$ = in.iterator();

      while(i$.hasNext()) {
         Jid jid = (Jid)i$.next();
         BareJid bareJid = jid.asBareJidIfPossible();
         if (bareJid != null) {
            out.add(bareJid);
         }
      }

   }

   public static Set<BareJid> filterBareJidSet(Collection<? extends Jid> input) {
      Set<BareJid> res = new HashSet(input.size());
      filterBareJid(input, res);
      return res;
   }

   public static List<BareJid> filterBareJidList(Collection<? extends Jid> input) {
      List<BareJid> res = new ArrayList(input.size());
      filterBareJid(input, res);
      return res;
   }

   public static void filterFullJid(Collection<? extends Jid> in, Collection<FullJid> out) {
      Iterator i$ = in.iterator();

      while(i$.hasNext()) {
         Jid jid = (Jid)i$.next();
         FullJid fullJid = jid.asFullJidIfPossible();
         if (fullJid != null) {
            out.add(fullJid);
         }
      }

   }

   public static Set<FullJid> filterFullJidSet(Collection<? extends Jid> input) {
      Set<FullJid> res = new HashSet(input.size());
      filterFullJid(input, res);
      return res;
   }

   public static List<FullJid> filterFullJidList(Collection<? extends Jid> input) {
      List<FullJid> res = new ArrayList(input.size());
      filterFullJid(input, res);
      return res;
   }

   public static void filterDomainFullJid(Collection<? extends Jid> in, Collection<DomainFullJid> out) {
      Iterator i$ = in.iterator();

      while(i$.hasNext()) {
         Jid jid = (Jid)i$.next();
         DomainFullJid domainFullJid = jid.asDomainFullJidIfPossible();
         if (domainFullJid != null) {
            out.add(domainFullJid);
         }
      }

   }

   public static Set<DomainFullJid> filterDomainFullJidSet(Collection<? extends Jid> input) {
      Set<DomainFullJid> res = new HashSet(input.size());
      filterDomainFullJid(input, res);
      return res;
   }

   public static List<DomainFullJid> filterDomainFullJidList(Collection<? extends Jid> input) {
      List<DomainFullJid> res = new ArrayList(input.size());
      filterDomainFullJid(input, res);
      return res;
   }

   public static Set<BareJid> bareJidSetFrom(Collection<CharSequence> jidStrings) {
      Set<BareJid> res = new HashSet(jidStrings.size());
      bareJidsFrom(jidStrings, res, (List)null);
      return res;
   }

   public static void bareJidsFrom(Collection<CharSequence> jidStrings, Collection<BareJid> output, List<XmppStringprepException> exceptions) {
      Iterator i$ = jidStrings.iterator();

      while(i$.hasNext()) {
         CharSequence jid = (CharSequence)i$.next();

         try {
            BareJid bareJid = JidCreate.bareFrom(jid);
            output.add(bareJid);
         } catch (XmppStringprepException var6) {
            if (exceptions == null) {
               throw new AssertionError(var6);
            }

            exceptions.add(var6);
         }
      }

   }

   public static Set<Jid> jidSetFrom(Collection<CharSequence> jidStrings) {
      Set<Jid> res = new HashSet(jidStrings.size());
      jidsFrom(jidStrings, res, (List)null);
      return res;
   }

   public static void jidsFrom(Collection<CharSequence> jidStrings, Collection<Jid> output, List<XmppStringprepException> exceptions) {
      Iterator i$ = jidStrings.iterator();

      while(i$.hasNext()) {
         CharSequence jidString = (CharSequence)i$.next();

         try {
            Jid jid = JidCreate.from(jidString);
            output.add(jid);
         } catch (XmppStringprepException var6) {
            if (exceptions == null) {
               throw new AssertionError(var6);
            }

            exceptions.add(var6);
         }
      }

   }

   public static List<String> toStringList(Collection<? extends Jid> jids) {
      List<String> res = new ArrayList(jids.size());
      toStrings(jids, res);
      return res;
   }

   public static Set<String> toStringSet(Collection<? extends Jid> jids) {
      Set<String> res = new HashSet(jids.size());
      toStrings(jids, res);
      return res;
   }

   public static void toStrings(Collection<? extends Jid> jids, Collection<String> jidStrings) {
      Iterator i$ = jids.iterator();

      while(i$.hasNext()) {
         Jid jid = (Jid)i$.next();
         jidStrings.add(jid.toString());
      }

   }

   public static class NotABareJidStringException extends Exception {
      private static final long serialVersionUID = -1710386661031655082L;

      public NotABareJidStringException(String message) {
         super(message);
      }
   }
}
