All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.api.parameter.QualifiedMode Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
package org.opentripplanner.api.parameter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import javax.ws.rs.BadRequestException;

public class QualifiedMode implements Serializable {

  private static final long serialVersionUID = 1L;

  public final ApiRequestMode mode;
  public final Set qualifiers;

  public QualifiedMode(String qMode) {
    try {
      Collection qualifiers = new ArrayList<>();
      StringBuilder mode = new StringBuilder();

      String[] elements = qMode.split("_");
      mode.append(elements[0].trim());
      for (int i = 1; i < elements.length; i++) {
        String element = elements[i].trim();
        if (element.isBlank()) {
          continue;
        }
        try {
          Qualifier q = Qualifier.valueOf(element);
          qualifiers.add(q);
        } catch (IllegalArgumentException e) {
          mode.append("_");
          mode.append(element);
        }
      }

      this.mode = ApiRequestMode.valueOf(mode.toString());
      this.qualifiers = Set.copyOf(qualifiers);
    } catch (IllegalArgumentException | IndexOutOfBoundsException e) {
      throw new BadRequestException(
        "Qualified mode is not valid: '" + qMode + "', details: " + e.getMessage()
      );
    }
  }

  public QualifiedMode(ApiRequestMode mode, Qualifier... qualifiers) {
    this.mode = mode;
    this.qualifiers = Set.of(qualifiers);
  }

  @Override
  public int hashCode() {
    return mode.hashCode() * qualifiers.hashCode();
  }

  @Override
  public boolean equals(Object other) {
    if (other instanceof QualifiedMode) {
      QualifiedMode qmOther = (QualifiedMode) other;
      return qmOther.mode.equals(this.mode) && qmOther.qualifiers.equals(this.qualifiers);
    }
    return false;
  }

  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(mode);
    for (Qualifier qualifier : qualifiers) {
      sb.append("_");
      sb.append(qualifier);
    }
    return sb.toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy