Last active
April 12, 2016 02:45
-
-
Save dvimont/a0d44c9a29a23a61ee6c63034dd4e7d8 to your computer and use it in GitHub Desktop.
OrderedSet usage examples (for https://github.com/dvimont/OrderedSet)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.commonvox.collections.gists; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.Date; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
import org.commonvox.collections.KeyComponentProfile; | |
import org.commonvox.collections.OrderedSet; | |
/** | |
* Simple examples of constructing and querying an OrderedSet. | |
*/ | |
public class OrderedSetGist { | |
public void examplesOfOrderedSetUsage() throws NoSuchMethodException { | |
orderBooksByAuthorAndTitle(); | |
orderBooksByGenreNewestToOldest(); | |
} | |
/** | |
* EXAMPLE 1 | |
*/ | |
public void orderBooksByAuthorAndTitle() { | |
//-- Construct and populate OrderedSet --// | |
OrderedSet<Book> booksByAuthorAndTitle | |
= new OrderedSet<>( | |
new KeyComponentProfile<>(Book.class, Author.class), | |
new KeyComponentProfile<>(Book.class, Book.Title.class)); | |
booksByAuthorAndTitle.addAll(getRandomOrderBookCollection()); | |
//-- Print results hierarchically --// | |
System.out.println("\n========\nBooks ordered by AUTHOR and TITLE\n========"); | |
printHierarchically(booksByAuthorAndTitle); | |
} | |
/** | |
* EXAMPLE 2 | |
*/ | |
public void orderBooksByGenreNewestToOldest() throws NoSuchMethodException { | |
//-- Construct KeyComponentProfiles (one w/ Comparator & specific get method specified) --// | |
KeyComponentProfile<Book> genreKeyComponent | |
= new KeyComponentProfile<>(Book.class, Genre.class); | |
KeyComponentProfile<Book> publicationDateKeyComponent | |
= new KeyComponentProfile<>(Book.class, Date.class, | |
(date1, date2) -> date2.compareTo(date1), // DESCENDING Date Comparator | |
Book.class.getDeclaredMethod("getPublicationDate")); // limit Date.class focus to THIS method! | |
KeyComponentProfile<Book> titleKeyComponent | |
= new KeyComponentProfile<>(Book.class, Book.Title.class); | |
//-- Construct and populate OrderedSet --// | |
OrderedSet<Book> booksByGenreAndPublicationDateAndTitle | |
= new OrderedSet<>(getRandomOrderBookCollection(), | |
genreKeyComponent, publicationDateKeyComponent, titleKeyComponent); | |
//-- Print results hierarchically --// | |
System.out.println("\n========\nBooks by Genre, listed NEWEST to OLDEST\n========"); | |
printHierarchically(booksByGenreAndPublicationDateAndTitle); | |
//-- Print all Genres in natural order --// | |
System.out.println("==========\nGENRE list\n=========="); | |
for (Object genre : booksByGenreAndPublicationDateAndTitle.keyComponentSet(genreKeyComponent)) { | |
System.out.println(genre); | |
} | |
} | |
void printHierarchically(OrderedSet<?> orderedSet) { | |
final String TAB = " "; | |
List<Object> previousKeyComponents = Arrays.asList("", "", "", "", "", "", "", "", "", ""); | |
for (Map.Entry<List<Object>,?> entry : orderedSet.entrySet()){ | |
List<Object> keyComponents = entry.getKey(); | |
Iterator<Object> keyComponentIterator = keyComponents.iterator(); | |
Iterator<Object> previousKeyComponentIterator = previousKeyComponents.iterator(); | |
int tabCount = 0; | |
boolean printRemainingComponents = false; | |
while (keyComponentIterator.hasNext()) { | |
Object keyComponent = keyComponentIterator.next(); | |
if (!keyComponent.equals(previousKeyComponentIterator.next()) || printRemainingComponents) { | |
printRemainingComponents = true; | |
for (int i = 0; i < tabCount; i++) { | |
System.out.print(TAB); | |
} | |
if (Date.class.isAssignableFrom(keyComponent.getClass())) { | |
System.out.println("DATE PUBLISHED: " + keyComponent); | |
} else { | |
System.out.println(keyComponent); | |
} | |
} | |
tabCount++; | |
} | |
previousKeyComponents = keyComponents; | |
} | |
System.out.println("==============="); | |
} | |
//========================================================================== | |
// CONSTRUCT SAMPLE LIST OF BOOKS IN RANDOM ORDER | |
//========================================================================== | |
private Collection<Book> getRandomOrderBookCollection() { | |
return new ArrayList<>(Arrays.asList( | |
new Book(1, "Adventures of Huckleberry Finn", | |
Arrays.asList(new Genre("Fiction"), new Genre("Adventure")), | |
Arrays.asList(new Author("Twain", "Mark")), | |
"1884-12-10", "1912-01-01"), | |
new Book(2, "Merriam-Webster Dictionary", | |
Arrays.asList(new Genre("Nonfiction"), new Genre("Reference")), | |
Arrays.asList(new Author("Webster", "Noah"), | |
new Author("Merriam", "George")), | |
"1840-01-01", "1864-01-01"), | |
new Book(3, "Advice to Youth", | |
Arrays.asList(new Genre("Nonfiction"), new Genre("Satire")), | |
Arrays.asList(new Author("Twain", "Mark")), | |
"1882-01-01", null), | |
new Book(4, "Lucifer's Hammer", | |
Arrays.asList(new Genre("Fiction"), new Genre("Science Fiction"), | |
new Genre("Adventure")), | |
Arrays.asList(new Author("Niven", "Larry"), new Author("Pournelle", "Jerry")), | |
"1977-01-01", "1993-01-01"), | |
new Book(5, "Slaughterhouse-Five", | |
Arrays.asList(new Genre("Fiction"), new Genre("Science Fiction"), | |
new Genre("Satire")), | |
Arrays.asList(new Author("Vonnegut", "Kurt")), | |
"1969-03-01", "1972-01-01"), | |
new Book(6, "Dissertation on the English Language", | |
Arrays.asList(new Genre("Nonfiction"), new Genre("Essay")), | |
Arrays.asList(new Author("Webster", "Noah")), | |
"1789-01-01", "1793-01-01"), | |
new Book(7, "Man Without a Country", | |
Arrays.asList(new Genre("Nonfiction"), new Genre("Satire")), | |
Arrays.asList(new Author("Vonnegut", "Kurt")), | |
"2005-01-01", "2007-01-01") | |
)); | |
} | |
//======================== | |
// CLASS DEFINITIONS | |
//======================== | |
public class Book implements Comparable<Book> { | |
int bookId; | |
Title title; | |
List<Genre> genres; | |
List<Author> authors; | |
Date revisionPublicationDate; | |
Date publicationDate; | |
final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); | |
public Book(int bookId, String title, List<Genre> genres, List<Author> authors, | |
String publicationDate, String revisionPublicationDate) { | |
this.bookId = bookId; | |
this.title = new Title(title); | |
this.genres = genres; | |
this.authors = authors; | |
try { this.publicationDate = DATE_FORMAT.parse(publicationDate); | |
} catch (ParseException e) {} | |
if (revisionPublicationDate == null) { | |
this.revisionPublicationDate = null; | |
} else { | |
try { this.revisionPublicationDate = DATE_FORMAT.parse(revisionPublicationDate); | |
} catch (ParseException e) {} | |
} | |
} | |
public Title getTitle() { | |
return title; | |
} | |
public List<Genre> getGenres() { | |
return genres; | |
} | |
public List<Author> getAuthors() { | |
return authors; | |
} | |
public Date getPublicationDate() { | |
return publicationDate; | |
} | |
public Date getRevisionPublicationDate() { | |
return revisionPublicationDate; | |
} | |
@Override | |
public int compareTo(Book other) { | |
return (new Integer(bookId)).compareTo(other.bookId); | |
} | |
@Override | |
public String toString() { | |
return "BOOK #" + bookId + " (" + title + ")"; | |
} | |
public class Title implements Comparable<Title> { | |
String title; | |
public Title(String title) { | |
this.title = title; | |
} | |
@Override | |
public int compareTo(Title other) { | |
return this.title.toLowerCase().compareTo(other.title.toLowerCase()); | |
} | |
@Override | |
public String toString() { | |
return "TITLE: " + title; | |
} | |
} | |
} | |
public class Author implements Comparable<Author> { | |
String lastName, firstName; | |
public Author(String lastName, String firstName) { | |
this.lastName = lastName; | |
this.firstName = firstName; | |
} | |
@Override | |
public int compareTo(Author other) { | |
int returnedInt = this.lastName.toLowerCase().compareTo(other.lastName.toLowerCase()); | |
if (returnedInt == 0) { | |
returnedInt = this.firstName.toLowerCase().compareTo(other.firstName.toLowerCase()); | |
} | |
return returnedInt; | |
} | |
@Override | |
public String toString() { | |
return "AUTHOR: " + firstName + " " + lastName; | |
} | |
} | |
public class Genre implements Comparable<Genre> { | |
String genre; | |
public Genre(String genre) { | |
this.genre = genre; | |
} | |
@Override | |
public int compareTo(Genre other) { | |
return this.genre.toLowerCase().compareTo(other.genre.toLowerCase()); | |
} | |
@Override | |
public String toString() { | |
return "GENRE: " + genre; | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
new OrderedSetGist().examplesOfOrderedSetUsage(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
======== | |
Books ordered by AUTHOR and TITLE | |
======== | |
AUTHOR: George Merriam | |
TITLE: Merriam-Webster Dictionary | |
BOOK #2 (TITLE: Merriam-Webster Dictionary) | |
AUTHOR: Larry Niven | |
TITLE: Lucifer's Hammer | |
BOOK #4 (TITLE: Lucifer's Hammer) | |
AUTHOR: Jerry Pournelle | |
TITLE: Lucifer's Hammer | |
BOOK #4 (TITLE: Lucifer's Hammer) | |
AUTHOR: Mark Twain | |
TITLE: Adventures of Huckleberry Finn | |
BOOK #1 (TITLE: Adventures of Huckleberry Finn) | |
TITLE: Advice to Youth | |
BOOK #3 (TITLE: Advice to Youth) | |
AUTHOR: Kurt Vonnegut | |
TITLE: Man Without a Country | |
BOOK #7 (TITLE: Man Without a Country) | |
TITLE: Slaughterhouse-Five | |
BOOK #5 (TITLE: Slaughterhouse-Five) | |
AUTHOR: Noah Webster | |
TITLE: Dissertation on the English Language | |
BOOK #6 (TITLE: Dissertation on the English Language) | |
TITLE: Merriam-Webster Dictionary | |
BOOK #2 (TITLE: Merriam-Webster Dictionary) | |
=============== | |
======== | |
Books by Genre, listed NEWEST to OLDEST | |
======== | |
GENRE: Adventure | |
DATE PUBLISHED: Sat Jan 01 00:00:00 JST 1977 | |
TITLE: Lucifer's Hammer | |
BOOK #4 (TITLE: Lucifer's Hammer) | |
DATE PUBLISHED: Wed Dec 10 00:00:00 JST 1884 | |
TITLE: Adventures of Huckleberry Finn | |
BOOK #1 (TITLE: Adventures of Huckleberry Finn) | |
GENRE: Essay | |
DATE PUBLISHED: Thu Jan 01 00:00:00 JST 1789 | |
TITLE: Dissertation on the English Language | |
BOOK #6 (TITLE: Dissertation on the English Language) | |
GENRE: Fiction | |
DATE PUBLISHED: Sat Jan 01 00:00:00 JST 1977 | |
TITLE: Lucifer's Hammer | |
BOOK #4 (TITLE: Lucifer's Hammer) | |
DATE PUBLISHED: Sat Mar 01 00:00:00 JST 1969 | |
TITLE: Slaughterhouse-Five | |
BOOK #5 (TITLE: Slaughterhouse-Five) | |
DATE PUBLISHED: Wed Dec 10 00:00:00 JST 1884 | |
TITLE: Adventures of Huckleberry Finn | |
BOOK #1 (TITLE: Adventures of Huckleberry Finn) | |
GENRE: Nonfiction | |
DATE PUBLISHED: Sat Jan 01 00:00:00 JST 2005 | |
TITLE: Man Without a Country | |
BOOK #7 (TITLE: Man Without a Country) | |
DATE PUBLISHED: Sun Jan 01 00:00:00 JST 1882 | |
TITLE: Advice to Youth | |
BOOK #3 (TITLE: Advice to Youth) | |
DATE PUBLISHED: Wed Jan 01 00:00:00 JST 1840 | |
TITLE: Merriam-Webster Dictionary | |
BOOK #2 (TITLE: Merriam-Webster Dictionary) | |
DATE PUBLISHED: Thu Jan 01 00:00:00 JST 1789 | |
TITLE: Dissertation on the English Language | |
BOOK #6 (TITLE: Dissertation on the English Language) | |
GENRE: Reference | |
DATE PUBLISHED: Wed Jan 01 00:00:00 JST 1840 | |
TITLE: Merriam-Webster Dictionary | |
BOOK #2 (TITLE: Merriam-Webster Dictionary) | |
GENRE: Satire | |
DATE PUBLISHED: Sat Jan 01 00:00:00 JST 2005 | |
TITLE: Man Without a Country | |
BOOK #7 (TITLE: Man Without a Country) | |
DATE PUBLISHED: Sat Mar 01 00:00:00 JST 1969 | |
TITLE: Slaughterhouse-Five | |
BOOK #5 (TITLE: Slaughterhouse-Five) | |
DATE PUBLISHED: Sun Jan 01 00:00:00 JST 1882 | |
TITLE: Advice to Youth | |
BOOK #3 (TITLE: Advice to Youth) | |
GENRE: Science Fiction | |
DATE PUBLISHED: Sat Jan 01 00:00:00 JST 1977 | |
TITLE: Lucifer's Hammer | |
BOOK #4 (TITLE: Lucifer's Hammer) | |
DATE PUBLISHED: Sat Mar 01 00:00:00 JST 1969 | |
TITLE: Slaughterhouse-Five | |
BOOK #5 (TITLE: Slaughterhouse-Five) | |
=============== | |
========== | |
GENRE list | |
========== | |
GENRE: Adventure | |
GENRE: Essay | |
GENRE: Fiction | |
GENRE: Nonfiction | |
GENRE: Reference | |
GENRE: Satire | |
GENRE: Science Fiction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment