

package com.netflix.spinnaker.orca.clouddriver.config;

import java.util.Map;
import java.util.regex.Pattern;

public class ByApplicationServiceSelector implements ServiceSelector {
  private final Object service;
  private final int priority;
  private final Pattern applicationPattern;

  public ByApplicationServiceSelector(Object service, Integer priority, Map<String, Object> config) {
    this.service = service;
    this.priority = priority;
    this.applicationPattern = Pattern.compile((String) config.get("applicationPattern"));
  }

  @Override
  public Object getService() {
    return service;
  }

  @Override
  public int getPriority() {
    return priority;
  }

  @Override
  public boolean supports(SelectableService.Criteria criteria) {
    if (criteria.getApplication() == null) {
      return false;
    }

    return applicationPattern.matcher(criteria.getApplication().toLowerCase()).matches();
  }
}
