abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r432762 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/model/Feed.java parser/src/main/java/org/apache/abdera/parser/stax/FOMFeed.java
Date Fri, 18 Aug 2006 22:49:11 GMT
Author: jmsnell
Date: Fri Aug 18 15:49:10 2006
New Revision: 432762

URL: http://svn.apache.org/viewvc?rev=432762&view=rev
Log:
Add a way of sorting entries by atom:updated value as suggested by Danny Ayers

Add new sortEntriesByUpdated() and sortEntries() methods.  

Lists of entries provided by Feed are not sortable using Collections.sort() because of the

underlying iterator implementation of FOMList and Axiom.  So this gives us essentially the

same thing but implemented in a way that does work for Axiom.

I wouldn't recommend trying to sort really large collections of entries, however.

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Feed.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMFeed.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Feed.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Feed.java?rev=432762&r1=432761&r2=432762&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Feed.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Feed.java Fri Aug
18 15:49:10 2006
@@ -17,6 +17,7 @@
 */
 package org.apache.abdera.model;
 
+import java.util.Comparator;
 import java.util.List;
 
 /**
@@ -125,4 +126,15 @@
    * Creates a Source element from this Feed
    */
   Source getAsSource();
+  
+  /**
+   * Sorts entries by the atom:updated property
+   * @param new_first If true, entries with newer atom:updated values will come first
+   */
+  void sortEntriesByUpdated(boolean new_first);
+  
+  /**
+   * Sorts entries using the given comparator
+   */
+  void sortEntries(Comparator<Entry> comparator);
 }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMFeed.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMFeed.java?rev=432762&r1=432761&r2=432762&view=diff
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMFeed.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMFeed.java
Fri Aug 18 15:49:10 2006
@@ -17,6 +17,9 @@
 */
 package org.apache.abdera.parser.stax;
 
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 
@@ -138,4 +141,33 @@
     super.addChild(node);
   }
  
+  public void sortEntriesByUpdated(boolean new_first) {
+    sortEntries(new UpdatedComparator(new_first));
+  }
+  
+  public void sortEntries(Comparator<Entry> comparator) {
+    if (comparator == null) return;
+    List<Entry> entries = this.getEntries();
+    Entry[] a = entries.toArray(new Entry[entries.size()]);
+    Arrays.sort(a, comparator);
+    for (Entry e: entries) { e.discard(); }
+    for (Entry e: a) { addEntry(e); }
+  }
+  
+  private static class UpdatedComparator implements Comparator<Entry> {
+    private boolean new_first = true;
+    UpdatedComparator(boolean new_first) {
+      this.new_first = new_first;
+    }
+    public int compare(Entry o1, Entry o2) {
+      Date d1 = o1.getUpdated();
+      Date d2 = o2.getUpdated();
+      if (d1 == null && d2 == null) return 0;
+      if (d1 == null && d2 != null) return -1;
+      if (d1 != null && d2 == null) return 1;
+      int r = d1.compareTo(d2);
+      return (new_first) ? -r : r;
+    }
+  };
+
 }



Mime
View raw message