/**
   * Count the number of filter to apply
   *
   * @param discoveryQueryParameters
   * @return int
   */
  private int countFilers(DiscoveryQueryParameters discoveryQueryParameters) {

    int total = 0;

    if (discoveryQueryParameters.getIdentityPublicKey() != null
        && discoveryQueryParameters.getIdentityPublicKey() != "") {
      total += 1;
    }

    if (discoveryQueryParameters.getAlias() != null && discoveryQueryParameters.getAlias() != "") {
      total += 1;
    }

    if (discoveryQueryParameters.getName() != null && discoveryQueryParameters.getName() != "") {
      total += 1;
    }

    if (discoveryQueryParameters.getExtraData() != null
        && discoveryQueryParameters.getExtraData() != "") {
      total += 1;
    }

    return total;
  }
  /**
   * Filter the PlatformComponentProfile that match with the discoveryQueryParameters
   *
   * @param discoveryQueryParameters
   * @return List<PlatformComponentProfile>
   */
  private List<PlatformComponentProfile> applyDiscoveryQueryParameters(
      DiscoveryQueryParameters discoveryQueryParameters, String clientIdentityPublicKey) {

    int totalFilterToApply = countFilers(discoveryQueryParameters);
    int filterMatched = 0;

    List<PlatformComponentProfile> list =
        getPrimaryFilteredListFromCache(
            discoveryQueryParameters.getPlatformComponentType(),
            discoveryQueryParameters.getNetworkServiceType(),
            clientIdentityPublicKey);
    List<PlatformComponentProfile> filteredLis = new ArrayList<>();

    System.out.println(
        "ComponentRegisteredListWebService - totalFilterToApply    = " + totalFilterToApply);

    if (totalFilterToApply > 0) {

      /*
       * Apply the basic filter
       */
      for (PlatformComponentProfile platformComponentProfile : list) {

        if (discoveryQueryParameters.getIdentityPublicKey() != null
            && discoveryQueryParameters.getIdentityPublicKey() != "") {
          if (platformComponentProfile
              .getIdentityPublicKey()
              .equals(discoveryQueryParameters.getIdentityPublicKey())) {
            filterMatched += 1;
          }
        }

        if (discoveryQueryParameters.getAlias() != null
            && discoveryQueryParameters.getAlias() != "") {
          if (discoveryQueryParameters
              .getAlias()
              .toLowerCase()
              .contains(platformComponentProfile.getAlias().toLowerCase())) {
            filterMatched += 1;
          }
        }

        if (discoveryQueryParameters.getName() != null
            && discoveryQueryParameters.getName() != "") {
          if (discoveryQueryParameters
              .getName()
              .toLowerCase()
              .contains(platformComponentProfile.getName().toLowerCase())) {
            filterMatched += 1;
          }
        }

        if (discoveryQueryParameters.getExtraData() != null
            && discoveryQueryParameters.getExtraData() != "") {
          if (discoveryQueryParameters
              .getExtraData()
              .toLowerCase()
              .contains(platformComponentProfile.getExtraData().toLowerCase())) {
            filterMatched += 1;
          }
        }

        // if all filter matched
        if (totalFilterToApply == filterMatched) {
          // Add to the list
          filteredLis.add(platformComponentProfile);
        }
      }

    } else {

      filteredLis = list;
    }

    /*
     * Apply geo location filter
     */
    if (discoveryQueryParameters.getLocation() != null
        && discoveryQueryParameters.getLocation().getLatitude() != 0
        && discoveryQueryParameters.getLocation().getLongitude() != 0) {

      filteredLis = applyGeoLocationFilter(filteredLis, discoveryQueryParameters);
    }

    /*
     * Apply pagination
     */
    if ((discoveryQueryParameters.getMax() != 0) && (discoveryQueryParameters.getOffset() != 0)) {

      /*
       * Apply pagination
       */
      if (filteredLis.size() > discoveryQueryParameters.getMax()
          && filteredLis.size() > discoveryQueryParameters.getOffset()) {
        filteredLis =
            filteredLis.subList(
                discoveryQueryParameters.getOffset(), discoveryQueryParameters.getMax());
      } else if (filteredLis.size() > 100) {
        filteredLis = filteredLis.subList(discoveryQueryParameters.getOffset(), 100);
      }

    } else if (filteredLis.size() > 100) {
      filteredLis = filteredLis.subList(0, 100);
    }

    return filteredLis;
  }