📄 gazetteer.java
字号:
package jwo.jpss.spatial; // Part of the spatial modelling package.
import java.util.*; // For the Java collections framework.
// **************************************************************
/** Stores a Gazetteer table and allows simple searching.
* @author Jo Wood.
* @version 1.3, 7th October, 2001.
*/
// **************************************************************
public class Gazetteer
{
// --------------------- Object variables ----------------------
private TreeMap entries; // Gazetteer entries.
// ----------------------- Constructors ------------------------
/** Initialises the gazetteer.
*/
public Gazetteer()
{
entries = new TreeMap();
}
// ----------------------- Methods --------------------------
/** Adds an entry to the gazetteer. Each entry should consist
* of a location, a name and a feature type.
* @param fp Location/extent of the feature.
* @param name Name of the feature.
* @param type Feature classification.
*/
public void addEntry(Footprint fp, String name, String type)
{
TextEntry key = new TextEntry(name,type);
entries.put(key,fp);
}
/** Reports all current gazetteer entries.
* @return All gazetteer entries in a form suitable for display.
*/
public String toString()
{
return entries.toString();
}
/** Reports the location corresponding to the given feature name
* @param Name of feature to search for.
* @return Location of given feature or null if none found.
*/
public Footprint getLocation(String featureName)
{
Iterator keys = entries.keySet().iterator();
while (keys.hasNext())
{
TextEntry place = (TextEntry)keys.next();
if (place.getName().equals(featureName))
return (Footprint)entries.get(place);
}
// No match found if we get this far.
return null;
}
/** Reports all current gazetteer entries as a 2D array. Useful for
* importing into a JTable.
* @return All gazetteer entries as a 2D array of n Rows by 2 columns.
*/
public String[][] getEntryArray()
{
String [][] gazArray = new String[entries.size()][2];
Iterator keys = entries.keySet().iterator();
int row=0;
while (keys.hasNext())
{
TextEntry place = (TextEntry)keys.next();
gazArray[row][0] = place.getName();
gazArray[row][1] = place.getType();
row++;
}
return gazArray;
}
/** Reports the gazetteer entries that contain the given string as a
* 2D array. Useful for importing into a JTable.
* @param text Text to search for.
* @return All gazetteer entries that match the given string.
*/
public String[][] getEntryArray(String text)
{
Vector matchedEntries = new Vector();
String [][] gazArray;
String searchString = text.toLowerCase();
Iterator keys = entries.keySet().iterator();
// Search for string and store matched entries.
while (keys.hasNext())
{
TextEntry place = (TextEntry)keys.next();
if (place.getName().toLowerCase().indexOf(searchString) >=0)
matchedEntries.add(place);
}
// Convert matched entries into an array.
int numMatches = matchedEntries.size();
gazArray = new String[numMatches][2];
int row = 0;
Iterator matchedKeys = matchedEntries.iterator();
while (matchedKeys.hasNext())
{
TextEntry place = (TextEntry)matchedKeys.next();
gazArray[row][0] = place.getName();
gazArray[row][1] = place.getType();
row++;
}
return gazArray;
}
/** Reports the number of entries in the gazetteer.
* @return Number of entries in the gazetteer.
*/
public int getNumEntries()
{
return entries.size();
}
// -------------------- Nested Classes ----------------------
/** Stores a gazetteer text entry. Consists of two text fields
* describing the spatial object and its type.
*/
private class TextEntry implements Comparable
{
// -------------------- Object variables --------------------
private String name,type; // Name of entry and its type.
// ---------------------- Constructor -----------------------
/** Creates a text entry with a given name and type.
* @param name Name of object in gazetteer.
* @param type Type of object in gazetteer.
*/
public TextEntry(String name, String type)
{
this.name = name;
this.type = type;
}
// -------------------- Accessor Methods --------------------
/** Reports the name of the gazetteer object.
* @return Name of gazetteer object.
*/
public String getName()
{
return name;
}
/** Reports the type of the gazetteer object.
* @return type of gazetteer object.
*/
public String getType()
{
return type;
}
/** Reports the contents of the text entry in a form suitable
* for display.
* @return Contents of the text entry object.
*/
public String toString()
{
return getName()+","+getType();
}
// ----------------- Implemented Methods --------------------
/** Compares this text entry object with another. Used for placing
* text entries in alphabetic order.
* @param o Object with which to compare this one.
* @return Comparative alphabetic order of this object and the
* given one.
*/
public int compareTo(Object o)
{
TextEntry otherObj = (TextEntry)o;
int placeCmp = name.compareTo(otherObj.getName());
if (placeCmp == 0)
return type.compareTo(otherObj.getType());
else
return placeCmp;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -