Example usage for org.joda.time Period minus

List of usage examples for org.joda.time Period minus

Introduction

In this page you can find the example usage for org.joda.time Period minus.

Prototype

public Period minus(ReadablePeriod period) 

Source Link

Document

Returns a new period with the specified period subtracted.

Usage

From source file:org.graylog.plugins.pipelineprocessor.ast.expressions.AdditionExpression.java

License:Open Source License

@Nullable
@Override//ww w  .  j  ava 2s.c om
public Object evaluateUnsafe(EvaluationContext context) {
    final Object leftValue = left.evaluateUnsafe(context);
    final Object rightValue = right.evaluateUnsafe(context);

    // special case for date arithmetic
    final boolean leftDate = DateTime.class.equals(leftValue.getClass());
    final boolean leftPeriod = Period.class.equals(leftValue.getClass());
    final boolean rightDate = DateTime.class.equals(rightValue.getClass());
    final boolean rightPeriod = Period.class.equals(rightValue.getClass());

    if (leftDate && rightPeriod) {
        final DateTime date = (DateTime) leftValue;
        final Period period = (Period) rightValue;

        return isPlus() ? date.plus(period) : date.minus(period);
    } else if (leftPeriod && rightDate) {
        final DateTime date = (DateTime) rightValue;
        final Period period = (Period) leftValue;

        return isPlus() ? date.plus(period) : date.minus(period);
    } else if (leftPeriod && rightPeriod) {
        final Period period1 = (Period) leftValue;
        final Period period2 = (Period) rightValue;

        return isPlus() ? period1.plus(period2) : period1.minus(period2);
    } else if (leftDate && rightDate) {
        // the most uncommon, this is only defined for - really and means "interval between them"
        // because adding two dates makes no sense
        if (isPlus()) {
            // makes no sense to compute and should be handles in the parser already
            return null;
        }
        final DateTime left = (DateTime) leftValue;
        final DateTime right = (DateTime) rightValue;

        if (left.isBefore(right)) {
            return new Duration(left, right);
        } else {
            return new Duration(right, left);
        }
    }
    if (isIntegral()) {
        final long l = (long) leftValue;
        final long r = (long) rightValue;
        if (isPlus) {
            return l + r;
        } else {
            return l - r;
        }
    } else {
        final double l = (double) leftValue;
        final double r = (double) rightValue;
        if (isPlus) {
            return l + r;
        } else {
            return l - r;
        }
    }
}