[Major] refactored compareTo()-method on StrolchElements

Parameters are now comparable, but remove from some others like the
timed states
This commit is contained in:
Robert von Burg 2015-07-29 18:54:42 +02:00
parent 59b86bc060
commit 4b96679588
19 changed files with 360 additions and 17 deletions

View File

@ -141,11 +141,6 @@ public abstract class AbstractStrolchElement implements StrolchElement {
return true;
}
@Override
public int compareTo(StrolchElement o) {
return getId().compareTo(o.getId());
}
@Override
public abstract String toString();
}

View File

@ -31,7 +31,7 @@ import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class Order extends GroupedParameterizedElement implements StrolchRootElement {
public class Order extends GroupedParameterizedElement implements StrolchRootElement, Comparable<Order> {
private static final long serialVersionUID = 0L;
@ -169,4 +169,9 @@ public class Order extends GroupedParameterizedElement implements StrolchRootEle
return builder.toString();
}
@Override
public int compareTo(Order o) {
return getId().compareTo(o.getId());
}
}

View File

@ -31,7 +31,7 @@ import li.strolch.model.visitor.StrolchRootElementVisitor;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class Resource extends GroupedParameterizedElement implements StrolchRootElement {
public class Resource extends GroupedParameterizedElement implements StrolchRootElement, Comparable<Resource> {
private static final long serialVersionUID = 0L;
@ -165,4 +165,9 @@ public class Resource extends GroupedParameterizedElement implements StrolchRoot
return builder.toString();
}
@Override
public int compareTo(Resource o) {
return getId().compareTo(o.getId());
}
}

View File

@ -20,7 +20,7 @@ import java.io.Serializable;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public interface StrolchElement extends Serializable, Comparable<StrolchElement> {
public interface StrolchElement extends Serializable {
/**
* Return the {@link Locator} for this element
@ -63,42 +63,84 @@ public interface StrolchElement extends Serializable, Comparable<StrolchElement>
* Set the currently set long value which defines the primary key for use in RDBM-Systems
*
* @param dbid
* the dbid to set
*/
public void setDbid(long dbid);
/**
* Returns the currently set long value which defines the primary key for use in RDBM-Systems
*
* @return
* @return the currently set long value which defines the primary key for use in RDBM-Systems
*/
public long getDbid();
/**
* Returns the type of this {@link StrolchElement}
*
* @return
* @return the type of this {@link StrolchElement}
*/
public String getType();
/**
* @return the direct parent of this element
*/
public StrolchElement getParent();
/**
* Returns the {@link StrolchRootElement} for this {@link StrolchElement}
*
* @return the {@link StrolchRootElement} for this {@link StrolchElement}
*/
public StrolchRootElement getRootElement();
/**
* Returns true if this element is a {@link StrolchRootElement}, false if not
*
* @return true if this element is a {@link StrolchRootElement}, false if not
*/
public boolean isRootElement();
/**
* Return a clone of this {@link StrolchElement}
*
* @return
* @return a clone of this {@link StrolchElement}
*/
public StrolchElement getClone();
/**
* <p>
* Returns the hashcode of this element.
* </p>
*
* <p>
* For most {@link StrolchElement} the equals and hashcode methods would only take into account the ID of the
* element, but certain implementations might require more specific attributes
* </p>
*
* @see Object#hashCode()
*
* @return the hashcode of this element
*/
@Override
public int hashCode();
/**
* <p>
* Returns true if this object equals the given parameter object
* </p>
*
* <p>
* For most {@link StrolchElement} the equals and hashcode methods would only take into account the ID of the
* element, but certain implementations might require more specific attributes
* </p>
*
* @see Object#equals(Object)
*
* @param obj
* the object to which to check for equality
*
* @return true if this object equals the given parameter object
*/
@Override
public boolean equals(Object obj);
@Override
public int compareTo(StrolchElement o);
}

View File

@ -38,7 +38,8 @@ import ch.eitchnet.utils.dbc.DBC;
*
* @author Martin Smock <martin.smock@bluewin.ch>
*/
public class Activity extends GroupedParameterizedElement implements IActivityElement, StrolchRootElement {
public class Activity extends GroupedParameterizedElement implements IActivityElement, StrolchRootElement,
Comparable<Activity> {
private static final long serialVersionUID = 1L;
@ -243,6 +244,11 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
return builder.toString();
}
@Override
public int compareTo(Activity o) {
return getId().compareTo(o.getId());
}
@Override
public <T> T accept(StrolchRootElementVisitor<T> visitor) {
throw new StrolchException("not implemented yet");

View File

@ -196,4 +196,7 @@ public abstract class AbstractParameter<T> extends AbstractStrolchElement implem
return builder.toString();
}
@Override
public abstract int compareTo(Parameter<?> otherParam);
}

View File

@ -17,6 +17,7 @@ package li.strolch.model.parameter;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -92,4 +93,10 @@ public class BooleanParameter extends AbstractParameter<Boolean> {
public static Boolean parseFromString(String valueS) {
return StringHelper.parseBoolean(valueS);
}
@Override
public int compareTo(Parameter<?> o) {
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return this.getValue().compareTo(((BooleanParameter) o).getValue());
}
}

View File

@ -19,6 +19,7 @@ import java.util.Date;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
@ -94,4 +95,10 @@ public class DateParameter extends AbstractParameter<Date> {
public static Date parseFromString(String valueS) {
return ISO8601FormatFactory.getInstance().getDateFormat().parse(valueS);
}
@Override
public int compareTo(Parameter<?> o) {
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return this.getValue().compareTo(((DateParameter) o).getValue());
}
}

View File

@ -17,6 +17,7 @@ package li.strolch.model.parameter;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
@ -92,4 +93,10 @@ public class DurationParameter extends AbstractParameter<Long> {
public static Long parseFromString(String valueS) {
return ISO8601FormatFactory.getInstance().getDurationFormat().parse(valueS);
}
@Override
public int compareTo(Parameter<?> o) {
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return this.getValue().compareTo(((DurationParameter) o).getValue());
}
}

View File

@ -22,6 +22,7 @@ import java.util.List;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -146,4 +147,10 @@ public class FloatListParameter extends AbstractParameter<List<Double>> implemen
}
return values;
}
@Override
public int compareTo(Parameter<?> o) {
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return Integer.valueOf(this.getValue().size()).compareTo(((FloatListParameter) o).getValue().size());
}
}

