Java 类org.joda.time.base.BaseInterval 实例源码

项目:jfixture    文件:BaseIntervalRelay.java   
@Override
public Object create(Object request, SpecimenContext context) {

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

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

    DateTime dateA = (DateTime) context.resolve(DateTime.class);
    DateTime dateB = (DateTime) context.resolve(DateTime.class);

    try {
        Constructor constructor = type.getRawType().getDeclaredConstructor(ReadableInstant.class, ReadableInstant.class);

        Object specimen;
        if (dateA.isBefore(dateB))
            specimen = constructor.newInstance(dateA, dateB);
        else
            specimen = constructor.newInstance(dateB, dateA);

        return specimen;

    } catch (Exception e) {
        e.printStackTrace();
        return new NoSpecimen();
    }
}