@Test public void canCalculateTheDistanceBetweenTwoLocationsOnTheSameContinent() { Coordinates centerBellInMontreal = Coordinates.locatedAt(45.4959755, -73.5693904); Coordinates yankeeStadiumInNewYork = Coordinates.locatedAt(40.8295818, -73.9261455); Length distance = centerBellInMontreal.getDistanceTo(yankeeStadiumInNewYork); assertThat(distance.toKilometers()).isWithin(0.5).of(519.23); }
@Test public void canCalculateTheDistanceBetweenTwoLocationsOnDifferentContinents() { // Example found on http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates Coordinates statueOfLiberty = Coordinates.locatedAt(40.6892, -74.0444); Coordinates eiffelTower = Coordinates.locatedAt(48.8583, 2.2945); Length distance = statueOfLiberty.getDistanceTo(eiffelTower); assertThat(distance.toKilometers()).isWithin(0.5).of(5_837); }
public Length addTwoDimensions(Length dimensionA, Length dimensionB) { double d1, d2, sum; d1 = dimensionA.convertToBase(); d2 = dimensionB.convertToBase(); sum = d1 + d2; Centimeters summation = new Centimeters(sum); return summation; }
@Test public void totalLengthTest() { Length first = new Length(4, 20); Length second = new Length(2, 155); Length finalLength = first.addLengths(second); assertEquals(7, finalLength.getMeter()); assertEquals(75, finalLength.getCentimeter()); }
/** * Encodes a value of type {@link Length}. * * @param output * The {@link OutputStream} to write the value to. * @param value * The value to encode. * @throws NullPointerException * Thrown if either {@link output} or {@link value} is * <i>null</i>. */ public static void encode(OutputStream output, Length value) throws NullPointerException { Assert.AssertNotNull(output, "output"); Assert.AssertNotNull(value, "value"); if (value.isIndefinite()) { output.append(0x80); } else { final int lengthValue = value.value(); if (lengthValue <= 0x7F) { output.append(lengthValue); } else { output.append(0x80 | encodedLength(lengthValue)); encode(output, lengthValue); } } }
@Test public void givenTwoFarLocationsButVeryLargeRange_ShouldBeInRange() { Coordinates manhattan = Coordinates.locatedAt(40.7791547, -73.9654464); Coordinates harvardUniversity = Coordinates.locatedAt(42.3770068, -71.1188488); Length range = Length.fromMiles(200); boolean inRange = harvardUniversity.isInRange(manhattan, range); assertThat(inRange).isTrue(); }
@Test public void givenTwoFarLocationsButTooShortRange_ShouldBeInRange() { Coordinates manhattan = Coordinates.locatedAt(40.7791547, -73.9654464); Coordinates harvardUniversity = Coordinates.locatedAt(42.3770068, -71.1188488); Length range = Length.fromMiles(50); boolean inRange = manhattan.isInRange(harvardUniversity, range); assertThat(inRange).isFalse(); }
@Test public void givenTwoVeryNearLocationsAndVeryShortRange_ShouldBeInRange() { Coordinates googlePlex = Coordinates.locatedAt(37.4218047, -122.0838097); Coordinates googleSoccerField = Coordinates.locatedAt(37.424242, -122.0874509); Length range = Length.fromMiles(0.30); boolean inRange = googlePlex.isInRange(googleSoccerField, range); assertThat(inRange).isTrue(); }
/** * Returns the key size of the given key object in bits. * * @param key the key object, cannot be null * @return the key size of the given key object in bits, or -1 if the key size is not accessible */ public static final int getKeySize(Key key) { int size = -1; if (key instanceof Length) { try { Length ruler = (Length) key; size = ruler.length(); } catch (UnsupportedOperationException usoe) { // ignore the exception } if (size >= 0) { return size; } } // try to parse the length from key specification if (key instanceof SecretKey) { SecretKey sk = (SecretKey) key; String format = sk.getFormat(); if ("RAW".equals(format) && sk.getEncoded() != null) { size = (sk.getEncoded().length * 8); } // Otherwise, it may be a unextractable key of PKCS#11, or // a key we are not able to handle. } else if (key instanceof RSAKey) { RSAKey pubk = (RSAKey) key; size = pubk.getModulus().bitLength(); } else if (key instanceof ECKey) { ECKey pubk = (ECKey) key; size = pubk.getParams().getOrder().bitLength(); } else if (key instanceof DSAKey) { DSAKey pubk = (DSAKey) key; size = pubk.getParams().getP().bitLength(); } else if (key instanceof DHKey) { DHKey pubk = (DHKey) key; size = pubk.getParams().getP().bitLength(); } // Otherwise, it may be a unextractable key of PKCS#11, or // a key we are not able to handle. return size; }
@Test public void testConvert() { for (final Length unit : Length.values()) { assertEquals(Length.M.convertTo(unit, BigDecimal.ONE), unit.factor); } try { Length.M.convertTo(null, BigDecimal.ONE); fail("toUnit is null"); // $NON-NLS-1$ } catch (IllegalArgumentException ex) { // nothing to do } catch (Exception ex) { fail(ex.getMessage()); } try { Length.M.convertTo(Length.KM, null); fail("value is null"); // $NON-NLS-1$ } catch (IllegalArgumentException ex) { // nothing to do } catch (Exception ex) { fail(ex.getMessage()); } }
/** * Determines the encoded length for a value of type {@link Length}, which * is used when the content of a container is being encoded. * * @param value * The value to get the encoded length for. * @return The encoded length of the passed value. * @throws NullPointerException * Thrown if {@link value} is <i>null</i>. */ public static int encodedLength(Length value) throws NullPointerException { Assert.AssertNotNull(value, "value"); return 1 + ((value.value() < 0x80 || value.isIndefinite()) ? 0 : encodedLength(value.value())); }
@Override public void add(Length other) { super.setLength((other.toMeters() + this.toMeters()) / METERS_PER_YARD); }
public static double convert(Length l) { return l.getLengthValue() / (12 * 2.54); }
@Test public void compareLength() { Length first = new Length(2, 30); Length second = new Length(0, 230); assertTrue(first.compareLengths(second)); }