package org.jivesoftware.smack.filter;

import org.jivesoftware.smack.packet.Stanza;
import org.jxmpp.jid.Jid;

public class FromMatchesFilter implements StanzaFilter {
   private final Jid address;
   private final boolean ignoreResourcepart;

   public FromMatchesFilter(Jid address, boolean ignoreResourcepart) {
      if (address != null && ignoreResourcepart) {
         this.address = address.withoutResource();
      } else {
         this.address = address;
      }

      this.ignoreResourcepart = ignoreResourcepart;
   }

   public static FromMatchesFilter create(Jid address) {
      return new FromMatchesFilter(address, address.hasNoResource());
   }

   public static FromMatchesFilter createBare(Jid address) {
      address = address == null ? null : address;
      return new FromMatchesFilter(address, true);
   }

   public static FromMatchesFilter createFull(Jid address) {
      return new FromMatchesFilter(address, false);
   }

   public boolean accept(Stanza packet) {
      Jid from = packet.getFrom();
      if (from == null) {
         return this.address == null;
      } else {
         if (this.ignoreResourcepart) {
            from = from.withoutResource();
         }

         return from.equals((CharSequence)this.address);
      }
   }

   public String toString() {
      String matchMode = this.ignoreResourcepart ? "ignoreResourcepart" : "full";
      return this.getClass().getSimpleName() + " (" + matchMode + "): " + this.address;
   }
}
