import java.util.*;

public class TextAbstraction {
    private boolean locked = false;
    private String[] sorted_word_list = null;
    private Vector original = new Vector();
    
    public void addWord(String word) { 
	if( locked ) {
	    throw new IllegalStateException("Attempt to call addWord() on locked TextAbstraction object");
	}
	
	original.add(word);
    }
    
    public void lock() {
	if( locked ) { return; } // already locked
	
	locked = true;
	Collections.sort( original ); //order the words
	
	// weed out duplicates
	for(int i=0; i < original.size()-1; i++) {
	    String thisword = (String)original.get(i);
	    if( thisword.equals((String)original.get(i+1)) ) {
		original.remove(i);
		i--;
	    }
	}
	
	sorted_word_list = new String[original.size()];
	for(int i=0; i<original.size(); i++) {
	    sorted_word_list[i] = (String)original.get(i);
	}
    }

    public int word2int(String word) {
	if(!locked) {
	    throw new IllegalStateException("Attempt to call word2int() before the TextAbstraction object was locked");
	}
	
	int index = Arrays.binarySearch(sorted_word_list, word);
	if( index < 0 ) return -1;
	else return index;
    }
    
    public String int2word(int value) {
	if(!locked) {
	    throw new IllegalStateException("Attempt to call int2word() before the TextAbstraction object was locked");
	}
	
	return sorted_word_list[value];
    }
    
    public int size() { return sorted_word_list.length; }

}
