gt:                                     if (chat != null) {
context: /*
   * This is the source code of Telegram for Android v. 5.x.x.
   * It is licensed under GNU GPL v. 2 or later.
   * You should have received a copy of the license in this archive (see LICENSE).
   *
   * Copyright Nikolai Kudashov, 2013-2018.
   */
  
  package org.telegram.ui.Adapters;
  
  import android.util.SparseArray;
  
  import org.telegram.PhoneFormat.PhoneFormat;
  import org.telegram.SQLite.SQLiteCursor;
  import org.telegram.SQLite.SQLitePreparedStatement;
  import org.telegram.messenger.AndroidUtilities;
  import org.telegram.messenger.ChatObject;
  import org.telegram.messenger.ContactsController;
  import org.telegram.messenger.FileLog;
  import org.telegram.messenger.MessageObject;
  import org.telegram.messenger.MessagesController;
  import org.telegram.messenger.MessagesStorage;
  import org.telegram.messenger.UserConfig;
  import org.telegram.tgnet.ConnectionsManager;
  import org.telegram.tgnet.TLObject;
  import org.telegram.tgnet.TLRPC;
  import org.telegram.ui.ChatUsersActivity;
  import org.telegram.ui.Components.ShareAlert;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  public class SearchAdapterHelper {
  
      public static class HashtagObject {
          String hashtag;
          int date;
      }
  
      public interface SearchAdapterHelperDelegate {
          void onDataSetChanged(int searchId);
  
          default void onSetHashtags(ArrayList<HashtagObject> arrayList, HashMap<String, HashtagObject> hashMap) {
  
          }
  
          default SparseArray<TLRPC.User> getExcludeUsers() {
              return null;
          }
  
          default SparseArray<TLRPC.TL_groupCallParticipant> getExcludeCallParticipants() {
              return null;
          }
  
          default boolean canApplySearchResults(int searchId) {
              return true;
          }
      }
  
      private SearchAdapterHelperDelegate delegate;
  
      private int reqId = 0;
      private int lastReqId;
      private String lastFoundUsername = null;
      private ArrayList<TLObject> localServerSearch = new ArrayList<>();
      private ArrayList<TLObject> globalSearch = new ArrayList<>();
      private SparseArray<TLObject> globalSearchMap = new SparseArray<>();
      private ArrayList<TLObject> groupSearch = new ArrayList<>();
      private SparseArray<TLObject> groupSearchMap = new SparseArray<>();
      private SparseArray<TLObject> phoneSearchMap = new SparseArray<>();
      private ArrayList<Object> phonesSearch = new ArrayList<>();
      private ArrayList<Object> localSearchResults;
  
      private int currentAccount = UserConfig.selectedAccount;
  
      private int channelReqId = 0;
      private int channelLastReqId;
      private String lastFoundChannel;
  
      private boolean allResultsAreGlobal;
      private boolean allowGlobalResults = true;
  
      private ArrayList<HashtagObject> hashtags;
      private HashMap<String, HashtagObject> hashtagsByText;
      private boolean hashtagsLoadedFromDb = false;
  
      protected static final class DialogSearchResult {
          public TLObject object;
          public int date;
          public CharSequence name;
      }
  
      public SearchAdapterHelper(boolean allAsGlobal) {
          allResultsAreGlobal = allAsGlobal;
      }
  
      public void setAllowGlobalResults(boolean value) {
          allowGlobalResults = value;
      }
  
      public boolean isSearchInProgress() {
          return reqId != 0 || channelReqId != 0;
      }
  
      public void queryServerSearch(String query, boolean allowUsername, boolean allowChats, boolean allowBots, boolean allowSelf, boolean canAddGroupsOnly, int channelId, boolean phoneNumbers, int type, int searchId) {
          if (reqId != 0) {
              ConnectionsManager.getInstance(currentAccount).cancelRequest(reqId, true);
              reqId = 0;
          }
          if (channelReqId != 0) {
              ConnectionsManager.getInstance(currentAccount).cancelRequest(channelReqId, true);
              channelReqId = 0;
          }
          if (query == null) {
              groupSearch.clear();
              groupSearchMap.clear();
              globalSearch.clear();
              globalSearchMap.clear();
              localServerSearch.clear();
              phonesSearch.clear();
              phoneSearchMap.clear();
              lastReqId = 0;
              channelLastReqId = 0;
              delegate.onDataSetChanged(searchId);
              return;
          }
          if (query.length() > 0) {
              if (channelId != 0) {
                  TLRPC.TL_channels_getParticipants req = new TLRPC.TL_channels_getParticipants();
                  if (type == ChatUsersActivity.TYPE_ADMIN) {
                      req.filter = new TLRPC.TL_channelParticipantsAdmins();
                  } else if (type == ChatUsersActivity.TYPE_KICKED) {
                      req.filter = new TLRPC.TL_channelParticipantsBanned();
                  } else if (type == ChatUsersActivity.TYPE_BANNED) {
                      req.filter = new TLRPC.TL_channelParticipantsKicked();
                  } else {
                      req.filter = new TLRPC.TL_channelParticipantsSearch();
                  }
                  req.filter.q = query;
                  req.limit = 50;
                  req.offset = 0;
                  req.channel = MessagesController.getInstance(currentAccount).getInputChannel(channelId);
                  final int currentReqId = ++channelLastReqId;
                  channelReqId = ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
                      if (currentReqId == channelLastReqId) {
                          channelReqId = 0;
                          if (error == null) {
                              TLRPC.TL_channels_channelParticipants res = (TLRPC.TL_channels_channelParticipants) response;
                              lastFoundChannel = query.toLowerCase();
                              MessagesController.getInstance(currentAccount).putUsers(res.users, false);
                              MessagesController.getInstance(currentAccount).putChats(res.chats, false);
                              groupSearch.clear();
                              groupSearchMap.clear();
                              groupSearch.addAll(res.participants);
                              int currentUserId = UserConfig.getInstance(currentAccount).getClientUserId();
                              for (int a = 0, N = res.participants.size(); a < N; a++) {
                                  TLRPC.ChannelParticipant participant = res.participants.get(a);
                                  int peerId = MessageObject.getPeerId(participant.peer);
                                  if (!allowSelf && peerId == currentUserId) {
                                      groupSearch.remove(participant);
                                      continue;
                                  }
                                  groupSearchMap.put(peerId, participant);
                              }
                              removeGroupSearchFromGlobal();
                              if (localSearchResults != null) {
                                  mergeResults(localSearchResults);
                              }
                              delegate.onDataSetChanged(searchId);
                          }
                      }
                  }), ConnectionsManager.RequestFlagFailOnServerErrors);
              } else {
                  lastFoundChannel = query.toLowerCase();
              }
          } else {
              groupSearch.clear();
              groupSearchMap.clear();
              channelLastReqId = 0;
              delegate.onDataSetChanged(searchId);
          }
          if (allowUsername) {
              if (query.length() > 0) {
                  TLRPC.TL_contacts_search req = new TLRPC.TL_contacts_search();
                  req.q = query;
                  req.limit = 50;
                  final int currentReqId = ++lastReqId;
                  reqId = ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
                      if (currentReqId == lastReqId) {
                          reqId = 0;
                      }
                      if (currentReqId == lastReqId && delegate.canApplySearchResults(searchId)) {
                          if (error == null) {
                              TLRPC.TL_contacts_found res = (TLRPC.TL_contacts_found) response;
                              globalSearch.clear();
                              globalSearchMap.clear();
                              localServerSearch.clear();
                              MessagesController.getInstance(currentAccount).putChats(res.chats, false);
                              MessagesController.getInstance(currentAccount).putUsers(res.users, false);
                              MessagesStorage.getInstance(currentAccount).putUsersAndChats(res.users, res.chats, true, true);
                              SparseArray<TLRPC.Chat> chatsMap = new SparseArray<>();
                              SparseArray<TLRPC.User> usersMap = new SparseArray<>();
                              for (int a = 0; a < res.chats.size(); a++) {
                                  TLRPC.Chat chat = res.chats.get(a);
                                  chatsMap.put(chat.id, chat);
                              }
                              for (int a = 0; a < res.users.size(); a++) {
                                  TLRPC.User user = res.users.get(a);
                                  usersMap.put(user.id, user);
                              }
                              for (int b = 0; b < 2; b++) {
                                  ArrayList<TLRPC.Peer> arrayList;
                                  if (b == 0) {
                                      if (!allResultsAreGlobal) {
                                          continue;
                                      }
                                      arrayList = res.my_results;
                                  } else {
                                      arrayList = res.results;
                                  }
                                  for (int a = 0; a < arrayList.size(); a++) {
                                      TLRPC.Peer peer = arrayList.get(a);
                                      TLRPC.User user = null;
                                      TLRPC.Chat chat = null;
                                      if (peer.user_id != 0) {
                                          user = usersMap.get(peer.user_id);
                                      } else if (peer.chat_id != 0) {
                                          chat = chatsMap.get(peer.chat_id);
                                      } else if (peer.channel_id != 0) {
                                          chat = chatsMap.get(peer.channel_id);
                                      }
                                      if (chat != null) {
                                          if (!allowChats || canAddGroupsOnly && !ChatObject.canAddBotsToChat(chat) || !allowGlobalResults && ChatObject.isNotInChat(chat)) {
                                              continue;
                                          }
                                          globalSearch.add(chat);
                                          globalSearchMap.put(-chat.id, chat);
                                      } else if (user != null) {
                                          if (canAddGroupsOnly || !allowBots && user.bot || !allowSelf && user.self || !allowGlobalResults && b == 1 && !user.contact) {
                                              continue;
                                          }
                                          globalSearch.add(user);
                                          globalSearchMap.put(user.id, user);
                                      }
                                  }
                              }
                              if (!allResultsAreGlobal) {
                                  for (int a = 0; a < res.my_results.size(); a++) {
                                      TLRPC.Peer peer = res.my_results.get(a);
                                      TLRPC.User user = null;
                                      TLRPC.Chat chat = null;
                                      if (peer.user_id != 0) {
                                          user = usersMap.get(peer.user_id);
                                      } else if (peer.chat_id != 0) {
                                          chat = chatsMap.get(peer.chat_id);
                                      } else if (peer.channel_id != 0) {
                                          chat = chatsMap.get(peer.channel_id);
                                      }
  