Removed/commented out/tagged some unused local variables.

Added a JUnit test for the new Trie that fails at present since the Trie is
case-insensitive.  Running JUnit tests is not something our build system
knows about at present, but Eclipse 2.0 makes it very easy.

Fixed a few compiler errors due to imports I'd forgotten.
This commit is contained in:
dchandler 2002-11-02 16:01:40 +00:00
parent b8391e923d
commit a6cc4a7ff3
31 changed files with 292 additions and 179 deletions

View file

@ -21,6 +21,8 @@ package org.thdl.media;
import java.lang.reflect.*;
import java.util.*;
import org.thdl.util.ThdlDebug;
public class SmartPlayerFactory {
/** You cannot instantiate this class. */
private SmartPlayerFactory() { }
@ -33,7 +35,7 @@ public class SmartPlayerFactory {
for (int i=0; i<possiblePlayers.length; i++) {
try {
Class mediaClass = Class.forName(possiblePlayers[i]);
playerClasses.add(mediaClass);
//FIXME: playerClasses.add(mediaClass);
SmartMoviePanel smp = (SmartMoviePanel)mediaClass.newInstance();
moviePlayers.add(smp);
} catch (ClassNotFoundException cnfe) {

View file

@ -208,7 +208,7 @@ public class SmartQT4JPlayer extends SmartMoviePanel
//mechanism of internally of the TimeCallback.
//Its recomended to use this Timer class to do callbacks , which would take care of the time base time changes and
//recscheduling of the tickle method .
Timer timer = new Timer(1, 2, new Tickler(), getMovie() );
Timer timer = new Timer(1, 2, new Tickler(), getMovie() ); //FIXME: is this used?
// timer.setActive(true);
}

View file

@ -96,8 +96,8 @@ public class QD extends JDesktopPane {
protected DuffPane sharedDP = new DuffPane();
protected DuffPane sharedDP2 = new DuffPane();
protected AbstractDocument findDoc = null;
protected AbstractDocument replaceDoc = null;
protected StyledDocument findDoc = null;
protected StyledDocument replaceDoc = null;
protected KeyStroke cutKey, copyKey, pasteKey, selectAllKey;
protected KeyStroke insert1TimeKey, insert2TimesKey, insertSpeakerKey;
@ -700,7 +700,7 @@ public void setMedia(URL url) {
}
public Project() {
JPanel p = new JPanel(new GridLayout(2,2));
JPanel p = new JPanel(new GridLayout(2,2)); //FIXME: is this used?
titleField = new JTextField();
int preferredHeight = titleField.getPreferredSize().height;
titleField.setPreferredSize(new Dimension(300, preferredHeight));
@ -775,11 +775,7 @@ public void changeKeyboard(JskadKeyboard kbd) {
public boolean saveTranscript() {
currentWork.stopWork();
//URGENT!! fix saving problem
//change keyboard back to wylie for a second
DuffPane dp = (DuffPane)pane;
//URGENT!! fix saving problem (FIXME: grep for URGENT and fix or Fix or FIX)
org.jdom.Element title = new org.jdom.Element("title");
title.setText(project.getTitle().trim());
@ -840,7 +836,6 @@ public boolean saveTranscript() {
org.jdom.Element text = new org.jdom.Element("text");
TibetanDocument doc = (TibetanDocument)pane.getDocument();
ImageIcon[] icons = sd.getSpeakerIcons();
int lastPoint = 0;
int k;
for (k=0; k<doc.getLength(); k++) {
@ -893,7 +888,7 @@ public boolean saveTranscript() {
File htmlOut = new File(f.substring(0, f.lastIndexOf('.')) + ".html");
transformer.transform(new JDOMSource(qdDoc), new StreamResult(new FileOutputStream(htmlOut)));
//URGENT FIX SAVING PROBLEM
//URGENT FIX SAVING PROBLEM (FIXME)
return true;
} catch (FileNotFoundException fnfe) {
@ -954,7 +949,7 @@ public boolean loadTranscript(File t) {
project = new Project();
//change keyboard back to wylie for a second
DuffPane dp = (DuffPane)pane;
// DuffPane dp = (DuffPane)pane;
//URGENT FIX LOADING PROBLEM
@ -1022,20 +1017,20 @@ if (keyboard_url != null) {
}
org.jdom.Element text = qd.getChild("text");
// DuffPane dp = (DuffPane)pane;
DuffPane dp = (DuffPane)pane;
TibetanDocument tDoc = (TibetanDocument)dp.getDocument();
if (tDoc.getLength() > 0)
dp.setText("");
java.util.List textContent = text.getContent();
ImageIcon[] icons = sd.getSpeakerIcons();
Iterator textIter = textContent.iterator();
boolean wasLastComponent = true;
// boolean wasLastComponent = true;
while (textIter.hasNext()) {
Object nextContent = textIter.next();
if (nextContent instanceof org.jdom.Text) {
String wylie = ((org.jdom.Text)nextContent).getText();
dp.toTibetanMachineWeb(wylie, tDoc.getLength());
wasLastComponent = false;
// wasLastComponent = false;
}
else if (nextContent instanceof org.jdom.Element) {
org.jdom.Element e = (org.jdom.Element)nextContent;
@ -1050,7 +1045,7 @@ if (keyboard_url != null) {
}
*/
new TimePoint(dp, clockIcon, Integer.valueOf(e.getAttributeValue("t")));
wasLastComponent = true;
// wasLastComponent = true;
}
else if (e.getName().equals("who")) {
/* if (!wasLastComponent)
@ -1062,7 +1057,7 @@ if (keyboard_url != null) {
}
*/
dp.insertComponent(new JLabel(" ", icons[Integer.parseInt(e.getAttributeValue("id"))], SwingConstants.LEFT));
wasLastComponent = true;
// wasLastComponent = true;
}
}
}
@ -1534,7 +1529,7 @@ public JMenuBar getTextMenuBar() {
return bar;
}
public int findNextText(int startPos, TibetanDocument sourceDoc, TibetanDocument findDoc) {
public int findNextText(int startPos, StyledDocument sourceDoc, StyledDocument findDoc) {
if (startPos<0 || startPos>sourceDoc.getLength()-1)
return -1;

View file

@ -146,8 +146,6 @@ public final class JdkVersionHacks {
// f.getToolkit().isFrameStateSupported(Frame.MAXIMIZED_BOTH)
// if possible.
boolean success = false;
Method isFSSMethod = null;
try {
isFSSMethod

View file

@ -262,7 +262,7 @@ public class Savant extends JDesktopPane
fullScreen.getContentPane().setBackground(Color.black);
Dimension screenSize = fullScreen.getToolkit().getScreenSize();
Dimension videoSize = sp.getVisualComponent().getPreferredSize();
Dimension controlSize = sp.getControlComponent().getPreferredSize();
// Dimension controlSize = sp.getControlComponent().getPreferredSize();
int videoWidth = videoSize.width;
int videoHeight = videoSize.height;
float vWidth = new Integer(videoWidth).floatValue();

View file

@ -80,11 +80,11 @@ public class TibetanEnglish implements TranscriptView
Element current = null;
DuffData[] dd;
DuffData[] space = TibTextUtils.getTibetanMachineWeb("_");
// DuffData[] space = TibTextUtils.getTibetanMachineWeb("_");
MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setForeground(mas, Color.blue);
Position endPos = null;
int wherestart;
// int wherestart;
TibetanDocument doc = new TibetanDocument(new StyleContext());
@ -111,7 +111,7 @@ public class TibetanEnglish implements TranscriptView
}
else
{
wherestart = endPos.getOffset();
// wherestart = endPos.getOffset();
doc.insertString(endPos.getOffset(), wylie, mas);
}
@ -153,7 +153,7 @@ public class TibetanEnglish implements TranscriptView
{
doc.insertString(endPos.getOffset(), "\n", null);
String wylie = current.getAttributeValue("who");
wherestart = endPos.getOffset();
// wherestart = endPos.getOffset();
doc.insertString(endPos.getOffset(), wylie, mas);
if (iter.hasNext())
current = (org.jdom.Element)iter.next();

View file

@ -81,11 +81,11 @@ public class TibetanWylie implements TranscriptView
String wylie;
DuffData[] dd;
DuffData[] space = TibTextUtils.getTibetanMachineWeb("_");
// DuffData[] space = TibTextUtils.getTibetanMachineWeb("_");
MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setForeground(mas, Color.blue);
Position endPos = null;
int wherestart;
// int wherestart;
TibetanDocument doc = new TibetanDocument(new StyleContext());
@ -112,7 +112,7 @@ public class TibetanWylie implements TranscriptView
}
else
{
wherestart = endPos.getOffset();
// wherestart = endPos.getOffset();
doc.insertString(endPos.getOffset(), wylie, mas);
}
@ -154,7 +154,7 @@ public class TibetanWylie implements TranscriptView
{
doc.insertString(endPos.getOffset(), "\n", null);
wylie = current.getAttributeValue("who");
wherestart = endPos.getOffset();
// wherestart = endPos.getOffset();
doc.insertString(endPos.getOffset(), wylie, mas);
if (iter.hasNext())
current = (org.jdom.Element)iter.next();

View file

@ -80,13 +80,13 @@ public class TibetanWylieEnglish implements TranscriptView
Element current = null;
DuffData[] dd;
DuffData[] space = TibTextUtils.getTibetanMachineWeb("_");
// DuffData[] space = TibTextUtils.getTibetanMachineWeb("_");
MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setForeground(mas, Color.blue);
MutableAttributeSet mas2 = new SimpleAttributeSet();
StyleConstants.setItalic(mas2, true);
Position endPos = null;
int wherestart;
// int wherestart;
String wylie;
TibetanDocument doc = new TibetanDocument(new StyleContext());
@ -113,7 +113,7 @@ public class TibetanWylieEnglish implements TranscriptView
}
else
{
wherestart = endPos.getOffset();
// wherestart = endPos.getOffset();
doc.insertString(endPos.getOffset(), wylie, mas);
}
@ -156,7 +156,7 @@ public class TibetanWylieEnglish implements TranscriptView
{
doc.insertString(endPos.getOffset(), "\n", null);
wylie = current.getAttributeValue("who");
wherestart = endPos.getOffset();
// wherestart = endPos.getOffset();
doc.insertString(endPos.getOffset(), wylie, mas);
if (iter.hasNext())
current = (org.jdom.Element)iter.next();

View file

@ -83,7 +83,7 @@ public class Wylie implements TranscriptView
MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setForeground(mas, Color.blue);
Position endPos = null;
AbstractDocument doc = new TibetanDocument(new StyleContext());
StyledDocument doc = new TibetanDocument(new StyleContext());
idBuffer = new StringBuffer();
startBuffer = new StringBuffer();

View file

@ -79,15 +79,14 @@ public class WylieEnglish implements TranscriptView
Iterator iter = elements.iterator();
Element current = null;
DuffData[] dd;
MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setForeground(mas, Color.blue);
MutableAttributeSet mas2 = new SimpleAttributeSet();
StyleConstants.setItalic(mas2, true);
Position endPos = null;
int wherestart;
// int wherestart;
String wylie;
AbstractDocument doc = new TibetanDocument(new StyleContext());
StyledDocument doc = new TibetanDocument(new StyleContext());
idBuffer = new StringBuffer();
startBuffer = new StringBuffer();
@ -112,7 +111,7 @@ public class WylieEnglish implements TranscriptView
}
else
{
wherestart = endPos.getOffset();
// wherestart = endPos.getOffset();
doc.insertString(endPos.getOffset(), wylie, mas);
}
@ -153,7 +152,7 @@ public class WylieEnglish implements TranscriptView
{
doc.insertString(endPos.getOffset(), "\n", null);
wylie = current.getAttributeValue("who");
wherestart = endPos.getOffset();
// wherestart = endPos.getOffset();
doc.insertString(endPos.getOffset(), wylie, mas);
if (iter.hasNext())
current = (org.jdom.Element)iter.next();

View file

@ -92,7 +92,6 @@ public class All implements TranscriptView
StyleConstants.setFontFamily(mas3, "Monospaced");
StyleConstants.setFontSize(mas3, 14);
Position endPos = null;
int wherestart;
String wylie;
idBuffer = new StringBuffer();
@ -118,7 +117,7 @@ public class All implements TranscriptView
}
else
{
wherestart = endPos.getOffset();
// wherestart = endPos.getOffset();
doc.insertString(endPos.getOffset(), wylie, mas);
}
@ -164,7 +163,7 @@ public class All implements TranscriptView
{
doc.insertString(endPos.getOffset(), "\n", null);
wylie = current.getAttributeValue("who");
wherestart = endPos.getOffset();
// wherestart = endPos.getOffset();
doc.insertString(endPos.getOffset(), wylie, mas);
if (iter.hasNext())
current = (org.jdom.Element)iter.next();

View file

@ -91,7 +91,6 @@ public class QuechuaEnglish implements TranscriptView
StyleConstants.setFontSize(mas2, 16);
StyleConstants.setFontSize(mas2, 16);
Position endPos = null;
int wherestart;
String wylie;
idBuffer = new StringBuffer();
@ -117,7 +116,7 @@ public class QuechuaEnglish implements TranscriptView
}
else
{
wherestart = endPos.getOffset();
// wherestart = endPos.getOffset();
doc.insertString(endPos.getOffset(), wylie, mas);
}
@ -160,7 +159,7 @@ public class QuechuaEnglish implements TranscriptView
{
doc.insertString(endPos.getOffset(), "\n", mas0);
wylie = current.getAttributeValue("who");
wherestart = endPos.getOffset();
// wherestart = endPos.getOffset();
doc.insertString(endPos.getOffset(), wylie, mas);
if (iter.hasNext())
current = (org.jdom.Element)iter.next();

View file

@ -1605,7 +1605,6 @@ public void paste(int offset) {
StringBuffer sb;
AttributeSet attr;
String fontName;
int fontNum;
Position endPos;
int i;
@ -1618,7 +1617,7 @@ public void paste(int offset) {
attr = doc.getCharacterElement(i).getAttributes();
fontName = StyleConstants.getFontFamily(attr);
if ((0 != (fontNum = TibetanMachineWeb.getTMWFontNumber(fontName))) || i==endPos.getOffset()) {
if ((0 != TibetanMachineWeb.getTMWFontNumber(fontName)) || i==endPos.getOffset()) {
if (i != start) {
try {
DuffData[] duffdata = TibTextUtils.getTibetanMachineWeb(sb.toString());
@ -1653,7 +1652,7 @@ public void paste(int offset) {
* document consists of both Tibetan and non-Tibetan fonts, however,
* the conversion stops at the first non-Tibetan font.
* @return the string of Wylie corresponding to the associated document
* @see org.thdl.tib.text.TibetanDocument.getWylie() */
* @see org.thdl.tib.text.TibetanDocument#getWylie() */
public String getWylie() {
return doc.getWylie();
}

View file

@ -26,6 +26,7 @@ import netscape.javascript.JSObject;
import org.thdl.tib.text.*;
import org.thdl.util.ThdlActionListener;
import org.thdl.util.ThdlDebug;
/**
* A version of Jskad which can be embedded into

View file

@ -68,7 +68,6 @@ public class Jskad4JavaScript extends JApplet {
public void setWylie(String wylie) {
try {
DuffData[] dd = TibTextUtils.getTibetanMachineWeb(wylie);
TibetanDocument t_doc = (TibetanDocument)jskad.dp.getDocument();
if (t_doc.getLength() > 0)
jskad.dp.newDocument(); // DLC FIXME: is it intended that t_doc is the new document? Because it is the old document at present.

View file

@ -172,7 +172,6 @@ public class AppletScannerFilter extends JApplet implements ActionListener, Focu
Object clicked = e.getSource();
StringSelection ss;
String s = null;
int start, end;
if (clicked==aboutItem)
{

View file

@ -101,7 +101,9 @@ public class BinaryFileGenerator extends LinkedList
{
final short newDefiniendum=1, halfDefiniendum=2, definition=3;
short status=newDefiniendum;
int marker, len, marker2, n=0, total=0, currentPage=0, currentLine=1;
int marker, len, marker2;
// int n=0;
int currentPage=0, currentLine=1;
char ch;
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(archivo)));
String entrada="", s1="", s2="", currentLetter="", temp="", lastWeirdDefiniendum="";
@ -383,7 +385,7 @@ public class BinaryFileGenerator extends LinkedList
Link link, newLink;
BinaryFileGenerator ultimo;
String firstSillable;
int pos, marker = word.indexOf(" "), comp;
int marker = word.indexOf(" "), comp;
if (marker<0)
firstSillable = word;

View file

@ -102,7 +102,7 @@ class ConsoleScannerFilter
public void run() throws Exception
{
String inStr, outStr;
String inStr;
while ((inStr=in.readLine())!=null)
{

View file

@ -22,10 +22,12 @@ import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.border.*;
import javax.swing.text.*;
import java.io.Serializable;
import org.thdl.tib.input.DuffPane;
import org.thdl.util.ThdlDebug;
import org.thdl.tib.text.*;
import org.thdl.tib.text.DuffData;
import java.io.Serializable;
/** Used by DictionaryTable to display a Tibetan word or phrase
(in either Roman or Tibetan script) in a single cell.

View file

@ -201,7 +201,6 @@ class LocalTibetanScanner implements TibetanScanner
{
boolean isDeclined =false;
int len = sil.length();
String dev;
if (len<3) return null;
@ -223,7 +222,6 @@ class LocalTibetanScanner implements TibetanScanner
public void scanBody(String in)
{
Word word;
boolean hayMasLineas=true;
if (in.equals("")) finishUp();
@ -263,7 +261,7 @@ class LocalTibetanScanner implements TibetanScanner
public void scanLine(String linea)
{
int init = 0, fin, i;
int init = 0, fin;
char ch;
String sil;
boolean doNotFinishUp;
@ -349,7 +347,6 @@ outAHere:
{
int i=wordList.size();
Token token[] = new Token[i];
Object obj;
ListIterator li = wordList.listIterator();
while(li.hasNext())
token[--i] = (Token)li.next();
@ -366,7 +363,6 @@ outAHere:
{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(archivo + ".dic")));
LinkedList ll1 = new LinkedList(), ll2 = new LinkedList();
int i;
String s;
while ((s=br.readLine())!=null)
{

View file

@ -272,7 +272,7 @@ public class Manipulate
{
sil = sil.toLowerCase();
int i=0, len=sil.length();
int i=0;
char ch, ch2;
while (!isVowel(sil.charAt(i))) i++;

View file

@ -85,7 +85,7 @@ public class MemorySyllableListTree extends LinkedList implements SyllableListTr
{
MemorySyllableListTree prefijo;
String firstSillable;
int pos, marker = word.indexOf(" ");
int marker = word.indexOf(" ");
if (marker<0)
firstSillable = word;

View file

@ -46,7 +46,7 @@ public class OnLineScannerFilter extends HttpServlet {
String parrafo = request.getParameter("parrafo"), checkboxName, script;
DictionarySource ds=null;
boolean checkedDicts[], allUnchecked, wantsTibetan;
int percent=100;
// int percent=100;
out.println("<html>");
out.println("<head>");
@ -176,7 +176,7 @@ public class OnLineScannerFilter extends HttpServlet {
synchronized public void desglosar(String in, PrintWriter pw, boolean tibetan)
{
boolean hayMasLineas=true;
//boolean hayMasLineas=true;
//int init = 0, fin;
//String linea;
Object words[];

View file

@ -125,7 +125,6 @@ public class RemoteTibetanScanner implements TibetanScanner
BufferedReader br = new BufferedReader(new InputStreamReader(uC.getInputStream()));
LinkedList ll1 = new LinkedList(), ll2 = new LinkedList();
int i;
String s;
while ((s=br.readLine())!=null)
{

View file

@ -258,7 +258,6 @@ public class WindowScannerFilter implements WindowListener, FocusListener, Actio
Object clicked = e.getSource();
StringSelection ss;
String s = null;
int start, end;
if (clicked == mnuAbout)
{

View file

@ -99,7 +99,8 @@ public class Word extends Token
public String getLink(boolean tibetan)
{
String localWord, result;
String localWord;
// String result;
if (wordSinDec==null) localWord = super.token;
else localWord = wordSinDec;
if (tibetan) localWord = TibetanHTML.getHTML(localWord + " ");

View file

@ -929,9 +929,6 @@ public class TibTextUtils {
if (dcs.length == 0)
return null;
AttributeSet attr;
String fontName;
int fontNum;
char ch;
String wylie;
@ -944,7 +941,7 @@ public class TibTextUtils {
for (int i=start; i<dcs.length; i++) {
ch = dcs[i].character;
int k = dcs[i].charNum;
fontNum = dcs[i].fontNum;
// int fontNum = dcs[i].fontNum;
if (k < 32) {
if (wylieBuffer.length() > 0 || !glyphList.isEmpty()) {

View file

@ -295,7 +295,7 @@ public class TibetanMachineWeb {
System.out.println("reading "+fileName);
String line;
boolean hashOn = false;
boolean isSanskrit = false;
boolean isSanskrit = false; //FIXME: this is never read.
boolean ignore = false;
while ((line = in.readLine()) != null) {

View file

@ -67,7 +67,7 @@ public class ThdlLazyExceptionTest extends TestCase {
junit.textui.TestRunner.run(ThdlLazyExceptionTest.class);
}
/*
/**
* Test for void ThdlLazyException()
*/
public void testThdlLazyException() {
@ -81,7 +81,7 @@ public class ThdlLazyExceptionTest extends TestCase {
} /* don't catch anything else. */
}
/*
/**
* Test for void ThdlLazyException(String)
*/
public void testThdlLazyExceptionString() {
@ -91,7 +91,7 @@ public class ThdlLazyExceptionTest extends TestCase {
assertTrue(null == e.getRealException());
}
/*
/**
* Test for void ThdlLazyException(String, Throwable)
*/
public void testThdlLazyExceptionStringThrowable() {
@ -103,7 +103,7 @@ public class ThdlLazyExceptionTest extends TestCase {
assertTrue("bah".equals(e.getRealException().getMessage()));
}
/*
/**
* Test for void ThdlLazyException(Throwable)
*/
public void testThdlLazyExceptionThrowable() {

View file

@ -88,115 +88,161 @@ package org.thdl.util;
public class Trie
{
/** Size of the m_nextChar array. */
public static final int ALPHA_SIZE = 128;
/** Size of the m_nextChar array. */
public static final int ALPHA_SIZE = 128;
/** The root node of the tree. */
Node m_Root;
/**
* Constructs a trie.
*/
public Trie()
{
m_Root = new Node();
}
/**
* Puts an object into the trie for lookup.
*
* @param key must be a 7-bit ASCII string
* @param value any java object.
*
* @return The old object that matched key, or null.
*/
public Object put(String key, Object value)
{
final int len = key.length();
Node node = m_Root;
for (int i = 0; i < len; i++)
{
Node nextNode = node.m_nextChar[Character.toUpperCase(key.charAt(i))];
if (nextNode != null)
{
node = nextNode;
}
else
{
for (; i < len; i++)
{
Node newNode = new Node();
node.m_nextChar[Character.toUpperCase(key.charAt(i))] = newNode;
node = newNode;
}
break;
}
}
Object ret = node.m_Value;
node.m_Value = value;
return ret;
}
/**
* Gets an object that matches the key.
*
* @param key must be a 7-bit ASCII string
*
* @return The object that matches the key, or null.
*/
public Object get(String key)
{
final int len = key.length();
Node node = m_Root;
for (int i = 0; i < len; i++)
{
try
{
node = node.m_nextChar[Character.toUpperCase(key.charAt(i))];
}
catch (ArrayIndexOutOfBoundsException e)
{
// the key is not 7-bit ASCII so we won't find it here
node = null;
}
if (node == null)
return null;
}
return node.m_Value;
}
/**
* The node representation for the trie.
*/
class Node
{
/** The root node of the tree. */
Node m_Root;
/**
* Constructor, creates a Node[ALPHA_SIZE].
* Constructs a trie.
*/
Node()
public Trie()
{
m_nextChar = new Node[ALPHA_SIZE];
m_Value = null;
m_Root = new Node();
}
/** The next nodes. */
Node m_nextChar[];
/**
* Puts an object into the trie for lookup.
*
* @param key must be a 7-bit ASCII string
* @param value any java object, but not null.
*
* @return The old object that matched key, or null.
* @throws NullPointerException if value is null or if key is null
*/
public Object put(String key, Object value)
{
if (null == value) throw new NullPointerException();
/** The value. */
Object m_Value;
}
final int len = key.length();
Node node = m_Root;
for (int i = 0; i < len; i++)
{
Node nextNode = node.m_nextChar[Character.toUpperCase(key.charAt(i))];
if (nextNode != null)
{
node = nextNode;
}
else
{
for (; i < len; i++)
{
Node newNode = new Node();
node.m_nextChar[Character.toUpperCase(key.charAt(i))] = newNode;
node.m_isLeaf = false;
node = newNode;
}
break;
}
}
Object ret = node.m_Value;
node.m_Value = value;
return ret;
}
/**
* Gets an object that matches the key.
*
* @param key must be a 7-bit ASCII string
*
* @return The object that matches the key, or null.
*/
public Object get(String key)
{
final int len = key.length();
Node node = m_Root;
for (int i = 0; i < len; i++)
{
try
{
node = node.m_nextChar[Character.toUpperCase(key.charAt(i))];
}
catch (ArrayIndexOutOfBoundsException e)
{
// the key is not 7-bit ASCII so we won't find it here
node = null;
}
if (node == null)
return null;
}
return node.m_Value;
}
/**
* Returns true if and only if key is a prefix of another, distinct
* member of the trie. Note that key may be in the trie as well as
* a prefix of something in the trie, but not all keys in the trie
* are prefixes of other keys. (In the trie you might naturally
* construct for the extended Wylie keyboard, "t" is a prefix, as is
* "ts", but "tsh", though a member of the trie, is not a prefix
* because there is no string x such that x begins with "tsh" but is
* not equal to "tsh" that is in the trie.)
*
* @param key must be a 7-bit ASCII string
*
* @return The object that matches the key, or null. */
public boolean hasPrefix(String key)
{
final int len = key.length();
Node node = m_Root;
for (int i = 0; i < len; i++)
{
try
{
node = node.m_nextChar[Character.toUpperCase(key.charAt(i))];
}
catch (ArrayIndexOutOfBoundsException e)
{
// the key is not 7-bit ASCII so we won't find it here
node = null;
}
if (node == null)
return false;
}
return !node.m_isLeaf;
}
/**
* The node representation for the trie.
*/
class Node
{
/**
* Constructor, creates a Node[ALPHA_SIZE].
*/
Node()
{
m_nextChar = new Node[ALPHA_SIZE];
m_Value = null;
m_isLeaf = true;
}
/** The next nodes. */
Node m_nextChar[];
/** The value. */
Object m_Value;
/** True if and only if this node has no children. */
boolean m_isLeaf;
}
}

View file

@ -0,0 +1,81 @@
package org.thdl.util;
import junit.framework.TestCase;
/**
* @author David Chandler
*
* Tests {@link org.thdl.util.Trie} at the unit level.
*/
public class TrieTest extends TestCase {
/**
* Constructor for TrieTest.
* @param arg0
*/
public TrieTest(String arg0) {
super(arg0);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(TrieTest.class);
}
/**
* Tests that the trie distinguishes between uppercase and
* lowercase. */
public void testCaseSupport() {
Trie t = new Trie();
t.put("S", "S value");
assertTrue(t.get("S").equals("S value"));
assertTrue(null == t.get("s"));
t.put("Sh", "Sh value");
assertTrue(t.get("Sh").equals("Sh value"));
assertTrue(t.get("S").equals("S value"));
assertTrue(null == t.get("s"));
assertTrue(null == t.get("sh"));
assertTrue(null == t.get("SH"));
assertTrue(null == t.get("sH"));
}
/**
* Test for put(String, Object)
*/
public void testReplacement() {
Trie t = new Trie();
t.put("S", "S value 1");
assertTrue(t.get("S").equals("S value 1"));
t.put("S", "S value 2");
assertTrue(t.get("S").equals("S value 2"));
}
/**
* Tests that put(*, null) throws a NullPointerException.
*/
public void testPuttingNull() {
Trie t = new Trie();
boolean threw = false;
try {
t.put("heya", null);
} catch (NullPointerException e) {
threw = true;
}
assertTrue(threw);
}
/**
* Test for put(String, Object)
*/
public void testPrefix() {
Trie t = new Trie();
t.put("t", "t value");
t.put("ts", "ts value");
t.put("tsh", "tsh value");
assertTrue(t.get("t").equals("t value"));
assertTrue(t.get("ts").equals("ts value"));
assertTrue(t.get("tsh").equals("tsh value"));
assertTrue(t.hasPrefix("t"));
assertTrue(t.hasPrefix("ts"));
assertTrue(!t.hasPrefix("tsh"));
}
}