Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.mapbox.services.api.MapboxBuilder;
import com.mapbox.services.api.MapboxService;
import com.mapbox.services.api.ServicesException;
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;
import com.mapbox.services.commons.models.Position;
import com.mapbox.services.commons.utils.TextUtils;
import com.mapbox.services.api.directions.v5.models.DirectionsResponse;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -131,6 +131,9 @@ public Call<DirectionsResponse> cloneCall() {
*/
public static class Builder<T extends Builder> extends MapboxBuilder {

private static final int TWO_COORDINATES = 2;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Constants can go in the Constants.java file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@cammace I don't like the approach of having this kind of all-constant classes. Constants should be in their pertinent classes, basically because it's where they belong and provide more context. Actually, using an all-constant class approach is a well known antipattern.

private static final int END = 1;
private static final int BEGINNING = 0;
// We use `Boolean` instead of `boolean` to allow unset (null) values.
private String user = null;
private String profile = null;
Expand Down Expand Up @@ -223,11 +226,7 @@ public T setOrigin(Position origin) {
coordinates = new ArrayList<>();
}

// The default behavior of ArrayList is to inserts the specified element at the
// specified position in this list (beginning) and to shift the element currently at
// that position (if any) and any subsequent elements to the right (adds one to
// their indices)
coordinates.add(0, origin);
insertOrigin(origin);

return (T) this;
}
Expand All @@ -246,9 +245,7 @@ public T setDestination(Position destination) {
coordinates = new ArrayList<>();
}

// The default behavior for ArrayList is to appends the specified element
// to the end of this list.
coordinates.add(destination);
insertDestination(destination);

return (T) this;
}
Expand Down Expand Up @@ -578,5 +575,25 @@ public MapboxDirections build() throws ServicesException {
}
return new MapboxDirections(this);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On a second thought, I think we should fix this in a simpler way.

  • setOrigin(Position origin) should set a private Position origin value.
  • setDestination(Position destination) should set a private Position destination value.
  • setCoordinates(List<Position> coordinates) should set a private List<Position> coordinates value.

Then, at build time we check if both (origin, destination) and coordinates have been set. If the developer has mixed them we through a ServicesException.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@zugaldia Since I don't know in depth the SDK, does it mean that coordinates would be a list of coordinates in between the origin and the destination? At the moment, origin is the first element of the list and destination is the last one. When you try to add a new origin, if you already had more than two coordinates, the new origin is prepended to the coordinates list. Similarly, when you try to add a new destination, if you already had more than two coordinates, the new destination is appended. But, if you called setCoordinates the whole coordinates is reset. Is that the behavior that we are looking for?
cc/ @cammace

private void insertOrigin(Position origin) {
if (isThereOnlyOriginAndDestination(coordinates)) {
coordinates.set(BEGINNING, origin);
} else {
coordinates.add(BEGINNING, origin);
}
}

private void insertDestination(Position destination) {
if (isThereOnlyOriginAndDestination(coordinates)) {
coordinates.set(END, destination);
} else {
coordinates.add(destination);
}
}

private boolean isThereOnlyOriginAndDestination(List<Position> coordinates) {
return coordinates.size() == TWO_COORDINATES;
}
}
}
Loading