Refactoring code to use generics

This commit is contained in:
Patrick Gotthard 2014-04-12 22:36:29 +02:00
parent e3848c5582
commit ac3fed65ce
9 changed files with 131 additions and 99 deletions

View file

@ -43,6 +43,7 @@ package org.rometools.feed.module.content;
import java.util.List; import java.util.List;
import org.jdom2.Content;
import org.jdom2.Namespace; import org.jdom2.Namespace;
/** /**
@ -53,10 +54,11 @@ import org.jdom2.Namespace;
* @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a> * @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a>
*/ */
public class ContentItem implements Cloneable { public class ContentItem implements Cloneable {
private String contentFormat; private String contentFormat;
private String contentEncoding; private String contentEncoding;
private String contentValue; private String contentValue;
private List contentValueDOM; private List<Content> contentValueDOM;
private String contentAbout; private String contentAbout;
private String contentValueParseType; private String contentValueParseType;
private List<Namespace> contentValueNamespace; private List<Namespace> contentValueNamespace;
@ -90,11 +92,11 @@ public class ContentItem implements Cloneable {
this.contentValue = contentValue; this.contentValue = contentValue;
} }
public List getContentValueDOM() { public List<Content> getContentValueDOM() {
return contentValueDOM; return contentValueDOM;
} }
public void setContentValueDOM(final List contentValueDOM) { public void setContentValueDOM(final List<Content> contentValueDOM) {
this.contentValueDOM = contentValueDOM; this.contentValueDOM = contentValueDOM;
} }

View file

@ -121,7 +121,7 @@ public class ContentModuleParser implements com.sun.syndication.io.ModuleParser
contentStrings.add(value.getText()); contentStrings.add(value.getText());
} }
ci.setContentValueDOM(value.clone().getContent()); ci.setContentValueDOM((List<Content>) value.clone().getContent());
} }
if (format != null) { if (format != null) {

View file

@ -149,7 +149,6 @@ public class SlashImpl implements Slash {
@Override @Override
public boolean equals(final Object obj) { public boolean equals(final Object obj) {
final EqualsBean eBean = new EqualsBean(this.getClass(), this); final EqualsBean eBean = new EqualsBean(this.getClass(), this);
return eBean.beanEquals(obj); return eBean.beanEquals(obj);
} }
} }

View file

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

View file

@ -17,12 +17,9 @@
*/ */
package org.rometools.feed.module.sle; package org.rometools.feed.module.sle;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import org.jdom2.Document; import org.jdom2.Document;
import org.rometools.feed.module.sle.io.ModuleParser;
import org.rometools.feed.module.sle.types.Group; import org.rometools.feed.module.sle.types.Group;
import org.rometools.feed.module.sle.types.Sort; import org.rometools.feed.module.sle.types.Sort;
@ -31,7 +28,6 @@ import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException; import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput; import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.SyndFeedOutput; import com.sun.syndication.io.SyndFeedOutput;
import com.sun.syndication.io.impl.ModuleGenerators;
/** /**
* This is a utiltiy class for grouping and sorting lists of entries based on the SLE. * This is a utiltiy class for grouping and sorting lists of entries based on the SLE.
@ -44,12 +40,13 @@ import com.sun.syndication.io.impl.ModuleGenerators;
* @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a> * @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a>
* @see SleEntry * @see SleEntry
*/ */
public class SleUtility { public final class SleUtility {
private static final String ITEM_MODULE_GENERATORS_POSFIX_KEY = ".item.ModuleGenerator.classes";
/** Creates a new instance of GroupAndSort */ // private static final String ITEM_MODULE_GENERATORS_POSFIX_KEY =
// ".item.ModuleGenerator.classes";
/** Private constructor for utility class */
private SleUtility() { private SleUtility() {
super();
} }
/** /**
@ -59,14 +56,12 @@ public class SleUtility {
* @param groups Group fields (from the SimpleListExtension module) * @param groups Group fields (from the SimpleListExtension module)
* @return Grouped list of entries. * @return Grouped list of entries.
*/ */
public static List group(final List values, final Group[] groups) { public static List<Extendable> group(final List<Extendable> values, final Group[] groups) {
final SortableList list = values instanceof SortableList ? (SortableList) values : new SortableList(values); final SortableList list = values instanceof SortableList ? (SortableList) values : new SortableList(values);
final GroupStrategy gs = new GroupStrategy(); final GroupStrategy gs = new GroupStrategy();
for (int i = groups.length - 1; i >= 0; i--) { for (int i = groups.length - 1; i >= 0; i--) {
list.sortOnProperty(groups[i], true, gs); list.sortOnProperty(groups[i], true, gs);
} }
return list; return list;
} }
@ -78,10 +73,9 @@ public class SleUtility {
* @param ascending Sort ascending/descending. * @param ascending Sort ascending/descending.
* @return Sorted list of values * @return Sorted list of values
*/ */
public static List sort(final List values, final Sort sort, final boolean ascending) { public static List<Extendable> sort(final List<Extendable> values, final Sort sort, final boolean ascending) {
final SortableList list = values instanceof SortableList ? (SortableList) values : new SortableList(values); final SortableList list = values instanceof SortableList ? (SortableList) values : new SortableList(values);
list.sortOnProperty(sort, ascending, new SortStrategy()); list.sortOnProperty(sort, ascending, new SortStrategy());
return list; return list;
} }
@ -94,10 +88,9 @@ public class SleUtility {
* @param ascending Sort ascending/descending * @param ascending Sort ascending/descending
* @return Grouped and sorted list of entries. * @return Grouped and sorted list of entries.
*/ */
public static List sortAndGroup(final List values, final Group[] groups, final Sort sort, final boolean ascending) { public static List<Extendable> sortAndGroup(final List<Extendable> values, final Group[] groups, final Sort sort, final boolean ascending) {
List list = sort(values, sort, ascending); List<Extendable> list = sort(values, sort, ascending);
list = group(list, groups); list = group(list, groups);
return list; return list;
} }
@ -108,86 +101,13 @@ public class SleUtility {
* structures. It is a very heavy operation and should not be called frequently! * structures. It is a very heavy operation and should not be called frequently!
*/ */
public static void initializeForSorting(final SyndFeed feed) throws FeedException { public static void initializeForSorting(final SyndFeed feed) throws FeedException {
final List syndEntries = feed.getEntries();
// TODO: the null parameter below will break delegating parsers and generators // TODO: the null parameter below will break delegating parsers and generators
final ModuleGenerators g = new ModuleGenerators(feed.getFeedType() + ITEM_MODULE_GENERATORS_POSFIX_KEY, null); // final ModuleGenerators g = new ModuleGenerators(feed.getFeedType() +
// ITEM_MODULE_GENERATORS_POSFIX_KEY, null);
final SyndFeedOutput o = new SyndFeedOutput(); final SyndFeedOutput o = new SyndFeedOutput();
final Document d = o.outputJDom(feed); final Document d = o.outputJDom(feed);
final SyndFeed feed2 = new SyndFeedInput().build(d); final SyndFeed feed2 = new SyndFeedInput().build(d);
feed.copyFrom(feed2); feed.copyFrom(feed2);
} }
private static interface ValueStrategy {
public Comparable getValue(Extendable o, Object valueName);
}
private static class GroupStrategy implements ValueStrategy {
@Override
public Comparable getValue(final Extendable o, final Object value) {
Comparable oc = null;
try {
oc = ((SleEntry) o.getModule(ModuleParser.TEMP.getURI())).getGroupByElement((Group) value).getValue();
} catch (final NullPointerException npe) {
; // watch it go by
}
return oc;
}
}
private static class SortStrategy implements ValueStrategy {
@Override
public Comparable getValue(final Extendable o, final Object value) {
Comparable oc = null;
try {
oc = ((SleEntry) o.getModule(ModuleParser.TEMP.getURI())).getSortByElement((Sort) value).getValue();
} catch (final NullPointerException npe) {
; // watch it go by
}
return oc;
}
}
private static class SortableList extends ArrayList {
/**
*
*/
private static final long serialVersionUID = 1L;
public SortableList(final Collection c) {
super(c);
}
/**
* performs a selection sort on all the beans in the List
*/
public synchronized void sortOnProperty(final Object value, final boolean ascending, final ValueStrategy strat) {
for (int i = 0; i < size() - 1; i++) {
for (int j = i + 1; j < size(); j++) {
final Extendable o1 = (Extendable) get(i);
final Comparable oc1 = strat.getValue(o1, value);
final Extendable o2 = (Extendable) get(j);
final Comparable oc2 = strat.getValue(o2, value);
System.out.println(oc1 + " < " + oc2);
if (ascending) {
if (oc1 != oc2 && (oc2 == null || oc1 != null && oc2 != null && oc2.compareTo(oc1) < 0)) { // swap
set(i, o2);
set(j, o1);
}
} else {
if (oc1 != oc2 && (oc1 == null || oc1 != null && oc2 != null && oc1.compareTo(oc2) < 0)) { // swap
set(i, o2);
set(j, o1);
}
}
}
}
}
}
} }

View file

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

View file

@ -0,0 +1,47 @@
package org.rometools.feed.module.sle;
import java.util.ArrayList;
import java.util.Collection;
import com.sun.syndication.feed.module.Extendable;
class SortableList extends ArrayList<Extendable> {
private static final long serialVersionUID = 1L;
public SortableList(final Collection<Extendable> c) {
super(c);
}
/**
* performs a selection sort on all the beans in the List
*/
public synchronized void sortOnProperty(final Object value, final boolean ascending, final ValueStrategy strat) {
for (int i = 0; i < size() - 1; i++) {
for (int j = i + 1; j < size(); j++) {
final Extendable o1 = get(i);
final Comparable oc1 = strat.getValue(o1, value);
final Extendable o2 = get(j);
final Comparable oc2 = strat.getValue(o2, value);
System.out.println(oc1 + " < " + oc2);
if (ascending) {
if (oc1 != oc2 && (oc2 == null || oc1 != null && oc2 != null && oc2.compareTo(oc1) < 0)) { // swap
set(i, o2);
set(j, o1);
}
} else {
if (oc1 != oc2 && (oc1 == null || oc1 != null && oc2 != null && oc1.compareTo(oc2) < 0)) { // swap
set(i, o2);
set(j, o1);
}
}
}
}
}
}

View file

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

View file

@ -8,6 +8,7 @@
package org.rometools.feed.module.sle; package org.rometools.feed.module.sle;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import junit.framework.Test; import junit.framework.Test;
@ -16,6 +17,7 @@ import junit.framework.TestSuite;
import org.rometools.feed.module.AbstractTestCase; import org.rometools.feed.module.AbstractTestCase;
import org.rometools.feed.module.sle.types.Sort; import org.rometools.feed.module.sle.types.Sort;
import com.sun.syndication.feed.module.Extendable;
import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput; import com.sun.syndication.io.SyndFeedInput;
@ -44,10 +46,10 @@ public class GroupAndSortTest extends AbstractTestCase {
final SyndFeed feed = input.build(new File(super.getTestFile("data/bookexample.xml"))); final SyndFeed feed = input.build(new File(super.getTestFile("data/bookexample.xml")));
final SimpleListExtension sle = (SimpleListExtension) feed.getModule(SimpleListExtension.URI); final SimpleListExtension sle = (SimpleListExtension) feed.getModule(SimpleListExtension.URI);
final List<SyndEntry> entries = feed.getEntries(); final List<Extendable> entries = new ArrayList<Extendable>(feed.getEntries());
final Sort[] sortFields = sle.getSortFields(); final Sort[] sortFields = sle.getSortFields();
final Sort sort = sortFields[1]; final Sort sort = sortFields[1];
List sortedEntries = SleUtility.sort(entries, sort, true); List<Extendable> sortedEntries = SleUtility.sort(entries, sort, true);
SyndEntry entry = (SyndEntry) sortedEntries.get(0); SyndEntry entry = (SyndEntry) sortedEntries.get(0);
assertEquals("Great Journeys of the Past", entry.getTitle()); assertEquals("Great Journeys of the Past", entry.getTitle());
sortedEntries = SleUtility.sort(entries, sort, false); sortedEntries = SleUtility.sort(entries, sort, false);
@ -100,7 +102,8 @@ public class GroupAndSortTest extends AbstractTestCase {
final SyndFeed feed = input.build(new File(super.getTestFile("data/YahooTopSongs.xml"))); final SyndFeed feed = input.build(new File(super.getTestFile("data/YahooTopSongs.xml")));
final SimpleListExtension sle = (SimpleListExtension) feed.getModule(SimpleListExtension.URI); final SimpleListExtension sle = (SimpleListExtension) feed.getModule(SimpleListExtension.URI);
System.out.println("Sorting on " + sle.getSortFields()[0]); System.out.println("Sorting on " + sle.getSortFields()[0]);
final List sortedEntries = SleUtility.sort(feed.getEntries(), sle.getSortFields()[0], true); final List<Extendable> entries = new ArrayList<Extendable>(feed.getEntries());
final List<Extendable> sortedEntries = SleUtility.sort(entries, sle.getSortFields()[0], true);
for (int i = 0; i < sortedEntries.size(); i++) { for (int i = 0; i < sortedEntries.size(); i++) {
final SyndEntry entry = (SyndEntry) sortedEntries.get(i); final SyndEntry entry = (SyndEntry) sortedEntries.get(i);
System.out.println(entry.getTitle()); System.out.println(entry.getTitle());