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

View file

@ -27,8 +27,9 @@ import com.sun.syndication.feed.CopyFrom;
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()
* call, <b>nor</b> will changing a value here change a value in another module or a foreign markup tag.
* 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() 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>
*/
@ -72,13 +73,14 @@ public class SleEntryImpl implements SleEntry {
/**
* Returns the interface the copyFrom works on.
* <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>
*
* @return the interface the copyFrom works on.
*/
@Override
public Class getInterface() {
public Class<SleEntry> getInterface() {
return SleEntry.class;
}
@ -128,8 +130,8 @@ public class SleEntryImpl implements SleEntry {
* <p>
* Any existing properties in this bean are lost.
* <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
* Hibernate ready implementation.
* 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 Hibernate ready implementation.
* <p>
*
* @param obj the instance to copy properties from.

View file

@ -74,7 +74,12 @@ public final class SleUtility {
* @return Sorted list of values
*/
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());
return list;
}
@ -104,10 +109,10 @@ public final class SleUtility {
// 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 SyndFeedOutput o = new SyndFeedOutput();
final Document d = o.outputJDom(feed);
final SyndFeed feed2 = new SyndFeedInput().build(d);
feed.copyFrom(feed2);
final SyndFeedOutput output = new SyndFeedOutput();
final Document document = output.outputJDom(feed);
final SyndFeed copy = new SyndFeedInput().build(document);
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;
import java.io.File;
@ -33,63 +26,52 @@ public class GroupAndSortTest extends AbstractTestCase {
}
public static Test suite() {
final TestSuite suite = new TestSuite(GroupAndSortTest.class);
return suite;
return new TestSuite(GroupAndSortTest.class);
}
/**
* Test of sort method, of class com.sun.syndication.feed.module.sle.GroupAndSort.
*/
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 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);
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);
assertEquals("Horror Stories, vol 16", entry.getTitle());
sortedEntries = SleUtility.sort(entries, sort, true);
entry = (SyndEntry) sortedEntries.get(0);
// sort by date ascending
sortedEntries = SleUtility.sort(entries, sortByDate, true);
// update first entry and reinitialize
entry = (SyndEntry) sortedEntries.get(0);
entry.setTitle("ZZZZZ");
SleUtility.initializeForSorting(feed);
System.out.println(entries.size() + " **Sorting on " + sortFields[2]);
sortedEntries = SleUtility.sort(entries, sortFields[2], false);
System.out.println("Sorted: " + sortedEntries.size());
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());
}
}
// sort by title desc
sortedEntries = SleUtility.sort(entries, sortByTitle, false);
entry = (SyndEntry) sortedEntries.get(0);
System.out.println(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);
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());
}
@ -98,16 +80,18 @@ public class GroupAndSortTest extends AbstractTestCase {
* Test of sort method, of class com.sun.syndication.feed.module.sle.GroupAndSort.
*/
public void testSort2() throws Exception {
final SyndFeedInput input = new SyndFeedInput();
final SyndFeed feed = input.build(new File(super.getTestFile("data/YahooTopSongs.xml")));
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> sortedEntries = SleUtility.sort(entries, sle.getSortFields()[0], true);
for (int i = 0; i < sortedEntries.size(); i++) {
final SyndEntry entry = (SyndEntry) sortedEntries.get(i);
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.
*/
public void testSortAndGroup() {
// TODO add your test code.
public void testSortAndGroup() throws Exception {
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());
}
}