From 59f48bed938d941e843e796559ff026e52a6a241 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sun, 3 Nov 2019 12:19:01 +0100 Subject: Implement duration TODOs --- .../edu/mimuw/cloudatlas/model/ValueDuration.java | 143 +++++++++++++++++---- 1 file changed, 121 insertions(+), 22 deletions(-) (limited to 'src/pl/edu/mimuw/cloudatlas/model') diff --git a/src/pl/edu/mimuw/cloudatlas/model/ValueDuration.java b/src/pl/edu/mimuw/cloudatlas/model/ValueDuration.java index c111d09..bf9d0e0 100644 --- a/src/pl/edu/mimuw/cloudatlas/model/ValueDuration.java +++ b/src/pl/edu/mimuw/cloudatlas/model/ValueDuration.java @@ -24,12 +24,28 @@ package pl.edu.mimuw.cloudatlas.model; +import java.lang.IllegalArgumentException; +import java.text.ParseException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * A class representing duration in milliseconds. The duration can be negative. This is a simple wrapper of a Java * Long object. */ public class ValueDuration extends ValueSimple { + public static class InvalidFormatException extends IllegalArgumentException { + } + + /** + * Regex pattern for duration strings. + */ + private static final Pattern DURATION_PATTERN = Pattern.compile( + "(?[+-])(?\\d+) " + + "(?[0-1][0-9]|2[0-3]):(?[0-5][0-9]):(?[0-5][0-9])." + + "(?[0-9]{3})" + ); + /** * Constructs a new ValueDuration object wrapping the specified value. * @@ -41,14 +57,12 @@ public class ValueDuration extends ValueSimple { @Override public Type getType() { - // TODO - throw new UnsupportedOperationException("Not yet implemented"); + return TypePrimitive.DURATION; } @Override public Value getDefaultValue() { - // TODO - throw new UnsupportedOperationException("Not yet implemented"); + return new ValueDuration(0l); } /** @@ -119,55 +133,140 @@ public class ValueDuration extends ValueSimple { } private static long parseDuration(String value) { - // TODO - throw new UnsupportedOperationException("Not yet implemented"); + Matcher matcher = DURATION_PATTERN.matcher(value); + if (!matcher.matches()) { + throw new InvalidFormatException(); + } + + long result = parseLongGroup(matcher, "days"); + result *= 24; + result += parseLongGroup(matcher, "hours"); + result *= 60; + result += parseLongGroup(matcher, "minutes"); + result *= 60; + result += parseLongGroup(matcher, "seconds"); + result *= 1000; + result += parseLongGroup(matcher, "milliseconds"); + result *= matcher.group("sign").equals("+") ? 1 : -1; + + return result; + } + + private static long parseLongGroup(Matcher matcher, String group) { + return Long.parseLong(matcher.group(group)); } @Override public ValueBoolean isLowerThan(Value value) { - // TODO - throw new UnsupportedOperationException("Not yet implemented"); + sameTypesOrThrow(value, Operation.COMPARE); + if(isNull() || value.isNull()) + return new ValueBoolean(null); + return new ValueBoolean(getValue() < ((ValueDuration)value).getValue()); } @Override public ValueDuration addValue(Value value) { - // TODO - throw new UnsupportedOperationException("Not yet implemented"); + sameTypesOrThrow(value, Operation.ADD); + if (isNull() || value.isNull()) { + return new ValueDuration((Long) null); + } + + return new ValueDuration(getValue() + ((ValueDuration)value).getValue()); } @Override public ValueDuration subtract(Value value) { - // TODO - throw new UnsupportedOperationException("Not yet implemented"); + sameTypesOrThrow(value, Operation.SUBTRACT); + if (isNull() || value.isNull()) + return new ValueDuration((Long) null); + return new ValueDuration(getValue() - ((ValueDuration)value).getValue()); } @Override public ValueDuration multiply(Value value) { - // TODO - throw new UnsupportedOperationException("Not yet implemented"); + if (!value.getType().isCompatible(TypePrimitive.INTEGER)) { + throw new IncompatibleTypesException(getType(), value.getType(), Operation.MULTIPLY); + } + + if (isNull() || value.isNull()) { + return new ValueDuration((Long) null); + } + + return new ValueDuration(getValue() * ((ValueInt)value).getValue()); } @Override public Value divide(Value value) { - // TODO - throw new UnsupportedOperationException("Not yet implemented"); + if (!value.getType().isCompatible(TypePrimitive.INTEGER)) { + throw new IncompatibleTypesException(getType(), value.getType(), Operation.MULTIPLY); + } + + if (isNull() || value.isNull()) { + return new ValueDuration((Long) null); + } + + if (((ValueInt)value).getValue() == 0l) { + throw new ArithmeticException("Division by zero."); + } + + return new ValueDuration(getValue() / ((ValueInt)value).getValue()); } @Override public ValueDuration modulo(Value value) { - // TODO - throw new UnsupportedOperationException("Not yet implemented"); + if (!value.getType().isCompatible(TypePrimitive.INTEGER)) { + throw new IncompatibleTypesException(getType(), value.getType(), Operation.MULTIPLY); + } + + if (isNull() || value.isNull()) { + return new ValueDuration((Long) null); + } + + if (((ValueInt)value).getValue() == 0l) { + throw new ArithmeticException("Division by zero."); + } + + return new ValueDuration(getValue() % ((ValueInt)value).getValue()); } @Override public ValueDuration negate() { - // TODO - throw new UnsupportedOperationException("Not yet implemented"); + if(isNull()) { + return new ValueDuration((Long) null); + } + + return new ValueDuration(-getValue()); + } + + public String toString() { + long remainingUnits = getValue(); + boolean positive = remainingUnits >= 0; + remainingUnits = positive ? remainingUnits : -remainingUnits; + + long milliseconds = remainingUnits % 1000; + remainingUnits /= 1000; + long seconds = remainingUnits % 60; + remainingUnits /= 60; + long minutes = remainingUnits % 60; + remainingUnits /= 60; + long hours = remainingUnits % 24; + remainingUnits /= 24; + long days = remainingUnits; + + return (positive ? "+" : "-") + Long.toString(days) + " " + Long.toString(hours) + + ":" + Long.toString(minutes) + ":" + Long.toString(seconds) + "." + + Long.toString(milliseconds); } @Override public Value convertTo(Type type) { - // TODO - throw new UnsupportedOperationException("Not yet implemented"); + switch(type.getPrimaryType()) { + case STRING: + return getValue() == null? ValueString.NULL_STRING : new ValueString(toString()); + case DURATION: + return this; + default: + throw new UnsupportedConversionException(getType(), type); + } } } -- cgit v1.2.3