|
| 1 | +package us.brianolsen.instructions.model; |
| 2 | + |
| 3 | +import java.io.Closeable; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import com.eclipsesource.v8.V8; |
| 8 | +import com.eclipsesource.v8.V8Array; |
| 9 | +import com.eclipsesource.v8.V8Object; |
| 10 | +import com.mapbox.services.api.directions.v5.models.IntersectionLanes; |
| 11 | +import com.mapbox.services.api.directions.v5.models.LegStep; |
| 12 | +import com.mapbox.services.api.directions.v5.models.StepIntersection; |
| 13 | +import com.mapbox.services.api.directions.v5.models.StepManeuver; |
| 14 | + |
| 15 | +import us.brianolsen.instructions.util.V8Util; |
| 16 | + |
| 17 | +public class V8LegStep implements Closeable { |
| 18 | + private final V8 runtime; |
| 19 | + private final V8Object v8Step; |
| 20 | + |
| 21 | + public V8LegStep(LegStep legStep, V8 runtime) { |
| 22 | + this.runtime = runtime; |
| 23 | + this.v8Step = convertFromLegStep(legStep); |
| 24 | + } |
| 25 | + |
| 26 | + public V8 getRuntime() { |
| 27 | + return runtime; |
| 28 | + } |
| 29 | + |
| 30 | + public V8Object getV8Step() { |
| 31 | + return v8Step; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public void close() throws IOException { |
| 36 | + releaseV8LegStep(); |
| 37 | + } |
| 38 | + |
| 39 | + private V8Object convertFromLegStep(LegStep legStep) { |
| 40 | + V8Object step = new V8Object(getRuntime()); |
| 41 | + V8Object tempManeuver = convertFromStepManeuver(legStep.getManeuver()); |
| 42 | + V8Array tempIntersections = convertFromStepIntersectionList(legStep.getIntersections()); |
| 43 | + |
| 44 | + V8Util.add(step, "destinations", legStep.getDestinations()); |
| 45 | + V8Util.add(step, "distance", legStep.getDistance()); |
| 46 | + V8Util.add(step, "duration", legStep.getDuration()); |
| 47 | + V8Util.add(step, "geometry", legStep.getGeometry()); |
| 48 | + V8Util.add(step, "mode", legStep.getMode()); |
| 49 | + V8Util.add(step, "pronunciation", legStep.getPronunciation()); |
| 50 | + V8Util.add(step, "ref", legStep.getRef()); |
| 51 | + V8Util.add(step, "rotary_name", legStep.getRotaryName()); |
| 52 | + V8Util.add(step, "rotary_pronunciation", legStep.getRotaryPronunciation()); |
| 53 | + V8Util.add(step, "intersections", tempIntersections); |
| 54 | + V8Util.add(step, "weight", legStep.getWeight()); |
| 55 | + V8Util.add(step, "maneuver", tempManeuver); |
| 56 | + V8Util.add(step, "name", legStep.getName()); |
| 57 | + |
| 58 | + V8Util.release(tempManeuver); |
| 59 | + V8Util.release(tempIntersections); |
| 60 | + return step; |
| 61 | + } |
| 62 | + |
| 63 | + private V8Object convertFromStepManeuver(StepManeuver stepManeuver) { |
| 64 | + if (stepManeuver == null) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + V8Object maneuver = new V8Object(getRuntime()); |
| 68 | + V8Util.add(maneuver, "bearing_after", stepManeuver.getBearingAfter()); |
| 69 | + V8Util.add(maneuver, "bearing_before", stepManeuver.getBearingBefore()); |
| 70 | + V8Util.add(maneuver, "instruction", stepManeuver.getInstruction()); |
| 71 | + V8Util.add(maneuver, "modifier", stepManeuver.getModifier()); |
| 72 | + V8Util.add(maneuver, "type", stepManeuver.getType()); |
| 73 | + V8Util.add(maneuver, "exit", stepManeuver.getExit()); |
| 74 | + return maneuver; |
| 75 | + } |
| 76 | + |
| 77 | + private V8Array convertFromStepIntersectionList(List<StepIntersection> stepIntersections) { |
| 78 | + if (stepIntersections == null || stepIntersections.isEmpty()) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + V8Array v8Array = new V8Array(getRuntime()); |
| 83 | + if (stepIntersections != null) { |
| 84 | + for (StepIntersection stepIntersection : stepIntersections) { |
| 85 | + V8Object tempStepIntersection = convertFromStepIntersection(stepIntersection); |
| 86 | + v8Array.push(tempStepIntersection); |
| 87 | + V8Util.release(tempStepIntersection); |
| 88 | + } |
| 89 | + } |
| 90 | + return v8Array; |
| 91 | + } |
| 92 | + |
| 93 | + private V8Object convertFromStepIntersection(StepIntersection stepIntersection) { |
| 94 | + if (stepIntersection == null) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + V8Object intersection = new V8Object(getRuntime()); |
| 98 | + V8Util.add(intersection, "in", stepIntersection.getIn()); |
| 99 | + V8Util.add(intersection, "out", stepIntersection.getOut()); |
| 100 | + |
| 101 | + V8Array tempBearings = new V8Array(getRuntime()); |
| 102 | + V8Array tempEntry = new V8Array(getRuntime()); |
| 103 | + V8Array tempLanes = new V8Array(getRuntime()); |
| 104 | + V8Array tempLocation = new V8Array(getRuntime()); |
| 105 | + |
| 106 | + if (stepIntersection.getBearings() != null) { |
| 107 | + for (Integer bearing : stepIntersection.getBearings()) { |
| 108 | + tempBearings.push(bearing); |
| 109 | + } |
| 110 | + } |
| 111 | + V8Util.add(intersection, "bearings", tempBearings); |
| 112 | + |
| 113 | + if (stepIntersection.getEntry() != null) { |
| 114 | + for (Boolean lane : stepIntersection.getEntry()) { |
| 115 | + tempEntry.push(lane); |
| 116 | + } |
| 117 | + } |
| 118 | + V8Util.add(intersection, "entry", tempEntry); |
| 119 | + |
| 120 | + if (stepIntersection.getLanes() != null) { |
| 121 | + for (IntersectionLanes lane : stepIntersection.getLanes()) { |
| 122 | + V8Object tempLane = convertFromIntersectionLanes(lane); |
| 123 | + tempLanes.push(tempLane); |
| 124 | + V8Util.release(tempLane); |
| 125 | + } |
| 126 | + } |
| 127 | + V8Util.add(intersection, "lanes", tempLanes); |
| 128 | + |
| 129 | + if (stepIntersection.getLocation() != null) { |
| 130 | + for (Double item : stepIntersection.getLocation()) { |
| 131 | + tempLocation.push(item); |
| 132 | + } |
| 133 | + } |
| 134 | + V8Util.add(intersection, "location", tempLocation); |
| 135 | + |
| 136 | + V8Util.release(tempBearings); |
| 137 | + V8Util.release(tempEntry); |
| 138 | + V8Util.release(tempLanes); |
| 139 | + V8Util.release(tempLocation); |
| 140 | + |
| 141 | + return intersection; |
| 142 | + } |
| 143 | + |
| 144 | + private V8Object convertFromIntersectionLanes(IntersectionLanes intersectionLanes) { |
| 145 | + if (intersectionLanes == null) { |
| 146 | + return null; |
| 147 | + } |
| 148 | + V8Object lanes = new V8Object(getRuntime()); |
| 149 | + V8Util.add(lanes, "valid", intersectionLanes.getValid()); |
| 150 | + |
| 151 | + V8Array indications = new V8Array(getRuntime()); |
| 152 | + if (intersectionLanes.getIndications() != null) { |
| 153 | + for (String indication : intersectionLanes.getIndications()) { |
| 154 | + indications.push(indication); |
| 155 | + } |
| 156 | + } |
| 157 | + V8Util.add(lanes, "indications", indications); |
| 158 | + |
| 159 | + return lanes; |
| 160 | + } |
| 161 | + |
| 162 | + private void releaseV8LegStep() { |
| 163 | + V8Object maneuver = (V8Object) V8Util.get(v8Step, "maneuver"); |
| 164 | + V8Array intersections = (V8Array) V8Util.getV8Array(v8Step, "intersections"); |
| 165 | + |
| 166 | + if (intersections != null) { |
| 167 | + |
| 168 | + for (int i = 0; i < intersections.length(); i++) { |
| 169 | + V8Object stepIntersection = (V8Object) intersections.get(i); |
| 170 | + V8Array bearings = (V8Array) V8Util.getV8Array(stepIntersection, "bearings"); |
| 171 | + V8Array entry = (V8Array) V8Util.getV8Array(stepIntersection, "entry"); |
| 172 | + V8Array lanes = (V8Array) V8Util.getV8Array(stepIntersection, "lanes"); |
| 173 | + V8Array location = (V8Array) V8Util.getV8Array(stepIntersection, "location"); |
| 174 | + |
| 175 | + if (lanes != null) { |
| 176 | + for (int j = 0; j < lanes.length(); j++) { |
| 177 | + V8Object intersectionLanes = (V8Object) lanes.get(j); |
| 178 | + V8Array indications = (V8Array) V8Util.getV8Array(stepIntersection, "indications"); |
| 179 | + |
| 180 | + V8Util.release(indications); |
| 181 | + V8Util.release(intersectionLanes); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + V8Util.release(bearings); |
| 186 | + V8Util.release(entry); |
| 187 | + V8Util.release(lanes); |
| 188 | + V8Util.release(location); |
| 189 | + V8Util.release(stepIntersection); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + V8Util.release(intersections); |
| 194 | + V8Util.release(maneuver); |
| 195 | + V8Util.release(v8Step); |
| 196 | + } |
| 197 | +} |
0 commit comments