Create a gist now

Instantly share code, notes, and snippets.

IndexedCollection simple usage example
package org.commonvox.indexedcollectionexample;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.commonvox.indexedcollection.CompositeIndex;
import org.commonvox.indexedcollection.IndexedCollection;
/**
* This is a simple example of constructing and querying an IndexedCollection.
* An IndexedCollection serves to "sort" a standard Java SE8 collection based on the natural order of
* multiple programmer-defined groups of its attributes. (Think composite-indexes in an RDBMS.)
* This example indexes a collection of Book objects in several ways: (1) by title; (2) by
* author/title; (3) by genre/author/title.
* IndexedCollection.jar is available here: https://github.com/dvimont/IndexedCollection/releases
* A full, open source desktop application which makes extensive use of IndexedCollection is
* available here: https://github.com/dvimont/LibriVoxExplorer
* @author Daniel Vimont
*/
public class IndexedCollectionUsageExample {
public void example() {
//-- CONSTRUCT IndexedCollection --//
IndexedCollection<Book> indexedCollection
= new IndexedCollection<>
(Book.class, getRandomOrderBookList(),
new CompositeIndex<>
(Book.class, "Books by Title",
Book.Title.class),
new CompositeIndex<>
(Book.class, "Books by Author/Title",
Author.class, Book.Title.class),
new CompositeIndex<>
(Book.class, "Books by Genre/Author/Title",
Genre.class, Author.class, Book.Title.class) );
//-- QUERY IndexedCollection --//
System.out.println("Books in ID order\n============");
for (Book book : indexedCollection.getValues(Book.class)) {
System.out.println(book); }
System.out.println("Books in TITLE order\n============");
for (Book book : indexedCollection.getValues(Book.class, Book.Title.class)) {
System.out.println(book); }
System.out.println("Books grouped by AUTHOR, in TITLE order\n============");
for (Comparable author : indexedCollection.getAttributeValues(Author.class)) {
System.out.println("Books by " + author + "\n------");
for (Book book : indexedCollection.getValues
(Book.class, author, Book.Title.class)) {
System.out.println(book); } }
System.out.println("BOOKS grouped by GENRE, in AUTHOR order\n============");
for (Comparable genre : indexedCollection.getAttributeValues(Genre.class)) {
System.out.println
("Books in <" + genre + "> genre, ordered by author\n------");
for (Book book : indexedCollection.getValuesSuppressConsecutiveDuplicates
(Book.class, genre, Author.class)) {
System.out.println(book); } }
}
//==========================================================================
// CONSTRUCT SAMPLE LIST OF BOOKS IN RANDOM ORDER
//==========================================================================
private List<Book> getRandomOrderBookList () {
return new ArrayList<Book>() {{
add(new Book("Adventures of Huckleberry Finn",
Arrays.asList(new Genre("Fiction"), new Genre("Adventure")),
Arrays.asList(new Author("Twain","Mark")) ));
add(new Book("Merriam-Webster Dictionary",
Arrays.asList(new Genre("Nonfiction"), new Genre("Reference")),
Arrays.asList(new Author("Webster","Noah"),new Author("Merriam","George")) ));
add(new Book("Through the Brazilian Wilderness",
Arrays.asList(new Genre("Nonfiction"), new Genre("Adventure")),
Arrays.asList(new Author("Roosevelt","Theodore")) ));
add(new Book("Advice to Youth",
Arrays.asList(new Genre("Nonfiction"), new Genre("Satire")),
Arrays.asList(new Author("Twain","Mark")) ));
add(new Book("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")) ));
add(new Book("Slaughterhouse-Five",
Arrays.asList(new Genre("Fiction"), new Genre("Science Fiction"),
new Genre("Satire")),
Arrays.asList(new Author("Vonnegut","Kurt")) ));
add(new Book("Dissertation on the English Language",
Arrays.asList(new Genre("Nonfiction"), new Genre("Essay")),
Arrays.asList(new Author("Webster","Noah")) ));
add(new Book("Man Without a Country",
Arrays.asList(new Genre("Nonfiction"), new Genre("Satire")),
Arrays.asList(new Author("Vonnegut","Kurt")) ));
}};
}
//========================
// CLASS DEFINITIONS
//========================
static int bookIdGenerator = 0;
public class Book implements Comparable<Book> {
int bookId;
Title title;
List<Genre> genres;
List<Author> authors;
public Book (String title, List<Genre> genres, List<Author> authors) {
this.bookId = ++bookIdGenerator;
this.title = new Title(title);
this.genres = genres;
this.authors = authors;
}
public Title getTitle() {
return title;
}
public List<Genre> getGenres() {
return genres;
}
public List<Author> getAuthors() {
return authors;
}
@Override
public int compareTo(Book other) {
return (new Integer(bookId)).compareTo(other.bookId);
}
@Override
public String toString() {
final String INDENT = " ";
StringBuilder output = new StringBuilder();
output.append("TITLE: ").append(title).append("\n");
for (Author author : authors) {
output.append(INDENT).append("AUTHOR: ").append(author).append("\n");
}
for (Genre genre : genres) {
output.append(INDENT).append("GENRE: ").append(genre).append("\n");
}
return output.toString();
}
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; }
}
}
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 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; }
}
public static void main(String[] args) throws Exception {
new IndexedCollectionUsageExample().example();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment