Skip to content

Commit

Permalink
Implement comparable interface in all properties
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Aug 5, 2021
1 parent beedabb commit 6925a07
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/main/java/net/fortuna/ical4j/model/Parameter.java
Expand Up @@ -37,6 +37,7 @@
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.net.URISyntaxException;
import java.util.Comparator;

/**
* Defines an iCalendar parameter. Subclasses of this class provide additional validation and typed values for specific
Expand All @@ -50,7 +51,7 @@
* <p/>
* $Id$ [Apr 5, 2004]
*/
public abstract class Parameter extends Content {
public abstract class Parameter extends Content implements Comparable<Parameter> {

private static final long serialVersionUID = -2058497904769713528L;

Expand Down Expand Up @@ -297,4 +298,14 @@ public <T extends Parameter> T copy() throws URISyntaxException {
}
return (T) factory.createParameter(getValue());
}

@Override
public int compareTo(Parameter o) {
if (this.equals(o)) {
return 0;
}
return Comparator.comparing(Parameter::getName)
.thenComparing(Parameter::getValue)
.compare(this, o);
}
}
Expand Up @@ -41,6 +41,7 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.Comparator;

/**
* $Id$
Expand Down Expand Up @@ -295,7 +296,9 @@ public Property copy() throws IOException, URISyntaxException, ParseException {
@Override
public int compareTo(Property o) {
if (o instanceof DateProperty) {
return getDate().compareTo(((DateProperty) o).getDate());
return Comparator.comparing(DateProperty::getName)
.thenComparing(DateProperty::getDate)
.compare(this, (DateProperty) o);
}
return super.compareTo(o);
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/net/fortuna/ical4j/model/property/Sequence.java
Expand Up @@ -40,6 +40,7 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.Comparator;

/**
* $Id$
Expand Down Expand Up @@ -210,26 +211,28 @@ public void validate() throws ValidationException {
@Override
public int compareTo(Property o) {
if (o instanceof Sequence) {
return Integer.compare(getSequenceNo(), ((Sequence) o).getSequenceNo());
return Comparator.comparing(Sequence::getName)
.thenComparing(Sequence::getSequenceNo)
.compare(this, (Sequence) o);
}
return super.compareTo(o);
}

public static class Factory extends Content.Factory implements PropertyFactory {
public static class Factory extends Content.Factory implements PropertyFactory<Sequence> {
private static final long serialVersionUID = 1L;

public Factory() {
super(SEQUENCE);
}

@Override
public Property createProperty(final ParameterList parameters, final String value)
public Sequence createProperty(final ParameterList parameters, final String value)
throws IOException, URISyntaxException, ParseException {
return new Sequence(parameters, value);
}

@Override
public Property createProperty() {
public Sequence createProperty() {
return new Sequence();
}
}
Expand Down

0 comments on commit 6925a07

Please sign in to comment.