DoubleValueFactory extends AbstractValueFactory<Double> { @Override public Double create( String value ) { if (value == null) return null; try { return Double.valueOf(value.trim()); } catch (NumberFormatException err) { throw new ValueFormatException(value, getPropertyType(), GraphI18n.errorConvertingType.text(String.class.getSimpleName(), Double.class.getSimpleName(), value), err); } } DoubleValueFactory( TextDecoder decoder,
                               ValueFactories factories ); @Override ValueFactory<Double> with( ValueFactories valueFactories ); @Override Double create( String value ); @Override Double create( String value,
                          TextDecoder decoder ); @Override Double create( int value ); @Override Double create( long value ); @Override Double create( boolean value ); @Override Double create( float value ); @Override Double create( double value ); @Override Double create( BigDecimal value ); @Override Double create( Calendar value ); @Override Double create( Date value ); @Override Double create( DateTime value ); @Override Double create( Name value ); @Override Double create( Path value ); @Override Double create( Path.Segment value ); @Override Double create( Reference value ); @Override Double create( URI value ); @Override Double create( UUID value ); @Override Double create( NodeKey value ); @Override Double create( byte[] value ); @Override Double create( BinaryValue value ); @Override Double create( InputStream stream ); @Override Double[] createEmptyArray( int length );  }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDoubleFromBooleanValue() { factory.create(true); }
@Test public void shouldCreateDoubleFromString() { assertThat(factory.create("1"), is(Double.valueOf(1))); assertThat(factory.create("-1.0"), is(Double.valueOf(-1.0d))); assertThat(factory.create("100.000101"), is(Double.valueOf(100.000101d))); }
@Test public void shouldCreateDoubleFromStringRegardlessOfLeadingAndTrailingWhitespace() { assertThat(factory.create(" 1 "), is(Double.valueOf(1))); assertThat(factory.create(" -1.0 "), is(Double.valueOf(-1.0d))); assertThat(factory.create(" 100.000101 "), is(Double.valueOf(100.000101d))); }
@Test public void shouldNotCreateDoubleFromIntegerValue() { assertThat(factory.create(1), is(1.0d)); }
@Test public void shouldNotCreateDoubleFromLongValue() { assertThat(factory.create(1l), is(1.0d)); }
@Test public void shouldNotCreateDoubleFromFloatValue() { assertThat(factory.create(1.0f), is(1.0d)); }
@Test public void shouldNotCreateDoubleFromDoubleValue() { assertThat(factory.create(1.0d), is(1.0d)); }
@Test public void shouldCreateDoubleFromBigDecimal() { BigDecimal value = new BigDecimal(100); assertThat(factory.create(value), is(value.doubleValue())); }
@Test public void shouldCreateDoubleFromDate() { Date value = new Date(); assertThat(factory.create(value), is(Double.valueOf(value.getTime()))); }
@Test public void shouldCreateDoubleFromCalendar() { Calendar value = Calendar.getInstance(); assertThat(factory.create(value), is(Double.valueOf(value.getTimeInMillis()))); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDoubleFromName() { factory.create(mock(Name.class)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDoubleFromPath() { factory.create(mock(Path.class)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDoubleFromReference() { factory.create(mock(Reference.class)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDoubleFromUri() throws Exception { factory.create(new URI("http: }
@Test public void shouldCreateDoubleFromByteArrayContainingUtf8EncodingOfStringWithDouble() throws Exception { assertThat(factory.create("0.1".getBytes("UTF-8")), is(0.1d)); assertThat(factory.create("1".getBytes("UTF-8")), is(1.0d)); assertThat(factory.create("-1.03".getBytes("UTF-8")), is(-1.03)); assertThat(factory.create("1003044".getBytes("UTF-8")), is(1003044.d)); }
@Test public void shouldCreateDoubleFromInputStreamContainingUtf8EncodingOfStringWithDouble() throws Exception { assertThat(factory.create(new ByteArrayInputStream("0.1".getBytes("UTF-8"))), is(0.1d)); assertThat(factory.create(new ByteArrayInputStream("1".getBytes("UTF-8"))), is(1.0d)); assertThat(factory.create(new ByteArrayInputStream("-1.03".getBytes("UTF-8"))), is(-1.03)); assertThat(factory.create(new ByteArrayInputStream("1003044".getBytes("UTF-8"))), is(1003044.d)); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDoubleFromByteArrayContainingUtf8EncodingOfStringWithContentsOtherThanDouble() throws Exception { factory.create("something".getBytes("UTF-8")); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDoubleFromInputStreamContainingUtf8EncodingOfStringWithContentsOtherThanDouble() throws Exception { factory.create(new ByteArrayInputStream("something".getBytes("UTF-8"))); }
@Test( expected = ValueFormatException.class ) public void shouldNotCreateDoubleFromReaderContainingStringWithContentsOtherThanDouble() throws Exception { factory.create(new ByteArrayInputStream("something".getBytes("UTF-8"))); }
@Test public void shouldCreateIteratorOverValuesWhenSuppliedIteratorOfUnknownObjects() { List<String> values = new ArrayList<String>(); for (int i = 0; i != 10; ++i) values.add("" + i); Iterator<Double> iter = factory.create(values.iterator()); Iterator<String> valueIter = values.iterator(); while (iter.hasNext()) { assertThat(iter.next(), is(factory.create(valueIter.next()))); } }