Refactored some code

This commit is contained in:
Patrick Gotthard 2014-04-13 12:12:27 +02:00
parent abd3681044
commit 18e31ddf15
4 changed files with 66 additions and 60 deletions

View file

@ -28,10 +28,9 @@ import com.sun.syndication.feed.module.ModuleImpl;
* @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 SimpleListExtensionImpl extends ModuleImpl implements SimpleListExtension { public class SimpleListExtensionImpl extends ModuleImpl implements SimpleListExtension {
/**
*
*/
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private String treatAs = "list"; private String treatAs = "list";
private Group[] groupFields; private Group[] groupFields;
private Sort[] sortFields; private Sort[] sortFields;
@ -114,4 +113,5 @@ public class SimpleListExtensionImpl extends ModuleImpl implements SimpleListExt
setSortFields(sle.getSortFields().clone()); setSortFields(sle.getSortFields().clone());
setTreatAs(sle.getTreatAs()); setTreatAs(sle.getTreatAs());
} }
} }

View file

@ -27,8 +27,9 @@ import com.sun.syndication.feed.CopyFrom;
import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.impl.ObjectBean;
/** /**
* This is a <b>parse only</b> module that holds the values of enternal fields declared in the SLE module. These will <b>not</b> be persisted on an output() * This is a <b>parse only</b> module that holds the values of enternal fields declared in the SLE
* call, <b>nor</b> will changing a value here change a value in another module or a foreign markup tag. * module. These will <b>not</b> be persisted on an output() call, <b>nor</b> will changing a value
* here change a value in another module or a foreign markup tag.
* *
* @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a> * @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a>
*/ */
@ -72,13 +73,14 @@ public class SleEntryImpl implements SleEntry {
/** /**
* Returns the interface the copyFrom works on. * Returns the interface the copyFrom works on.
* <p> * <p>
* This is useful when dealing with properties that may have multiple implementations. For example, Module. * This is useful when dealing with properties that may have multiple implementations. For
* example, Module.
* <p> * <p>
* *
* @return the interface the copyFrom works on. * @return the interface the copyFrom works on.
*/ */
@Override @Override
public Class getInterface() { public Class<SleEntry> getInterface() {
return SleEntry.class; return SleEntry.class;
} }
@ -128,8 +130,8 @@ public class SleEntryImpl implements SleEntry {
* <p> * <p>
* Any existing properties in this bean are lost. * Any existing properties in this bean are lost.
* <p> * <p>
* This method is useful for moving from one implementation of a bean interface to another. For example from the default SyndFeed bean implementation to a * This method is useful for moving from one implementation of a bean interface to another. For
* Hibernate ready implementation. * example from the default SyndFeed bean implementation to a Hibernate ready implementation.
* <p> * <p>
* *
* @param obj the instance to copy properties from. * @param obj the instance to copy properties from.

View file

@ -74,7 +74,12 @@ public final class SleUtility {
* @return Sorted list of values * @return Sorted list of values
*/ */
public static List<Extendable> sort(final List<Extendable> 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;
if (values instanceof SortableList) {
list = (SortableList) values;
} else {
list = new SortableList(values);
}
list.sortOnProperty(sort, ascending, new SortStrategy()); list.sortOnProperty(sort, ascending, new SortStrategy());
return list; return list;
} }
@ -104,10 +109,10 @@ public final class SleUtility {
// 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() + // final ModuleGenerators g = new ModuleGenerators(feed.getFeedType() +
// ITEM_MODULE_GENERATORS_POSFIX_KEY, null); // ITEM_MODULE_GENERATORS_POSFIX_KEY, null);
final SyndFeedOutput o = new SyndFeedOutput(); final SyndFeedOutput output = new SyndFeedOutput();
final Document d = o.outputJDom(feed); final Document document = output.outputJDom(feed);
final SyndFeed feed2 = new SyndFeedInput().build(d); final SyndFeed copy = new SyndFeedInput().build(document);
feed.copyFrom(feed2); feed.copyFrom(copy);
} }
} }

View file

