class CalendarConverter implements ConditionalConverter<Object, Object> {
  private static DatatypeFactory dataTypeFactory;

  private static DatatypeFactory getDataTypeFactory() {
    if (dataTypeFactory == null) {
      try {
        dataTypeFactory = DatatypeFactory.newInstance();
      } catch (DatatypeConfigurationException e) {
        throw new Errors().addMessage(e,
            "Failed to create DataTypeFactory required for XMLGregorianCalendar conversion")
            .toMappingException();
      }
    }

    return dataTypeFactory;
  }

  public Object convert(MappingContext<Object, Object> context) {<FILL_FUNCTION_BODY>}

  public MatchResult match(Class<?> sourceType, Class<?> destinationType) {
    return (Calendar.class.isAssignableFrom(destinationType) || destinationType == XMLGregorianCalendar.class)
        && (Date.class.isAssignableFrom(sourceType) || Calendar.class.isAssignableFrom(sourceType)
            || sourceType == XMLGregorianCalendar.class || sourceType == Long.class || sourceType == Long.TYPE) ? MatchResult.FULL
        : MatchResult.NONE;
  }
}

    Object source = context.getSource();
    if (source == null)
      return null;

    Class<?> destinationType = context.getDestinationType();
    if (!Calendar.class.isAssignableFrom(destinationType)
        && !destinationType.equals(XMLGregorianCalendar.class))
      throw new Errors().errorMapping(source, destinationType).toMappingException();

    GregorianCalendar calendar = new GregorianCalendar();

    if (source instanceof Date)
      calendar.setTimeInMillis(((Date) source).getTime());
    else if (source instanceof Calendar) {
      Calendar cal = (Calendar) source;
      calendar.setTimeZone(cal.getTimeZone());
      calendar.setTimeInMillis(cal.getTime().getTime());
    } else if (source instanceof XMLGregorianCalendar) {
      XMLGregorianCalendar xmlCal = (XMLGregorianCalendar) source;
      GregorianCalendar cal = xmlCal.toGregorianCalendar();
      calendar.setTimeZone(cal.getTimeZone());
      calendar.setTimeInMillis(cal.getTime().getTime());
    } else if (source instanceof Long)
      calendar.setTimeInMillis(((Long) source).longValue());

    return destinationType.equals(XMLGregorianCalendar.class) ? getDataTypeFactory().newXMLGregorianCalendar(
        calendar)
        : calendar;
  
<no_super_class>