-
Notifications
You must be signed in to change notification settings - Fork 117
Fix set origin and destination #383
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -131,6 +131,9 @@ public Call<DirectionsResponse> cloneCall() { | |
| */ | ||
| public static class Builder<T extends Builder> extends MapboxBuilder { | ||
|
|
||
| private static final int TWO_COORDINATES = 2; | ||
| 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; | ||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -578,5 +575,25 @@ public MapboxDirections build() throws ServicesException { | |
| } | ||
| return new MapboxDirections(this); | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Then, at build time we check if both (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.