import java.net.*; import java.io.*; import java.util.*; /** * This class represents a set of Translations in various * languages. */ public class TranslationSet extends Object { //-------------------------------------------- private HashSet set; //-------------------------------------------- public static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- /** construct a new empty set to hold translations which * will be added later, probably by the Record class. */ public TranslationSet() { this.set = new HashSet(); //this.set = new Translation[10]; } //-- constr: () //-------------------------------------------- /** how many translations */ public int count() { return this.set.size(); } //-------------------------------------------- /** is the set empty */ public boolean isEmpty() { if (this.set.size() == 0) { return true; } else { return false; } } //-------------------------------------------- /** return a particular translation based on its language code. * These language codes will be in accordance with whatever * is the applicable RFC and will probably be supplied by the * java 'locale' class which contains all of these codes * built in. */ public String getTranslationByCode(String sLanguageCode) { String sReturn = ""; Translation tCurrentTranslation; Iterator it = this.set.iterator(); while (it.hasNext()) { tCurrentTranslation = (Translation)it.next(); if (tCurrentTranslation.getLanguageCode() == sLanguageCode) { sReturn = tCurrentTranslation.getTranslation(); } } //-- while return sReturn; } //-- getTranslationByCode //-------------------------------------------- /** add a Translation object to the set. Translation objects * will become available for adding as the Record object * parses through the data string. Normally the Record object * will call this method */ public void addTranslation(Translation tTranslation) { this.set.add(tTranslation); } //-------------------------------------------- public String print() { StringBuffer sbReturn = new StringBuffer(""); Translation tCurrentTranslation; Iterator it = this.set.iterator(); while (it.hasNext()) { tCurrentTranslation = (Translation)it.next(); sbReturn.append(tCurrentTranslation.print()); sbReturn.append(NEWLINE); } return sbReturn.toString(); } //-- print //-------------------------------------------- public String toString() { StringBuffer sbReturn = new StringBuffer(""); Translation tCurrentTranslation; Iterator it = this.set.iterator(); while (it.hasNext()) { tCurrentTranslation = (Translation)it.next(); sbReturn.append(tCurrentTranslation.toString()); sbReturn.append(NEWLINE); } return sbReturn.toString(); } /** return a string which displays the data contained * by the object at a particular time, in an * unambiguous format. */ public String debug() { StringBuffer sbReturn = new StringBuffer(""); Translation tCurrentTranslation; Iterator it = this.set.iterator(); while (it.hasNext()) { tCurrentTranslation = (Translation)it.next(); sbReturn.append(tCurrentTranslation.debug()); } return sbReturn.toString(); } //-- method: debug //-------------------------------------------- /** A main method for console testing */ public static void main(String[] args) throws Exception { StringBuffer sbUsageMessage = new StringBuffer(""); sbUsageMessage.append("usage: java TranslationSet x"); sbUsageMessage.append(TranslationSet.NEWLINE); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } TranslationSet ts = new TranslationSet(); ts.addTranslation(new Translation("tr=[en]how far is it")); ts.addTranslation(new Translation("tr=[es]es muy lejos o no")); ts.addTranslation(new Translation("tr=[fr]cest tres loin")); System.out.println(ts.toString()); } //-- main() } //-- TranslationSet class