org.joda.convert.StringConvert Java Examples

The following examples show how to use org.joda.convert.StringConvert. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExplainMap.java    From Strata with Apache License 2.0 6 votes vote down vote up
private void explanationString(StringBuilder buf, String indent) {
  buf.append("{").append(System.lineSeparator());
  String entryIndent = indent + "  ";
  for (Entry<ExplainKey<?>, Object> entry : map.entrySet()) {
    buf.append(entryIndent).append(entry.getKey()).append(" = ");
    if (entry.getValue() instanceof List) {
      // list
      @SuppressWarnings("unchecked")
      List<ExplainMap> list = (List<ExplainMap>) entry.getValue();
      explanationString(buf, entryIndent, list);
    } else {
      // single entry
      try {
        buf.append(StringConvert.INSTANCE.convertToString(entry.getValue()));
      } catch (Exception ex) {
        buf.append(entry.getValue());
      }
    }
    buf.append(',').append(System.lineSeparator());
  }
  if (!map.entrySet().isEmpty()) {
    buf.deleteCharAt(buf.lastIndexOf(","));
  }
  buf.append(indent).append("}");
}
 
Example #2
Source File: AttributeType.java    From Strata with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private AttributeType(String name, Class<T> type) {
  this.name = ArgChecker.notEmpty(name, "name");
  this.type = ArgChecker.notNull(type, "type");
  if (StringConvert.INSTANCE.isConvertible(type)) {
    TypedStringConverter<T> converter = StringConvert.INSTANCE.findTypedConverter(type);
    this.toStoredFormConverter = value -> converter.convertToString(value);
    this.fromStoredFormConverter =
        value -> value instanceof String ?
            converter.convertFromString(type, (String) value) :
            converter.convertFromString(type, StringConvert.INSTANCE.convertToString(value));
  } else {
    this.toStoredFormConverter = value -> type.cast(value);
    this.fromStoredFormConverter = value -> type.cast(value);
  }
}
 
Example #3
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public void testDateMidnight() {
    DateMidnight test = new DateMidnight(2010, 6, 30, ISOChronology.getInstance(ZONE));
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("2010-06-30T00:00:00.000+02:00", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(DateMidnight.class, str));
}
 
Example #4
Source File: TestHelper.java    From Strata with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that the object can be serialized and deserialized via a string using Joda-Convert.
 * 
 * @param <T>  the type
 * @param cls  the effective type
 * @param base  the object to be tested
 */
public static <T> void assertJodaConvert(Class<T> cls, Object base) {
  assertNotNull(base, "assertJodaConvert() called with null Class");
  assertNotNull(base, "assertJodaConvert() called with null Object");
  StringConvert convert = StringConvert.create();
  String str = convert.convertToString(base);
  T result = convert.convertFromString(cls, str);
  assertEquals(result, base, "Result from roundtrip not equal to base object");
}
 
Example #5
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testMonths() {
    Months test = Months.months(5);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("P5M", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(Months.class, str));
}
 
Example #6
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testLocalDate() {
    LocalDate test = new LocalDate(2010, 6, 30);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("2010-06-30", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(LocalDate.class, str));
}
 
Example #7
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testLocalTime() {
    LocalTime test = new LocalTime(2, 30, 50, 678);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("02:30:50.678", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(LocalTime.class, str));
}
 
Example #8
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testYearMonth() {
    YearMonth test = new YearMonth(2010, 6);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("2010-06", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(YearMonth.class, str));
}
 
Example #9
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testMonthDay() {
    MonthDay test = new MonthDay(6, 30);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("--06-30", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(MonthDay.class, str));
}
 
Example #10
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testMonthDay_leapDay() {
    MonthDay test = new MonthDay(2, 29);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("--02-29", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(MonthDay.class, str));
}
 
Example #11
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testTimeZone() {
    DateTimeZone test = DateTimeZone.forID("Europe/Paris");
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("Europe/Paris", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(DateTimeZone.class, str));
}
 
Example #12
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testDuration() {
    Duration test = new Duration(12345678L);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("PT12345.678S", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(Duration.class, str));
}
 
Example #13
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testPeriod() {
    Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("P1Y2M3W4DT5H6M7.008S", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(Period.class, str));
}
 
