static MoveProgress create(Measure<Double, Length> dist, Measure<Long, Duration> pTime, List<Point> pTravelledNodes) { checkArgument(dist.getValue() >= 0d, "Distance must be greater than or equal to 0."); checkArgument(pTime.getValue() >= 0L, "Time must be greather than or equal to 0."); return new AutoValue_MoveProgress(dist, pTime, ImmutableList.copyOf(pTravelledNodes)); }
@Override public JsonElement doSerialize(Measure<?, ?> src, Type typeOfSrc, JsonSerializationContext context) { final JsonObject obj = new JsonObject(); obj.add(UNIT, context.serialize(src.getUnit(), Unit.class)); obj.add(VALUE, context.serialize(src.getValue())); obj.addProperty(VALUE_TYPE, src.getValue().getClass().getName()); return obj; } }
@Override public JsonElement doSerialize(Measure<?, ?> src, Type typeOfSrc, JsonSerializationContext context) { final JsonObject obj = new JsonObject(); obj.add(UNIT, context.serialize(src.getUnit(), Unit.class)); obj.add(VALUE, context.serialize(src.getValue())); obj.addProperty(VALUE_TYPE, src.getValue().getClass().getName()); return obj; } }
/** * Returns the hash code for this scalar. * * @return the hash code value. */ public int hashCode() { return getUnit().hashCode() + getValue().hashCode(); }
/** * Returns the hash code for this scalar. * * @return the hash code value. */ public int hashCode() { return getUnit().hashCode() + getValue().hashCode(); }
/** * Returns the hash code for this scalar. * * @return the hash code value. */ public int hashCode() { return getUnit().hashCode() + getValue().hashCode(); }
/** * Compares this measure against the specified object for * strict equality (same unit and amount). * To compare measures stated using different units the * {@link #compareTo} method should be used. * * @param obj the object to compare with. * @return <code>true</code> if both objects are identical (same * unit and same amount); <code>false</code> otherwise. */ @SuppressWarnings("unchecked") public boolean equals(Object obj) { if (!(obj instanceof Measure)) return false; Measure that = (Measure) obj; return this.getUnit().equals(that.getUnit()) && this.getValue().equals(that.getValue()); }
/** * Compares this measure against the specified object for * strict equality (same unit and amount). * To compare measures stated using different units the * {@link #compareTo} method should be used. * * @param obj the object to compare with. * @return <code>true</code> if both objects are identical (same * unit and same amount); <code>false</code> otherwise. */ @SuppressWarnings("unchecked") public boolean equals(Object obj) { if (!(obj instanceof Measure)) return false; Measure that = (Measure) obj; return this.getUnit().equals(that.getUnit()) && this.getValue().equals(that.getValue()); }
/** * Compares this measure against the specified object for * strict equality (same unit and amount). * To compare measures stated using different units the * {@link #compareTo} method should be used. * * @param obj the object to compare with. * @return <code>true</code> if both objects are identical (same * unit and same amount); <code>false</code> otherwise. */ @SuppressWarnings("unchecked") public boolean equals(Object obj) { if (!(obj instanceof Measure)) return false; Measure that = (Measure) obj; return this.getUnit().equals(that.getUnit()) && this.getValue().equals(that.getValue()); }
@Override public void tick(TimeLapse timeLapse) { if (!destination.isPresent()) { nextDestination(); } final MoveProgress mp = roadModel.get().moveTo(this, destination.get(), timeLapse); if (roadModel.get().getPosition(this).equals(destination.get()) || stuckTickCount >= MAX_STUCK_TICK_COUNT) { nextDestination(); stuckTickCount = 0; } else if (mp.distance().getValue().doubleValue() == 0d) { stuckTickCount++; } else { stuckTickCount = 0; } }
/** * Returns the <code>String</code> representation of this measure * The string produced for a given measure is always the same; * it is not affected by locale. This means that it can be used * as a canonical string representation for exchanging data, * or as a key for a Hashtable, etc. Locale-sensitive * measure formatting and parsing is handled by the {@link * MeasureFormat} class and its subclasses. * * @return the string representation of this measure. */ public String toString() { if (getUnit() instanceof CompoundUnit) return MeasureFormat.DEFAULT.formatCompound(doubleValue(getUnit()), getUnit(), new StringBuffer(), null).toString(); return getValue() + " " + getUnit(); }
/** * Returns the <code>String</code> representation of this measure * The string produced for a given measure is always the same; * it is not affected by locale. This means that it can be used * as a canonical string representation for exchanging data, * or as a key for a Hashtable, etc. Locale-sensitive * measure formatting and parsing is handled by the {@link * MeasureFormat} class and its subclasses. * * @return the string representation of this measure. */ public String toString() { if (getUnit() instanceof CompoundUnit) return MeasureFormat.DEFAULT.formatCompound(doubleValue(getUnit()), getUnit(), new StringBuffer(), null).toString(); return getValue() + " " + getUnit(); }
/** * Returns the <code>String</code> representation of this measure * The string produced for a given measure is always the same; * it is not affected by locale. This means that it can be used * as a canonical string representation for exchanging data, * or as a key for a Hashtable, etc. Locale-sensitive * measure formatting and parsing is handled by the {@link * MeasureFormat} class and its subclasses. * * @return the string representation of this measure. */ public String toString() { if (getUnit() instanceof CompoundUnit) return MeasureFormat.DEFAULT.formatCompound(doubleValue(getUnit()), getUnit(), new StringBuffer(), null).toString(); return getValue() + " " + getUnit(); }
public void compatibilityCheck(List<Point> t) { final MovingRoadUser truck = new TrivialRoadUser(); rm.addObjectAt(truck, t.get(0)); final double len = pathLength(t); // speed of trivial truck is 1 len per hour thus we need to travel 'len' // hours final MoveProgress progress = rm.followPath( truck, new LinkedList<Point>(t), TimeLapseFactory.create(NonSI.HOUR, 0, DoubleMath.roundToLong(len, RoundingMode.CEILING))); assertEquals(len, progress.distance().getValue(), EPSILON); }
/** * Situation: <code>SW ->- tru1 ->- SE </code><br> * tru1 wants to move directly to SW, which should throw an exception since * moving backward over an directed connection is not allowed. */ @Test(expected = IllegalArgumentException.class) public void followPathFailIllegalPath1() { final TestRoadUser testRoadUser = new TestRoadUser(); model.addObjectAt(testRoadUser, SW); final Queue<Point> p = new LinkedList<Point>(Arrays.asList(SW, SE)); final MoveProgress progress = model.followPath(testRoadUser, p, timeLength(3600000)); assertEquals(1.0, progress.distance().getValue(), EPSILON); assertEquals(asList(SE), p); assertEquals(3600000, progress.time().getValue().longValue()); assertEquals(asList(SW), progress.travelledNodes()); model.followPath(testRoadUser, asPath(SW), timeLength(1)); }
/** * Follow path */ @Test public void followPathAllAtOnce() { final SpeedyRoadUser agent = new SpeedyRoadUser(18000000d); // sc.from(5, TimeUnit.MS) // .to(TimeUnit.H)); model.addObjectAt(agent, SW); assertEquals(SW, model.getPosition(agent)); final Queue<Point> path = asPath(SW, SE, NE); final MoveProgress travelled = model.followPath(agent, path, timeLength(5)); assertEquals(20, travelled.distance().getValue(), EPSILON); assertEquals(4, travelled.time().getValue().longValue()); assertEquals(0, path.size()); assertEquals(NE, model.getPosition(agent)); }
@Test public void followPathStepByStep() { final SpeedyRoadUser agent = new SpeedyRoadUser(speed); model.addObjectAt(agent, new Point(0, 0)); assertEquals(new Point(0, 0), model.getPosition(agent)); assertEquals(5, path.size()); final MoveProgress progress = model.followPath(agent, path, AbstractRoadModelTest.hour()); assertEquals(speed, progress.distance().getValue(), DELTA); assertEquals(4, path.size()); }
@Test public void followPathNotTillEnd() { final MovingRoadUser agent = new SpeedyRoadUser(1); model.addObjectAt(agent, new Point(0, 0)); assertEquals(new Point(0, 0), model.getPosition(agent)); final Queue<Point> path = asPath(SW, SE, NE); MoveProgress travelled = model.followPath(agent, path, hour(10)); assertEquals(10d, travelled.distance().getValue(), EPSILON); assertEquals(1, path.size()); travelled = model.followPath(agent, path, hour(1)); assertEquals(1d, travelled.distance().getValue(), EPSILON); assertEquals(1, path.size()); assertEquals(new Point(10, 1), model.getPosition(agent)); }
@Test public void followPathAllAtOnce() { final int timeNeeded = DoubleMath.roundToInt(pathLength / speed * 1.5, RoundingMode.CEILING); final TimeLapse timeLapse = TimeLapseFactory.create(NonSI.HOUR, 0, timeNeeded); final SpeedyRoadUser agent = new SpeedyRoadUser(speed); model.addObjectAt(agent, new Point(0, 0)); assertEquals(new Point(0, 0), model.getPosition(agent)); assertEquals(5, path.size()); final MoveProgress travelled = model.followPath(agent, path, timeLapse); assertTrue(timeLapse.hasTimeLeft()); assertEquals(pathLength, travelled.distance().getValue(), DELTA); assertTrue("time spend < timeNeeded", timeNeeded > travelled.time().getValue()); assertEquals(0, path.size()); assertEquals(new Point(5, 15), model.getPosition(agent)); }