Back to project page book.
The source code is released under:
MIT License
If you think the Android project book listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package book; // w w w . j av a 2s. c o m import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.Db; /* * * book object * contains key -> words pairs and * some appropriate methods for doing things with them * */ public class Book { private long id; // database id of the book private String name; private HashMap<String, List<String>> words; private Db db; public Book(Db db){ this.db = db; this.words = new HashMap<String, List<String>>(); } public Book(String name){ this.setName(name); this.words = new HashMap<String, List<String>>(); } public Book(long id, String name){ this.setId(id); this.setName(name); this.words = new HashMap<String, List<String>>(); } // save the booooook public void save(){ this.db.save(this); } // very handy method public void load(){ } // delete.. public void remove(){ this.db.remove(this); } public void setDb(Db db){ this.db = db; } // loads a string of words into multiple words (keys) into this book public void setString(String text){ this.words = new HashMap<String, List<String>>(); String[] txt = text.split(" "); for (String t : txt){ this.addWord(t); } } // clean up public void Clear(){ this.words = new HashMap<String, List<String>>(); } // overloading addword method public void addWord(String word, List<String> translations){ if (!this.hasWord(word)){ words.put(word, translations); } } // check if this already has the word.. regardless of translations.. public boolean hasWord(String word){ return this.words.containsKey(word); } public List<String> getWordsAsList(){ List<String> wlist = new ArrayList<String>(); for (String word : words.keySet()){ wlist.add(word); } return wlist; } public void addWord(String word){ if (!this.words.containsKey(word)){ this.words.put(word, new ArrayList<String>()); } } // combines two books into one (this) public void merge(Book book){ HashMap<String ,List<String>> words = book.getWords(); for (String word : words.keySet()){ this.addWord(word, words.get(word)); } } // add some words to this public void addWords(List<String> words){ for (String word : words ){ this.addWord(word); } } // remove a word public void removeWord(String word){ if (this.hasWord(word)){ words.remove(word); } this.db.removeWord(this.id, word); } // add translations into this book by different ways public void addTranslations(String word, List<String> translations){ for (String translation : translations){ this.addTranslation(word, translation); } } public void addTranslation(String word, String translation){ this.addWord(word); if (!this.words.get(word).contains(translation)){ this.words.get(word).add(translation); } } // word is this public void setWords(HashMap<String, List<String>> words){ this.words = words; } // speak public HashMap<String, List<String>> getWords(){ return words; } // count em public int getCount(){ return this.words.size(); } // captain obvious public String getName() { return name; } // set my name public void setName(String name) { this.name = name; } // long id is long public long getId() { return id; } // long id is quite long public void setId(long id) { this.id = id; } }