org.joda.time.base.BaseLocal Java Examples

The following examples show how to use org.joda.time.base.BaseLocal. 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: BaseLocalRelay.java    From jfixture with MIT License 6 votes vote down vote up
@Override
public Object create(Object request, SpecimenContext context) {

    if (!(request instanceof SpecimenType)) {
        return new NoSpecimen();
    }

    SpecimenType type = (SpecimenType) request;
    if (!BaseLocal.class.isAssignableFrom(type.getRawType())) {
        return new NoSpecimen();
    }

    Date date = (Date) context.resolve(Date.class);
    long instant = date.getTime();

    try {
        return type.getRawType().getDeclaredConstructor(long.class).newInstance(instant);
    } catch (Exception e) {
        e.printStackTrace();
        return new NoSpecimen();
    }
}
 
Example #2
Source File: StringToJodaTimeBaseLocalConverter.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return targetTypeToken.isSubtypeOf(BaseLocal.class) && stringToDateConverter.canHandle(source, TypeToken.of(Date.class));
}
 
Example #3
Source File: DateToJodaTimeBaseLocalConverter.java    From beanmother with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
    return targetTypeToken.isSubtypeOf(BaseLocal.class) && (source instanceof Date);
}
 
Example #4
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected StackManipulation convertDateTime(TypeDescriptor<?> type) {
  // The setter might be called with a different subclass of ReadableInstant than the one stored
  // in this POJO. We must extract the value passed into the setter and copy it into an instance
  // that the POJO can accept.

  // Generate the following code:
  //   return new T(value.getMillis());
  // Unless T is a sub-class of BaseLocal. Then generate:
  //   return new T(value.getMillis(), DateTimeZone.UTC);

  ForLoadedType loadedType = new ForLoadedType(type.getRawType());
  List<StackManipulation> stackManipulations = new ArrayList<>();

  // Create a new instance of the target type.
  stackManipulations.add(TypeCreation.of(loadedType));
  stackManipulations.add(Duplication.SINGLE);
  // Load the parameter and cast it to a ReadableInstant.
  stackManipulations.add(readValue);
  stackManipulations.add(TypeCasting.to(READABLE_INSTANT_TYPE));
  // Call ReadableInstant.getMillis to extract the millis since the epoch.
  stackManipulations.add(
      MethodInvocation.invoke(
          READABLE_INSTANT_TYPE
              .getDeclaredMethods()
              .filter(ElementMatchers.named("getMillis"))
              .getOnly()));

  if (type.isSubtypeOf(TypeDescriptor.of(BaseLocal.class))) {
    // Access DateTimeZone.UTC
    stackManipulations.add(
        FieldAccess.forField(
                DATE_TIME_ZONE_TYPE
                    .getDeclaredFields()
                    .filter(ElementMatchers.named("UTC"))
                    .getOnly())
            .read());
    // All subclasses of BaseLocal contain a ()(long, DateTimeZone) constructor
    // that takes in a millis and time zone argument. Call that constructor of the field to
    // initialize it.
    stackManipulations.add(
        MethodInvocation.invoke(
            loadedType
                .getDeclaredMethods()
                .filter(
                    ElementMatchers.isConstructor()
                        .and(
                            ElementMatchers.takesArguments(
                                ForLoadedType.of(long.class), DATE_TIME_ZONE_TYPE)))
                .getOnly()));
  } else {
    // All subclasses of ReadableInstant and ReadablePartial contain a ()(long) constructor
    // that takes in a millis argument. Call that constructor of the field to initialize it.
    stackManipulations.add(
        MethodInvocation.invoke(
            loadedType
                .getDeclaredMethods()
                .filter(
                    ElementMatchers.isConstructor()
                        .and(ElementMatchers.takesArguments(ForLoadedType.of(long.class))))
                .getOnly()));
  }

  StackManipulation stackManipulation = new Compound(stackManipulations);
  return new ShortCircuitReturnNull(readValue, stackManipulation);
}