Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add byPrimaryKey and getKeyColumnNames methods to GtfsTableContainer #1000

Merged
merged 1 commit into from
Sep 1, 2021

Conversation

aababilov
Copy link
Collaborator

These methods will be used by translations validator.

Sample code for GtfsStopTimeTableContainer.

  private void setupIndices(NoticeContainer noticeContainer) {
    for (GtfsStopTime newEntity : entities) {
      CompositeKey key = new CompositeKey(newEntity.tripId(), newEntity.stopSequence());
      GtfsStopTime oldEntity = byTripIdAndStopSequenceMap.getOrDefault(key, null);
      if (oldEntity != null) {
        noticeContainer.addValidationNotice(
            new DuplicateKeyNotice(
                gtfsFilename(),
                newEntity.csvRowNumber(),
                oldEntity.csvRowNumber(),
                GtfsStopTimeTableLoader.TRIP_ID_FIELD_NAME,
                oldEntity.tripId(),
                GtfsStopTimeTableLoader.STOP_SEQUENCE_FIELD_NAME,
                oldEntity.stopSequence()));
      } else {
        byTripIdAndStopSequenceMap.put(key, newEntity);
      }
    }
    for (GtfsStopTime entity : entities) {
      byTripIdMap.put(entity.tripId(), entity);
    }
    for (List<GtfsStopTime> entityList : Multimaps.asMap(byTripIdMap).values()) {
      entityList.sort(
          (entity1, entity2) -> Integer.compare(entity1.stopSequence(), entity2.stopSequence()));
    }
    for (GtfsStopTime entity : entities) {
      byStopIdMap.put(entity.stopId(), entity);
    }
  }

  @Override
  public ImmutableList<String> getKeyColumnNames() {
    return GtfsStopTimeTableLoader.KEY_COLUMN_NAMES;
  }

  @Override
  public Optional<GtfsStopTime> byPrimaryKey(String id, String subId) {
    try {
      return Optional.ofNullable(
          byTripIdAndStopSequenceMap.getOrDefault(
              new CompositeKey(id, Integer.parseInt(subId)), null));
    } catch (NumberFormatException e) {
      return Optional.empty();
    }
  }

  static class CompositeKey {
    private final String firstKey;

    private final int sequenceKey;

    CompositeKey(String firstKey, int sequenceKey) {
      this.firstKey = firstKey;
      this.sequenceKey = sequenceKey;
    }

    @Override
    public boolean equals(Object obj) {
      if (obj == this) {
        return true;
      }
      if (obj instanceof CompositeKey) {
        CompositeKey other = (CompositeKey) obj;
        return Objects.equals(firstKey, other.firstKey) && sequenceKey == other.sequenceKey;
      }
      return false;
    }

    @Override
    public int hashCode() {
      return Objects.hash(firstKey, sequenceKey);
    }
  }

…ainer

These methods will be used by translations validator.

Sample code for GtfsStopTimeTableContainer.

```
  private void setupIndices(NoticeContainer noticeContainer) {
    for (GtfsStopTime newEntity : entities) {
      CompositeKey key = new CompositeKey(newEntity.tripId(), newEntity.stopSequence());
      GtfsStopTime oldEntity = byTripIdAndStopSequenceMap.getOrDefault(key, null);
      if (oldEntity != null) {
        noticeContainer.addValidationNotice(
            new DuplicateKeyNotice(
                gtfsFilename(),
                newEntity.csvRowNumber(),
                oldEntity.csvRowNumber(),
                GtfsStopTimeTableLoader.TRIP_ID_FIELD_NAME,
                oldEntity.tripId(),
                GtfsStopTimeTableLoader.STOP_SEQUENCE_FIELD_NAME,
                oldEntity.stopSequence()));
      } else {
        byTripIdAndStopSequenceMap.put(key, newEntity);
      }
    }
    for (GtfsStopTime entity : entities) {
      byTripIdMap.put(entity.tripId(), entity);
    }
    for (List<GtfsStopTime> entityList : Multimaps.asMap(byTripIdMap).values()) {
      entityList.sort(
          (entity1, entity2) -> Integer.compare(entity1.stopSequence(), entity2.stopSequence()));
    }
    for (GtfsStopTime entity : entities) {
      byStopIdMap.put(entity.stopId(), entity);
    }
  }

  @OverRide
  public ImmutableList<String> getKeyColumnNames() {
    return GtfsStopTimeTableLoader.KEY_COLUMN_NAMES;
  }

  @OverRide
  public Optional<GtfsStopTime> byPrimaryKey(String id, String subId) {
    try {
      return Optional.ofNullable(
          byTripIdAndStopSequenceMap.getOrDefault(
              new CompositeKey(id, Integer.parseInt(subId)), null));
    } catch (NumberFormatException e) {
      return Optional.empty();
    }
  }

  static class CompositeKey {
    private final String firstKey;

    private final int sequenceKey;

    CompositeKey(String firstKey, int sequenceKey) {
      this.firstKey = firstKey;
      this.sequenceKey = sequenceKey;
    }

    @OverRide
    public boolean equals(Object obj) {
      if (obj == this) {
        return true;
      }
      if (obj instanceof CompositeKey) {
        CompositeKey other = (CompositeKey) obj;
        return Objects.equals(firstKey, other.firstKey) && sequenceKey == other.sequenceKey;
      }
      return false;
    }

    @OverRide
    public int hashCode() {
      return Objects.hash(firstKey, sequenceKey);
    }
  }

```
@isabelle-dr
Copy link
Contributor

🎉🎉🎉 ----> 1000th PR! <----- 🎉🎉🎉

Copy link
Contributor

@lionel-nj lionel-nj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @aababilov, LGTM!

@lionel-nj lionel-nj merged commit 02b1185 into MobilityData:master Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants