@@ -559,29 +559,41 @@ public T setBaseUrl(String baseUrl) {
559559 public MapboxDirections build () throws ServicesException {
560560 validateAccessToken (accessToken );
561561
562+ // Get the total number of coordinates being passed to API
563+ int coordLength = 0 ;
564+ if (coordinates != null ) {
565+ coordLength = coordinates .size ();
566+ }
567+ if (origin != null ) {
568+ coordLength += 1 ;
569+ }
570+ if (destination != null ) {
571+ coordLength += 1 ;
572+ }
573+
562574 if (profile == null ) {
563575 throw new ServicesException (
564576 "A profile is required for the Directions API. Use one of the profiles found in the"
565577 + "DirectionsCriteria.java file." );
566578 }
567579
568- if (coordinates == null || coordinates . size () < 2 ) {
580+ if (coordLength < 2 ) {
569581 throw new ServicesException (
570582 "You should provide at least two coordinates (from/to)." );
571583 }
572584
573585 if (profile .equals (DirectionsCriteria .PROFILE_DRIVING_TRAFFIC )
574- && coordinates . size () > 3 ) {
586+ && coordLength > 3 ) {
575587 throw new ServicesException (
576588 "Using the driving-traffic profile allows for maximum of 3 coordinates." );
577589 }
578590
579- if (coordinates . size () > 25 ) {
591+ if (coordLength > 25 ) {
580592 throw new ServicesException (
581593 "All profiles (except driving-traffic) allows for maximum of 25 coordinates." );
582594 }
583595
584- if (radiuses != null && radiuses .length != coordinates . size () ) {
596+ if (radiuses != null && radiuses .length != coordLength ) {
585597 throw new ServicesException (
586598 "There must be as many radiuses as there are coordinates." );
587599 }
@@ -604,7 +616,7 @@ public MapboxDirections build() throws ServicesException {
604616 }
605617 }
606618
607- if (bearings != null && bearings .length != coordinates . size () ) {
619+ if (bearings != null && bearings .length != coordLength ) {
608620 throw new ServicesException (
609621 "There must be as many bearings as there are coordinates." );
610622 }
0 commit comments