[New] TimeValue and its implementations are now Comparable

This commit is contained in:
Robert von Burg 2019-10-10 10:20:40 +02:00
parent ff434cb285
commit fedc2824f6
8 changed files with 47 additions and 3 deletions

View File

@ -24,7 +24,7 @@ package li.strolch.model.timevalue;
* *
* @author Martin Smock <smock.martin@gmail.com> * @author Martin Smock <smock.martin@gmail.com>
*/ */
public interface IValue<T> { public interface IValue<T> extends Comparable<IValue<T>> {
/** /**
* @return the type of this {@link IValue} * @return the type of this {@link IValue}

View File

@ -115,4 +115,9 @@ public class BooleanValue implements IValue<Boolean>, Serializable {
} }
return true; return true;
} }
@Override
public int compareTo(IValue<Boolean> o) {
return Boolean.compare(this.value, o.getValue());
}
} }

View File

@ -153,4 +153,11 @@ public class FloatListValue implements IValue<List<Double>>, Serializable {
return true; return true;
} }
@Override
public int compareTo(IValue<List<Double>> o) {
List<Double> otherValues = o.getValue();
if (this.value.equals(otherValues))
return 0;
return Integer.compare(this.value.size(), otherValues.size());
}
} }

View File

@ -125,4 +125,8 @@ public class FloatValue implements IValue<Double>, Serializable {
return true; return true;
} }
@Override
public int compareTo(IValue<Double> o) {
return Double.compare(this.value, o.getValue());
}
} }

View File

@ -116,4 +116,8 @@ public class IntegerValue implements IValue<Integer>, Serializable {
return true; return true;
} }
@Override
public int compareTo(IValue<Integer> o) {
return Integer.compare(this.value, o.getValue());
}
} }

View File

@ -116,4 +116,8 @@ public class LongValue implements IValue<Long>, Serializable {
return true; return true;
} }
@Override
public int compareTo(IValue<Long> o) {
return Long.compare(this.value, o.getValue());
}
} }

View File

@ -141,4 +141,12 @@ public class StringSetValue implements IValue<Set<AString>>, Serializable {
sb.append("]"); sb.append("]");
return sb.toString(); return sb.toString();
} }
@Override
public int compareTo(IValue<Set<AString>> o) {
Set<AString> otherValues = o.getValue();
if (this.aStrings.equals(otherValues))
return 0;
return Integer.compare(this.aStrings.size(), otherValues.size());
}
} }

View File

@ -31,7 +31,9 @@ public class TimeValue<T extends IValue> implements ITimeValue<T>, Serializable
/** /**
* @param time * @param time
* the time for this value
* @param value * @param value
* the actual value for this value
*/ */
public TimeValue(final Long time, final T value) { public TimeValue(final Long time, final T value) {
this.time = time; this.time = time;
@ -41,7 +43,7 @@ public class TimeValue<T extends IValue> implements ITimeValue<T>, Serializable
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T getValue() { public T getValue() {
return (T) this.value.getCopy(); return this.value == null ? null : (T) this.value.getCopy();
} }
@Override @Override
@ -62,9 +64,19 @@ public class TimeValue<T extends IValue> implements ITimeValue<T>, Serializable
return this; return this;
} }
@SuppressWarnings("unchecked")
@Override @Override
public int compareTo(final ITimeValue<T> arg0) { public int compareTo(final ITimeValue<T> arg0) {
return this.getTime().compareTo(arg0.getTime()); int i = getTime().compareTo(arg0.getTime());
if (i != 0)
return i;
if (this.value == null && arg0.getValue() == null)
return 0;
if (this.value == null)
return -1;
if (arg0.getValue() == null)
return 1;
return getValue().compareTo(arg0.getValue());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")