Example #14
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testMutablePeriod() {
    MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("P1Y2M3W4DT5H6M7.008S", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(MutablePeriod.class, str));
}
 
Example #15
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testYears() {
    Years test = Years.years(5);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("P5Y", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(Years.class, str));
}
 
Example #16
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testLocalDateTime() {
    LocalDateTime test = new LocalDateTime(2010, 6, 30, 2, 30);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("2010-06-30T02:30:00.000", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(LocalDateTime.class, str));
}
 
Example #17
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testWeeks() {
    Weeks test = Weeks.weeks(5);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("P5W", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(Weeks.class, str));
}
 
Example #18
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testDays() {
    Days test = Days.days(5);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("P5D", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(Days.class, str));
}
 
Example #19
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testHours() {
    Hours test = Hours.hours(5);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("PT5H", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(Hours.class, str));
}
 
Example #20
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testMinutes() {
    Minutes test = Minutes.minutes(5);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("PT5M", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(Minutes.class, str));
}
 
Example #21
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testSeconds() {
    Seconds test = Seconds.seconds(5);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("PT5S", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(Seconds.class, str));
}
 
Example #22
Source File: MeasureTest.java    From Strata with Apache License 2.0 4 votes vote down vote up
@Test
public void test_isConvertible() {
  assertThat(StringConvert.INSTANCE.isConvertible(Measure.class)).isTrue();
}
 
Example #23
Source File: JodaConvertStringBindingTest.java    From jadira with Apache License 2.0 4 votes vote down vote up
@Test
public void testUnmarshalUsingJoda() throws SecurityException, NoSuchMethodException {
    
    JodaConvertStringBinding<SubjectJodaConvert> unmarshaller = new JodaConvertStringBinding<SubjectJodaConvert>(SubjectJodaConvert.class, StringConvert.INSTANCE.findConverter(SubjectJodaConvert.class));
    assertEquals(new SubjectJodaConvert("UNMARSHALLED_BY_JODA"), unmarshaller.unmarshal("UNMARSHALLED_BY_JODA:MARSHALLED_BY_JODA"));
}
 
Example #24
Source File: JodaConvertStringBindingTest.java    From jadira with Apache License 2.0 4 votes vote down vote up
@Test
public void testMarshalUsingJoda() throws SecurityException, NoSuchMethodException {
    
    JodaConvertStringBinding<SubjectJodaConvert> marshaller = new JodaConvertStringBinding<SubjectJodaConvert>(SubjectJodaConvert.class, StringConvert.INSTANCE.findConverter(SubjectJodaConvert.class));
    assertEquals("1:MARSHALLED_BY_JODA", marshaller.marshal(new SubjectJodaConvert("1")));
}
 
Example #25
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testPeriod() {
    Period test = new Period(1, 2, 3, 4, 5, 6, 7, 8);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("P1Y2M3W4DT5H6M7.008S", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(Period.class, str));
}
 
Example #26
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testDateTime() {
    DateTime test = new DateTime(2010, 6, 30, 2, 30, 50, 678, ISOChronology.getInstance(ZONE));
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("2010-06-30T02:30:50.678+02:00", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(DateTime.class, str));
}
 
Example #27
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testMutableDateTime() {
    MutableDateTime test = new MutableDateTime(2010, 6, 30, 2, 30, 50, 678, ISOChronology.getInstance(ZONE));
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("2010-06-30T02:30:50.678+02:00", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(MutableDateTime.class, str));
}
 
Example #28
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testLocalDateTime() {
    LocalDateTime test = new LocalDateTime(2010, 6, 30, 2, 30);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("2010-06-30T02:30:00.000", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(LocalDateTime.class, str));
}
 
Example #29
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testLocalDate() {
    LocalDate test = new LocalDate(2010, 6, 30);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("2010-06-30", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(LocalDate.class, str));
}
 
Example #30
Source File: TestStringConvert.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testLocalTime() {
    LocalTime test = new LocalTime(2, 30, 50, 678);
    String str = StringConvert.INSTANCE.convertToString(test);
    assertEquals("02:30:50.678", str);
    assertEquals(test, StringConvert.INSTANCE.convertFromString(LocalTime.class, str));
}