View File

@ -15,6 +15,7 @@
*/
package li.strolch.model.parameter;
import ch.eitchnet.utils.dbc.DBC;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
@ -93,4 +94,10 @@ public class FloatParameter extends AbstractParameter<Double> {
public static Double parseFromString(String valueS) {
return Double.valueOf(valueS);
}
@Override
public int compareTo(Parameter<?> o) {
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return this.getValue().compareTo(((FloatParameter) o).getValue());
}
}

View File

@ -22,6 +22,7 @@ import java.util.List;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -146,4 +147,10 @@ public class IntegerListParameter extends AbstractParameter<List<Integer>> imple
}
return values;
}
@Override
public int compareTo(Parameter<?> o) {
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return Integer.valueOf(this.getValue().size()).compareTo(((IntegerListParameter) o).getValue().size());
}
}

View File

@ -15,6 +15,7 @@
*/
package li.strolch.model.parameter;
import ch.eitchnet.utils.dbc.DBC;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
@ -92,4 +93,10 @@ public class IntegerParameter extends AbstractParameter<Integer> {
public static Integer parseFromString(String valueS) {
return Integer.valueOf(valueS);
}
@Override
public int compareTo(Parameter<?> o) {
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return this.getValue().compareTo(((IntegerParameter) o).getValue());
}
}

View File

@ -22,6 +22,7 @@ import java.util.List;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -146,4 +147,10 @@ public class LongListParameter extends AbstractParameter<List<Long>> implements
}
return values;
}
@Override
public int compareTo(Parameter<?> o) {
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return Integer.valueOf(this.getValue().size()).compareTo(((LongListParameter) o).getValue().size());
}
}

View File

@ -15,6 +15,7 @@
*/
package li.strolch.model.parameter;
import ch.eitchnet.utils.dbc.DBC;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
@ -92,4 +93,10 @@ public class LongParameter extends AbstractParameter<Long> {
public static Long parseFromString(String valueS) {
return Long.valueOf(valueS);
}
@Override
public int compareTo(Parameter<?> o) {
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return this.getValue().compareTo(((LongParameter) o).getValue());
}
}

