Refactored some code

This commit is contained in:
Patrick Gotthard 2014-04-13 14:47:53 +02:00
parent 2495106e97
commit 26b7f4c4d2
8 changed files with 81 additions and 72 deletions

View file

@ -9,18 +9,15 @@ import com.sun.syndication.feed.module.Extendable;
class GroupStrategy implements ValueStrategy {
@Override
public Comparable getValue(final Extendable o, final Object value) {
Comparable oc = null;
public EntryValue getValue(final Extendable extendable, final Object value) {
try {
final String uri = ModuleParser.TEMP.getURI();
final SleEntry entry = (SleEntry) o.getModule(uri);
final SleEntry entry = (SleEntry) extendable.getModule(uri);
final Group group = (Group) value;
final EntryValue entryValue = entry.getGroupByElement(group);
oc = entryValue.getValue();
return entry.getGroupByElement(group);
} catch (final NullPointerException npe) {
; // watch it go by
return null;
}
return oc;
}
}

View file

@ -9,18 +9,15 @@ import com.sun.syndication.feed.module.Extendable;
class SortStrategy implements ValueStrategy {
@Override
public Comparable getValue(final Extendable o, final Object value) {
Comparable oc = null;
public EntryValue getValue(final Extendable extendable, final Object value) {
try {
final String uri = ModuleParser.TEMP.getURI();
final SleEntry entry = (SleEntry) o.getModule(uri);
final SleEntry entry = (SleEntry) extendable.getModule(uri);
final Sort sort = (Sort) value;
final EntryValue entryValue = entry.getSortByElement(sort);
oc = entryValue.getValue();
return entry.getSortByElement(sort);
} catch (final NullPointerException npe) {
; // watch it go by
return null;
}
return oc;
}
}

View file

@ -3,6 +3,8 @@ package org.rometools.feed.module.sle;
import java.util.ArrayList;
import java.util.Collection;
import org.rometools.feed.module.sle.types.EntryValue;
import com.sun.syndication.feed.module.Extendable;
class SortableList<T extends Extendable> extends ArrayList<T> {
@ -16,7 +18,7 @@ class SortableList<T extends Extendable> extends ArrayList<T> {
/**
* performs a selection sort on all the beans in the List
*/
public synchronized void sortOnProperty(final Object value, final boolean ascending, final ValueStrategy strat) {
public synchronized void sortOnProperty(final Object value, final boolean ascending, final ValueStrategy strategy) {
final int elementCount = size();
@ -27,8 +29,8 @@ class SortableList<T extends Extendable> extends ArrayList<T> {
final T entry1 = get(i);
final T entry2 = get(j);
final Comparable oc1 = strat.getValue(entry1, value);
final Comparable oc2 = strat.getValue(entry2, value);
final EntryValue oc1 = strategy.getValue(entry1, value);
final EntryValue oc2 = strategy.getValue(entry2, value);
if (oc1 != oc2) {
final boolean bothNotNull = oc1 != null && oc2 != null;

View file

@ -1,9 +1,11 @@
package org.rometools.feed.module.sle;
import org.rometools.feed.module.sle.types.EntryValue;
import com.sun.syndication.feed.module.Extendable;
interface ValueStrategy {
public Comparable getValue(Extendable o, Object valueName);
public EntryValue getValue(Extendable extendable, Object value);
}

View file

@ -28,67 +28,53 @@ import com.sun.syndication.feed.impl.ObjectBean;
*
* @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a>
*/
public class DateValue implements EntryValue<Date> {
public class DateValue implements EntryValue {
private static final long serialVersionUID = 8864338943592633517L;
private final ObjectBean obj = new ObjectBean(DateValue.class, this);
private Date value;
private String element;
private String label;
private Date value;
private Namespace namespace = Namespace.XML_NAMESPACE;
/**
*
* @param element
*/
public void setElement(final String element) {
this.element = element;
}
/**
*
* @return
*/
@Override
public String getElement() {
return element;
}
/**
*
* @param label
*/
public void setLabel(final String label) {
this.label = label;
}
/**
*
* @return
*/
@Override
public String getLabel() {
return label;
}
/**
*
* @param value
*/
public void setValue(final Date value) {
this.value = value;
}
/**
*
* @return
*/
@Override
public Date getValue() {
return value;
}
@Override
public Namespace getNamespace() {
return namespace;
}
public void setNamespace(final Namespace namespace) {
this.namespace = namespace == null ? Namespace.XML_NAMESPACE : namespace;
}
@Override
public Object clone() {
final DateValue clone = new DateValue();
@ -115,11 +101,13 @@ public class DateValue implements EntryValue<Date> {
}
@Override
public Namespace getNamespace() {
return namespace;
public int compareTo(final EntryValue other) {
final Comparable<?> otherValue = other.getValue();
if (otherValue instanceof Date) {
return value.compareTo((Date) otherValue);
} else {
throw new RuntimeException("Can't compare different EntryValue types");
}
}
public void setNamespace(final Namespace namespace) {
this.namespace = namespace == null ? Namespace.XML_NAMESPACE : namespace;
}
}

View file

@ -26,7 +26,7 @@ import org.jdom2.Namespace;
*
* @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a>
*/
public interface EntryValue<T extends Comparable<T>> extends Serializable, Cloneable {
public interface EntryValue extends Serializable, Cloneable, Comparable<EntryValue> {
/**
* Returns the name of the element.
*
@ -46,7 +46,7 @@ public interface EntryValue<T extends Comparable<T>> extends Serializable, Clone
*
* @return Returns the value of the element.
*/
public T getValue();
public Comparable<?> getValue();
/**
* Returns the namespace of the element.
@ -54,4 +54,5 @@ public interface EntryValue<T extends Comparable<T>> extends Serializable, Clone
* @return Returns the namespace of the element.
*/
public Namespace getNamespace();
}

View file

@ -28,14 +28,15 @@ import com.sun.syndication.feed.impl.ObjectBean;
*
* @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a>
*/
public class NumberValue implements EntryValue<BigDecimal> {
public class NumberValue implements EntryValue {
private static final long serialVersionUID = 8043418996659222922L;
private final ObjectBean obj = new ObjectBean(NumberValue.class, this);
private BigDecimal value;
private String element;
private String label;
private BigDecimal value;
private Namespace namespace = Namespace.XML_NAMESPACE;
public void setElement(final String element) {
@ -65,6 +66,15 @@ public class NumberValue implements EntryValue<BigDecimal> {
return value;
}
@Override
public Namespace getNamespace() {
return namespace;
}
public void setNamespace(final Namespace namespace) {
this.namespace = namespace == null ? Namespace.XML_NAMESPACE : namespace;
}
@Override
public Object clone() {
final NumberValue clone = new NumberValue();
@ -72,7 +82,6 @@ public class NumberValue implements EntryValue<BigDecimal> {
clone.setLabel(getLabel());
clone.setValue(value);
clone.setNamespace(namespace);
return clone;
}
@ -92,11 +101,13 @@ public class NumberValue implements EntryValue<BigDecimal> {
}
@Override
public Namespace getNamespace() {
return namespace;
public int compareTo(final EntryValue other) {
final Comparable<?> otherValue = other.getValue();
if (otherValue instanceof BigDecimal) {
return value.compareTo((BigDecimal) otherValue);
} else {
throw new RuntimeException("Can't compare different EntryValue types");
}
}
public void setNamespace(final Namespace namespace) {
this.namespace = namespace == null ? Namespace.XML_NAMESPACE : namespace;
}
}

View file

@ -26,18 +26,19 @@ import com.sun.syndication.feed.impl.ObjectBean;
*
* @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a>
*/
public class StringValue implements EntryValue<String> {
public class StringValue implements EntryValue {
private static final long serialVersionUID = -8384073300710802173L;
private final ObjectBean obj = new ObjectBean(StringValue.class, this);
private String element;
private String label;
private String value;
private Namespace namespace = Namespace.XML_NAMESPACE;
public void setElement(final String element) {
this.element = element;
public void setValue(final String value) {
this.value = value;
}
@Override
@ -54,15 +55,24 @@ public class StringValue implements EntryValue<String> {
return label;
}
public void setValue(final String value) {
this.value = value;
}
@Override
public String getValue() {
return value;
}
public void setElement(final String element) {
this.element = element;
}
@Override
public Namespace getNamespace() {
return namespace;
}
public void setNamespace(final Namespace namespace) {
this.namespace = namespace == null ? Namespace.XML_NAMESPACE : namespace;
}
@Override
public Object clone() {
final StringValue clone = new StringValue();
@ -70,7 +80,6 @@ public class StringValue implements EntryValue<String> {
clone.setLabel(getLabel());
clone.setValue(value);
clone.setNamespace(namespace);
return clone;
}
@ -90,11 +99,13 @@ public class StringValue implements EntryValue<String> {
}
@Override
public Namespace getNamespace() {
return namespace;
public int compareTo(final EntryValue other) {
final Comparable<?> otherValue = other.getValue();
if (otherValue instanceof String) {
return value.compareTo((String) otherValue);
} else {
throw new RuntimeException("Can't compare different EntryValue types");
}
}
public void setNamespace(final Namespace namespace) {
this.namespace = namespace == null ? Namespace.XML_NAMESPACE : namespace;
}
}