@Override public boolean applies(MappingContext<S, D> context) { return !Objects.equal(context.getSource(), Defaults.defaultValue(context.getSourceType())); } }
@Override public Object convert(MappingContext<Temporal, Object> mappingContext) { LocalDateTime source = (LocalDateTime) mappingContext.getSource(); return convertLocalDateTime(source, mappingContext); } }
@Override public Object convert(MappingContext<Temporal, Object> mappingContext) { Instant source = (Instant) mappingContext.getSource(); return convertInstant(source, mappingContext); } }
public String convert(MappingContext<Object, String> context) { Object source = context.getSource(); if (source == null) return null; Class<?> sourceType = context.getSourceType(); return sourceType.isArray() && sourceType.getComponentType() == Character.TYPE || sourceType.getComponentType() == Character.class ? String.valueOf((char[]) source) : source.toString(); }
public boolean applies(MappingContext<Object, Object> context) { return context.getSource() == null; }
@Override public Temporal convert(MappingContext<Temporal, Temporal> mappingContext) { if (mappingContext.getSource() == null) return null; else if (mappingContext.getSourceType().equals(mappingContext.getDestinationType())) return mappingContext.getSource(); else throw new Errors().addMessage("Unsupported mapping types[%s->%s]", mappingContext.getSourceType().getName(), mappingContext.getDestinationType()) .toMappingException(); } }
public boolean applies(MappingContext<Object, Object> context) { return context.getSource() != null; }
public Character convert(MappingContext<Object, Character> context) { Object source = context.getSource(); if (source == null) return null; String stringValue = source.toString(); if (stringValue.length() == 0) return null; return Character.valueOf(stringValue.charAt(0)); }
/** * Delegates conversion to {@link #convert(Object)}. */ public D convert(MappingContext<S, D> context) { return convert(context.getSource()); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public Enum<?> convert(MappingContext<Object, Enum<?>> context) { Object source = context.getSource(); if (source == null) return null; String name = source.getClass() == String.class ? (String) source : ((Enum<?>) source).name(); if (name != null) try { return Enum.valueOf((Class) context.getDestinationType(), name); } catch (IllegalArgumentException ignore) { } return null; }
@Override public Object convert(MappingContext<Optional<Object>, Object> mappingContext) { if (mappingContext.getSource() == null || !mappingContext.getSource().isPresent()) { return null; } MappingContext<Object, Object> propertyContext = mappingContext.create( mappingContext.getSource().get(), mappingContext.getDestinationType()); return mappingContext.getMappingEngine().map(propertyContext); } }
@Override public Object convert(MappingContext<Temporal, Object> mappingContext) { LocalDate source = (LocalDate) mappingContext.getSource(); Class<?> destinationType = mappingContext.getDestinationType(); if (destinationType.equals(String.class)) return DateTimeFormatter.ofPattern(config.getDatePattern()) .format(source); LocalDateTime localDateTime = source.atStartOfDay(); return convertLocalDateTime(localDateTime, mappingContext); } }
private LocalDate convertLocalDate(MappingContext<?, ?> mappingContext) { Object source = mappingContext.getSource(); Class<?> sourceType = source.getClass(); if (sourceType.equals(String.class)) return LocalDate.parse((String) source, DateTimeFormatter.ofPattern(config.getDatePattern())); return convertInstant(mappingContext).atZone(config.getZoneId()).toLocalDate(); }
@Override public Optional<Object> convert(MappingContext<Object, Optional<Object>> mappingContext) { if (mappingContext.getSource() == null) { return Optional.empty(); } MappingContext<?, ?> propertyContext = mappingContext.create( mappingContext.getSource(), MappingContextHelper.resolveDestinationGenericType(mappingContext)); Object destination = mappingContext.getMappingEngine().map(propertyContext); return Optional.ofNullable(destination); } }
public Boolean convert(MappingContext<Object, Boolean> context) { Object source = context.getSource(); if (source == null) return null; String stringValue = source.toString().toLowerCase(); if (stringValue.length() == 0) return null; for (int i = 0; i < TRUE_STRINGS.length; i++) if (TRUE_STRINGS[i].equals(stringValue)) return Boolean.TRUE; for (int i = 0; i < FALSE_STRINGSS.length; i++) if (FALSE_STRINGSS[i].equals(stringValue)) return Boolean.FALSE; throw new Errors().errorMapping(context.getSource(), context.getDestinationType()) .toMappingException(); }
private LocalDateTime convertLocalDateTime(MappingContext<?, ?> mappingContext) { Object source = mappingContext.getSource(); Class<?> sourceType = source.getClass(); if (sourceType.equals(String.class)) return LocalDateTime.parse((String) source, DateTimeFormatter.ofPattern(config.getDateTimePattern())); return convertInstant(mappingContext).atZone(config.getZoneId()).toLocalDateTime(); }
public Number convert(MappingContext<Object, Number> context) { Object source = context.getSource(); if (source == null) return null; Class<?> destinationType = Primitives.wrapperFor(context.getDestinationType()); if (source instanceof Number) return numberFor((Number) source, destinationType); if (source instanceof Boolean) return numberFor(((Boolean) source).booleanValue() ? 1 : 0, destinationType); if (source instanceof Date && Long.class.equals(destinationType)) return Long.valueOf(((Date) source).getTime()); if (source instanceof Calendar && Long.class.equals(destinationType)) return Long.valueOf(((Calendar) source).getTime().getTime()); if (source instanceof XMLGregorianCalendar && Long.class.equals(destinationType)) return ((XMLGregorianCalendar) source).toGregorianCalendar().getTimeInMillis(); return numberFor(source.toString(), destinationType); }
@SuppressWarnings("all") private Object createDestination(MappingContext<Object, Object> context) { int sourceLength = Iterables.getLength(context.getSource()); int destinationLength = context.getDestination() != null ? Iterables.getLength(context.getDestination()) : 0; int newLength = Math.max(sourceLength, destinationLength); Object originalDestination = context.getDestination(); Class<?> destType = context.getDestinationType(); Object destination = Array.newInstance(destType.isArray() ? destType.getComponentType() : destType, newLength); if (originalDestination != null) System.arraycopy(originalDestination, 0, destination, 0, destinationLength); return destination; }
public Date convert(MappingContext<Object, Date> context) { Object source = context.getSource(); if (source == null) return null; Class<?> destinationType = context.getDestinationType(); if (source instanceof Date) return dateFor(((Date) source).getTime(), destinationType); if (source instanceof Calendar) return dateFor(((Calendar) source).getTimeInMillis(), destinationType); if (source instanceof XMLGregorianCalendar) return dateFor(((XMLGregorianCalendar) source).toGregorianCalendar().getTimeInMillis(), destinationType); if (source instanceof Long) return dateFor(((Long) source).longValue(), destinationType); return dateFor(source.toString(), context.getDestinationType()); }
public Object convert(MappingContext<Object, Object> context) { MappingContextImpl<Object, Object> contextImpl = (MappingContextImpl<Object, Object>) context; return (!contextImpl.isProvidedDestination() && context.getDestination() != null) ? context.getDestination() : context.getSource(); }