@ -1,10 +1,3 @@
/*
* GroupAndSortTest.java
* JUnit based test
*
* Created on April 29, 2006, 8:56 PM
*/
package org.rometools.feed.module.sle; package org.rometools.feed.module.sle;
import java.io.File; import java.io.File;
@ -33,63 +26,52 @@ public class GroupAndSortTest extends AbstractTestCase {
} }
public static Test suite() { public static Test suite() {
final TestSuite suite = new TestSuite(GroupAndSortTest.class); return new TestSuite(GroupAndSortTest.class);
return suite;
} }
/** /**
* Test of sort method, of class com.sun.syndication.feed.module.sle.GroupAndSort. * Test of sort method, of class com.sun.syndication.feed.module.sle.GroupAndSort.
*/ */
public void testSort() throws Exception { public void testSort() throws Exception {
final SyndFeedInput input = new SyndFeedInput();
final SyndFeed feed = input.build(new File(super.getTestFile("data/bookexample.xml")));
final SimpleListExtension sle = (SimpleListExtension) feed.getModule(SimpleListExtension.URI);
final List<Extendable> entries = new ArrayList<Extendable>(feed.getEntries()); final File testdata = new File(super.getTestFile("data/bookexample.xml"));
final SyndFeed feed = new SyndFeedInput().build(testdata);
final List<SyndEntry> feedEntries = feed.getEntries();
final List<Extendable> entries = new ArrayList<Extendable>(feedEntries);
final SimpleListExtension sle = (SimpleListExtension) feed.getModule(SimpleListExtension.URI);
final Sort[] sortFields = sle.getSortFields(); final Sort[] sortFields = sle.getSortFields();
final Sort sort = sortFields[1];
List<Extendable> sortedEntries = SleUtility.sort(entries, sort, true); final Sort sortByDate = sortFields[1];
final Sort sortByTitle = sortFields[2];
// sort by date ascending
List<Extendable> sortedEntries = SleUtility.sort(entries, sortByDate, 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);
// sort by date descending
sortedEntries = SleUtility.sort(entries, sortByDate, false);
entry = (SyndEntry) sortedEntries.get(0); entry = (SyndEntry) sortedEntries.get(0);
assertEquals("Horror Stories, vol 16", entry.getTitle()); assertEquals("Horror Stories, vol 16", entry.getTitle());
sortedEntries = SleUtility.sort(entries, sort, true); // sort by date ascending
entry = (SyndEntry) sortedEntries.get(0); sortedEntries = SleUtility.sort(entries, sortByDate, true);
// update first entry and reinitialize
entry = (SyndEntry) sortedEntries.get(0);
entry.setTitle("ZZZZZ"); entry.setTitle("ZZZZZ");
SleUtility.initializeForSorting(feed); SleUtility.initializeForSorting(feed);
System.out.println(entries.size() + " **Sorting on " + sortFields[2]);
sortedEntries = SleUtility.sort(entries, sortFields[2], false); // sort by title desc
System.out.println("Sorted: " + sortedEntries.size()); sortedEntries = SleUtility.sort(entries, sortByTitle, false);
for (int i = 0; i < sortedEntries.size(); i++) {
entry = (SyndEntry) sortedEntries.get(i);
System.out.println(i + " -- " + entry.getTitle());
final SleEntry slent = (SleEntry) entry.getModule(SleEntry.URI);
for (int j = 0; j < slent.getSortValues().length; j++) {
System.out.println(slent.getSortValues()[j].getElement() + " == " + slent.getSortValues()[j].getValue());
}
}
entry = (SyndEntry) sortedEntries.get(0); entry = (SyndEntry) sortedEntries.get(0);
System.out.println(entry.getTitle());
assertEquals("ZZZZZ", entry.getTitle()); assertEquals("ZZZZZ", entry.getTitle());
entry = (SyndEntry) sortedEntries.get(1);
System.out.println(entry.getTitle());
sortedEntries = SleUtility.sort(entries, sortFields[2], true); // sort by title asc
sortedEntries = SleUtility.sort(entries, sortByTitle, true);
entry = (SyndEntry) sortedEntries.get(0); entry = (SyndEntry) sortedEntries.get(0);
System.out.println(entry.getTitle());
assertEquals("Horror Stories, vol 16", entry.getTitle());
entry = (SyndEntry) sortedEntries.get(1);
System.out.println(entry.getTitle());
sortedEntries = SleUtility.sortAndGroup(entries, sle.getGroupFields(), sortFields[2], true);
entry = (SyndEntry) sortedEntries.get(0);
System.out.println(entry.getTitle());
assertEquals("Horror Stories, vol 16", entry.getTitle()); assertEquals("Horror Stories, vol 16", entry.getTitle());
} }
@ -98,16 +80,18 @@ public class GroupAndSortTest extends AbstractTestCase {
* Test of sort method, of class com.sun.syndication.feed.module.sle.GroupAndSort. * Test of sort method, of class com.sun.syndication.feed.module.sle.GroupAndSort.
*/ */
public void testSort2() throws Exception { public void testSort2() throws Exception {
final SyndFeedInput input = new SyndFeedInput(); final SyndFeedInput input = new SyndFeedInput();
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]);
final List<Extendable> entries = new ArrayList<Extendable>(feed.getEntries()); final List<Extendable> entries = new ArrayList<Extendable>(feed.getEntries());
final List<Extendable> sortedEntries = SleUtility.sort(entries, sle.getSortFields()[0], true); 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());
} }
} }
/** /**
@ -120,8 +104,23 @@ public class GroupAndSortTest extends AbstractTestCase {
/** /**
* Test of sortAndGroup method, of class com.sun.syndication.feed.module.sle.GroupAndSort. * Test of sortAndGroup method, of class com.sun.syndication.feed.module.sle.GroupAndSort.
*/ */
public void testSortAndGroup() { public void testSortAndGroup() throws Exception {
// TODO add your test code.
final File testdata = new File(super.getTestFile("data/bookexample.xml"));
final SyndFeed feed = new SyndFeedInput().build(testdata);
final List<SyndEntry> feedEntries = feed.getEntries();
final List<Extendable> entries = new ArrayList<Extendable>(feedEntries);
final SimpleListExtension sle = (SimpleListExtension) feed.getModule(SimpleListExtension.URI);
final Sort[] sortFields = sle.getSortFields();
final Sort sortByTitle = sortFields[2];
final List<Extendable> sortedEntries = SleUtility.sortAndGroup(entries, sle.getGroupFields(), sortByTitle, true);
final SyndEntry entry = (SyndEntry) sortedEntries.get(0);
assertEquals("Horror Stories, vol 16", entry.getTitle());
} }
} }