View File

@ -23,7 +23,7 @@ import li.strolch.model.visitor.ParameterVisitor;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public interface Parameter<T> extends StrolchElement {
public interface Parameter<T> extends StrolchElement, Comparable<Parameter<?>> {
/**
* Returns the value of the parameter as string
@ -141,9 +141,12 @@ public interface Parameter<T> extends StrolchElement {
@Override
public boolean equals(Object obj);
@Override
public int compareTo(Parameter<?> o);
@Override
public Parameter<T> getClone();
public <U> U accept(ParameterVisitor visitor);
public <U> U accept(ParameterVisitor visitor);
}

View File

@ -22,6 +22,7 @@ import java.util.List;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -146,4 +147,10 @@ public class StringListParameter extends AbstractParameter<List<String>> impleme
}
return values;
}
@Override
public int compareTo(Parameter<?> o) {
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return Integer.valueOf(this.getValue().size()).compareTo(((StringListParameter) o).getValue().size());
}
}

View File

@ -17,6 +17,7 @@ package li.strolch.model.parameter;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import ch.eitchnet.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -90,4 +91,10 @@ public class StringParameter extends AbstractParameter<String> {
public <U> U accept(ParameterVisitor visitor) {
return visitor.visitStringParam(this);
}
@Override
public int compareTo(Parameter<?> o) {
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return this.getValue().compareToIgnoreCase(((StringParameter) o).getValue());
}
}

View File

@ -0,0 +1,207 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.model;
import static li.strolch.model.ModelGenerator.PARAM_BOOLEAN_ID;
import static li.strolch.model.ModelGenerator.PARAM_DATE_ID;
import static li.strolch.model.ModelGenerator.PARAM_FLOAT_ID;
import static li.strolch.model.ModelGenerator.PARAM_INTEGER_ID;
import static li.strolch.model.ModelGenerator.PARAM_LIST_FLOAT_ID;
import static li.strolch.model.ModelGenerator.PARAM_LIST_INTEGER_ID;
import static li.strolch.model.ModelGenerator.PARAM_LIST_LONG_ID;
import static li.strolch.model.ModelGenerator.PARAM_LIST_STRING_ID;
import static li.strolch.model.ModelGenerator.PARAM_LONG_ID;
import static li.strolch.model.ModelGenerator.PARAM_STRING_ID;
import static org.junit.Assert.assertEquals;
import li.strolch.model.parameter.Parameter;
import org.junit.Test;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ModelCompareTest {
@Test
public void shouldCompareToEqual() {
ParameterBag bag1 = ModelGenerator.createParameterBag("@1", "@1", "Test");
ParameterBag bag2 = ModelGenerator.createParameterBag("@1", "@1", "Test");
Parameter<?> param1;
Parameter<?> param2;
param1 = bag1.getParameter(PARAM_BOOLEAN_ID);
param2 = bag2.getParameter(PARAM_BOOLEAN_ID);
assertEquals(0, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_FLOAT_ID);
param2 = bag2.getParameter(PARAM_FLOAT_ID);
assertEquals(0, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_INTEGER_ID);
param2 = bag2.getParameter(PARAM_INTEGER_ID);
assertEquals(0, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LONG_ID);
param2 = bag2.getParameter(PARAM_LONG_ID);
assertEquals(0, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_STRING_ID);
param2 = bag2.getParameter(PARAM_STRING_ID);
assertEquals(0, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_DATE_ID);
param2 = bag2.getParameter(PARAM_DATE_ID);
assertEquals(0, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LIST_STRING_ID);
param2 = bag2.getParameter(PARAM_LIST_STRING_ID);
assertEquals(0, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LIST_INTEGER_ID);
param2 = bag2.getParameter(PARAM_LIST_INTEGER_ID);
assertEquals(0, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LIST_FLOAT_ID);
param2 = bag2.getParameter(PARAM_LIST_FLOAT_ID);
assertEquals(0, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LIST_LONG_ID);
param2 = bag2.getParameter(PARAM_LIST_LONG_ID);
assertEquals(0, param1.compareTo(param2));
}
@Test
public void shouldCompareToLessThan() {
ParameterBag bag1 = ModelGenerator.createParameterBag("@1", "@1", "Test");
ParameterBag bag2 = ModelGenerator.createParameterBag("@1", "@1", "Test");
Parameter<?> param1;
Parameter<?> param2;
param1 = bag1.getParameter(PARAM_BOOLEAN_ID);
param2 = bag2.getParameter(PARAM_BOOLEAN_ID);
param1.setValueFromString(Boolean.FALSE.toString());
assertEquals(-1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_FLOAT_ID);
param2 = bag2.getParameter(PARAM_FLOAT_ID);
param1.setValueFromString("-10.123");
assertEquals(-1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_INTEGER_ID);
param2 = bag2.getParameter(PARAM_INTEGER_ID);
param1.setValueFromString("-10");
assertEquals(-1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LONG_ID);
param2 = bag2.getParameter(PARAM_LONG_ID);
param1.setValueFromString("-10876543");
assertEquals(-1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_STRING_ID);
param2 = bag2.getParameter(PARAM_STRING_ID);
param1.setValueFromString("a");
assertEquals(-18, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_DATE_ID);
param2 = bag2.getParameter(PARAM_DATE_ID);
param1.setValueFromString("1970-01-01T00:00:00.000Z");
assertEquals(-1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LIST_STRING_ID);
param2 = bag2.getParameter(PARAM_LIST_STRING_ID);
param1.setValueFromString("");
assertEquals(-1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LIST_INTEGER_ID);
param2 = bag2.getParameter(PARAM_LIST_INTEGER_ID);
param1.setValueFromString("1");
assertEquals(-1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LIST_FLOAT_ID);
param2 = bag2.getParameter(PARAM_LIST_FLOAT_ID);
param1.setValueFromString("1.0");
assertEquals(-1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LIST_LONG_ID);
param2 = bag2.getParameter(PARAM_LIST_LONG_ID);
param1.setValueFromString("10");
assertEquals(-1, param1.compareTo(param2));
}
@Test
public void shouldCompareToGreaterThan() {
ParameterBag bag1 = ModelGenerator.createParameterBag("@1", "@1", "Test");
ParameterBag bag2 = ModelGenerator.createParameterBag("@1", "@1", "Test");
Parameter<?> param1;
Parameter<?> param2;
param1 = bag1.getParameter(PARAM_BOOLEAN_ID);
param2 = bag2.getParameter(PARAM_BOOLEAN_ID);
param2.setValueFromString(Boolean.FALSE.toString());
assertEquals(1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_FLOAT_ID);
param2 = bag2.getParameter(PARAM_FLOAT_ID);
param2.setValueFromString("-10.123");
assertEquals(1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_INTEGER_ID);
param2 = bag2.getParameter(PARAM_INTEGER_ID);
param2.setValueFromString("-10");
assertEquals(1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LONG_ID);
param2 = bag2.getParameter(PARAM_LONG_ID);
param2.setValueFromString("-10876543");
assertEquals(1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_STRING_ID);
param2 = bag2.getParameter(PARAM_STRING_ID);
param2.setValueFromString("a");
assertEquals(18, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_DATE_ID);
param2 = bag2.getParameter(PARAM_DATE_ID);
param2.setValueFromString("1970-01-01T00:00:00.000Z");
assertEquals(1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LIST_STRING_ID);
param2 = bag2.getParameter(PARAM_LIST_STRING_ID);
param2.setValueFromString("");
assertEquals(1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LIST_INTEGER_ID);
param2 = bag2.getParameter(PARAM_LIST_INTEGER_ID);
param2.setValueFromString("1");
assertEquals(1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LIST_FLOAT_ID);
param2 = bag2.getParameter(PARAM_LIST_FLOAT_ID);
param2.setValueFromString("1.0");
assertEquals(1, param1.compareTo(param2));
param1 = bag1.getParameter(PARAM_LIST_LONG_ID);
param2 = bag2.getParameter(PARAM_LIST_LONG_ID);
param2.setValueFromString("10");
assertEquals(1, param1.compareTo(param2));
}
}