From ac3fed65cef029fda541195583e7e3d770b57757 Mon Sep 17 00:00:00 2001 From: Patrick Gotthard Date: Sat, 12 Apr 2014 22:36:29 +0200 Subject: [PATCH] Refactoring code to use generics --- .../feed/module/content/ContentItem.java | 8 +- .../content/io/ContentModuleParser.java | 2 +- .../feed/module/slash/SlashImpl.java | 1 - .../feed/module/sle/GroupStrategy.java | 26 +++++ .../rometools/feed/module/sle/SleUtility.java | 102 ++---------------- .../feed/module/sle/SortStrategy.java | 26 +++++ .../feed/module/sle/SortableList.java | 47 ++++++++ .../feed/module/sle/ValueStrategy.java | 9 ++ .../feed/module/sle/GroupAndSortTest.java | 9 +- 9 files changed, 131 insertions(+), 99 deletions(-) create mode 100644 src/main/java/org/rometools/feed/module/sle/GroupStrategy.java create mode 100644 src/main/java/org/rometools/feed/module/sle/SortStrategy.java create mode 100644 src/main/java/org/rometools/feed/module/sle/SortableList.java create mode 100644 src/main/java/org/rometools/feed/module/sle/ValueStrategy.java diff --git a/src/main/java/org/rometools/feed/module/content/ContentItem.java b/src/main/java/org/rometools/feed/module/content/ContentItem.java index 431b752..7800bef 100644 --- a/src/main/java/org/rometools/feed/module/content/ContentItem.java +++ b/src/main/java/org/rometools/feed/module/content/ContentItem.java @@ -43,6 +43,7 @@ package org.rometools.feed.module.content; import java.util.List; +import org.jdom2.Content; import org.jdom2.Namespace; /** @@ -53,10 +54,11 @@ import org.jdom2.Namespace; * @author Robert "kebernet" Cooper */ public class ContentItem implements Cloneable { + private String contentFormat; private String contentEncoding; private String contentValue; - private List contentValueDOM; + private List contentValueDOM; private String contentAbout; private String contentValueParseType; private List contentValueNamespace; @@ -90,11 +92,11 @@ public class ContentItem implements Cloneable { this.contentValue = contentValue; } - public List getContentValueDOM() { + public List getContentValueDOM() { return contentValueDOM; } - public void setContentValueDOM(final List contentValueDOM) { + public void setContentValueDOM(final List contentValueDOM) { this.contentValueDOM = contentValueDOM; } diff --git a/src/main/java/org/rometools/feed/module/content/io/ContentModuleParser.java b/src/main/java/org/rometools/feed/module/content/io/ContentModuleParser.java index cc9728a..4123290 100644 --- a/src/main/java/org/rometools/feed/module/content/io/ContentModuleParser.java +++ b/src/main/java/org/rometools/feed/module/content/io/ContentModuleParser.java @@ -121,7 +121,7 @@ public class ContentModuleParser implements com.sun.syndication.io.ModuleParser contentStrings.add(value.getText()); } - ci.setContentValueDOM(value.clone().getContent()); + ci.setContentValueDOM((List) value.clone().getContent()); } if (format != null) { diff --git a/src/main/java/org/rometools/feed/module/slash/SlashImpl.java b/src/main/java/org/rometools/feed/module/slash/SlashImpl.java index 94a263f..81c85c7 100644 --- a/src/main/java/org/rometools/feed/module/slash/SlashImpl.java +++ b/src/main/java/org/rometools/feed/module/slash/SlashImpl.java @@ -149,7 +149,6 @@ public class SlashImpl implements Slash { @Override public boolean equals(final Object obj) { final EqualsBean eBean = new EqualsBean(this.getClass(), this); - return eBean.beanEquals(obj); } } diff --git a/src/main/java/org/rometools/feed/module/sle/GroupStrategy.java b/src/main/java/org/rometools/feed/module/sle/GroupStrategy.java new file mode 100644 index 0000000..f3eae59 --- /dev/null +++ b/src/main/java/org/rometools/feed/module/sle/GroupStrategy.java @@ -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; + } + +} \ No newline at end of file diff --git a/src/main/java/org/rometools/feed/module/sle/SleUtility.java b/src/main/java/org/rometools/feed/module/sle/SleUtility.java index 28d7ad1..0d71a79 100644 --- a/src/main/java/org/rometools/feed/module/sle/SleUtility.java +++ b/src/main/java/org/rometools/feed/module/sle/SleUtility.java @@ -17,12 +17,9 @@ */ package org.rometools.feed.module.sle; -import java.util.ArrayList; -import java.util.Collection; import java.util.List; 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.Sort; @@ -31,7 +28,6 @@ import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.io.FeedException; import com.sun.syndication.io.SyndFeedInput; 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. @@ -44,12 +40,13 @@ import com.sun.syndication.io.impl.ModuleGenerators; * @author Robert "kebernet" Cooper * @see SleEntry */ -public class SleUtility { - private static final String ITEM_MODULE_GENERATORS_POSFIX_KEY = ".item.ModuleGenerator.classes"; +public final class SleUtility { - /** 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() { - super(); } /** @@ -59,14 +56,12 @@ public class SleUtility { * @param groups Group fields (from the SimpleListExtension module) * @return Grouped list of entries. */ - public static List group(final List values, final Group[] groups) { + public static List group(final List values, final Group[] groups) { final SortableList list = values instanceof SortableList ? (SortableList) values : new SortableList(values); final GroupStrategy gs = new GroupStrategy(); - for (int i = groups.length - 1; i >= 0; i--) { list.sortOnProperty(groups[i], true, gs); } - return list; } @@ -78,10 +73,9 @@ public class SleUtility { * @param ascending Sort ascending/descending. * @return Sorted list of values */ - public static List sort(final List values, final Sort sort, final boolean ascending) { + public static List sort(final List values, final Sort sort, final boolean ascending) { final SortableList list = values instanceof SortableList ? (SortableList) values : new SortableList(values); list.sortOnProperty(sort, ascending, new SortStrategy()); - return list; } @@ -94,10 +88,9 @@ public class SleUtility { * @param ascending Sort ascending/descending * @return Grouped and sorted list of entries. */ - public static List sortAndGroup(final List values, final Group[] groups, final Sort sort, final boolean ascending) { - List list = sort(values, sort, ascending); + public static List sortAndGroup(final List values, final Group[] groups, final Sort sort, final boolean ascending) { + List list = sort(values, sort, ascending); list = group(list, groups); - return list; } @@ -108,86 +101,13 @@ public class SleUtility { * structures. It is a very heavy operation and should not be called frequently! */ 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 - 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 Document d = o.outputJDom(feed); final SyndFeed feed2 = new SyndFeedInput().build(d); 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); - } - } - } - } - } - } } diff --git a/src/main/java/org/rometools/feed/module/sle/SortStrategy.java b/src/main/java/org/rometools/feed/module/sle/SortStrategy.java new file mode 100644 index 0000000..dee1fec --- /dev/null +++ b/src/main/java/org/rometools/feed/module/sle/SortStrategy.java @@ -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; + } + +} \ No newline at end of file diff --git a/src/main/java/org/rometools/feed/module/sle/SortableList.java b/src/main/java/org/rometools/feed/module/sle/SortableList.java new file mode 100644 index 0000000..d1c8318 --- /dev/null +++ b/src/main/java/org/rometools/feed/module/sle/SortableList.java @@ -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 { + + 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 = 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); + } + } + } + } + } + +} \ No newline at end of file diff --git a/src/main/java/org/rometools/feed/module/sle/ValueStrategy.java b/src/main/java/org/rometools/feed/module/sle/ValueStrategy.java new file mode 100644 index 0000000..89c7dfa --- /dev/null +++ b/src/main/java/org/rometools/feed/module/sle/ValueStrategy.java @@ -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); + +} \ No newline at end of file diff --git a/src/test/java/org/rometools/feed/module/sle/GroupAndSortTest.java b/src/test/java/org/rometools/feed/module/sle/GroupAndSortTest.java index c443b87..e9a3a6e 100644 --- a/src/test/java/org/rometools/feed/module/sle/GroupAndSortTest.java +++ b/src/test/java/org/rometools/feed/module/sle/GroupAndSortTest.java @@ -8,6 +8,7 @@ package org.rometools.feed.module.sle; import java.io.File; +import java.util.ArrayList; import java.util.List; import junit.framework.Test; @@ -16,6 +17,7 @@ import junit.framework.TestSuite; import org.rometools.feed.module.AbstractTestCase; 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.SyndFeed; 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 SimpleListExtension sle = (SimpleListExtension) feed.getModule(SimpleListExtension.URI); - final List entries = feed.getEntries(); + final List entries = new ArrayList(feed.getEntries()); final Sort[] sortFields = sle.getSortFields(); final Sort sort = sortFields[1]; - List sortedEntries = SleUtility.sort(entries, sort, true); + List sortedEntries = SleUtility.sort(entries, sort, true); SyndEntry entry = (SyndEntry) sortedEntries.get(0); assertEquals("Great Journeys of the Past", entry.getTitle()); 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 SimpleListExtension sle = (SimpleListExtension) feed.getModule(SimpleListExtension.URI); System.out.println("Sorting on " + sle.getSortFields()[0]); - final List sortedEntries = SleUtility.sort(feed.getEntries(), sle.getSortFields()[0], true); + final List entries = new ArrayList(feed.getEntries()); + final List 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());