Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import exceptions.DernierMotRechercheException;
- import exceptions.DicoVideException;
- import exceptions.MotDejaPresentException;
- import exceptions.MotNonPresentException;
- import java.util.Iterator;
- import java.util.SortedSet;
- import java.util.TreeSet;
- /**
- * Implémentation d'un dictionnaire en utilisant une ensemble ordonné.
- */
- public class DicoTreeSet implements iDico {
- private TreeSet<String> set;
- public DicoTreeSet() {
- set = new TreeSet<>();
- }
- @Override
- public boolean dicoVide() {
- return set.isEmpty();
- }
- @Override
- public boolean appartient(String mot) {
- return set.contains(mot);
- }
- @Override
- public void ajoutMot(String mot) throws MotDejaPresentException {
- if (appartient(mot))
- throw new MotDejaPresentException();
- set.add(mot);
- }
- @Override
- public int nbMots() {
- return set.size();
- }
- @Override
- public String premierMot() throws DicoVideException {
- if(dicoVide())
- throw new DicoVideException();
- return set.first();
- }
- @Override
- public String dernierMot() throws DicoVideException {
- if(dicoVide())
- throw new DicoVideException();
- return set.last();
- }
- @Override
- public String motSuivant(String mot) throws DernierMotRechercheException, MotNonPresentException {
- if (!appartient(mot))
- throw new MotNonPresentException();
- if (set.higher(mot)==null)
- throw new DernierMotRechercheException();
- return set.higher(mot);
- }
- @Override
- public void suppressionMot(String mot) throws MotNonPresentException {
- if (!appartient(mot))
- throw new MotNonPresentException();
- set.remove(mot);
- }
- @Override
- public Iterator<String> iterator() {
- return set.iterator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement