📄 channellist.java
字号:
package org.trinet.jasi;
import java.sql.Date;
import java.sql.Connection;
import java.util.*;
import java.io.*;
import org.trinet.util.*;
import org.trinet.util.gazetteer.LatLonZ;
import org.trinet.util.gazetteer.GeoidalUnits;
import org.trinet.util.DateTime;
//public class ChannelList extends ArrayList {
public class ChannelList extends ChannelableList {
static boolean debug = false;
static String cacheFilename = null;
static String asciiFilename = null;
static String binaryFilename = null;
static final String FileSep =
System.getProperty("file.separator"); // "/" on UNIX, "\" on PC's
/**
A list of channel objects that can be sorted, etc.<p>
This class also supports local caching of a channel list which is usually
faster then reading from a database, especially over slow links. The cache
contains serialized Channel objects. The smartLoad() method
attempts to read from a local cache. If it fails it reads from the DataSource
then writes a cache file for later use.
*/
public ChannelList() {
this(getCacheFilename());
}
public ChannelList(int initCapacity) {
super(initCapacity);
}
public ChannelList(String cacheFilename) {
this.
setCacheFilename(cacheFilename);
}
/**
* Add an object. Will not add object if its already in the list.
* Returns 'true' if object was added.
*/
public boolean add(Object obj) {
if ( contains(obj) ) return false;
return super.add(obj) ;
}
public boolean addWithoutCheck(Object obj) {
return super.add(obj) ;
}
/** Create a HashMap of the channel list to speed lookups. */
protected static final int DEFAULT_CAPACITY = 5759;
protected static final float DEFAULT_LOAD_FACTOR = 0.9f;
protected HashMap lookupMap;
public void createLookupMap() {
lookupMap = new HashMap(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
int size = this.size();
for (int idx = 0; idx < size; idx++) {
Channel channel = (Channel) get(idx);
lookupMap.put(channel.getChannelName(), channel);
}
}
/** Find a "similar" channel using a HashMap of the channel list to speed lookups.
* "Similar" means match the net, sta, and seedchan strings. No other fields are compared.
* @see: Channel.equals() */
public Channel findSimilarInMap(Channel chan) {
if (chan == null) return null;
Channel channel = null;
if (lookupMap == null) createLookupMap();
if (! lookupMap.isEmpty()) {
channel = (Channel) lookupMap.get(chan.getChannelName());
}
if (debug) System.out.println(" findSimilar listChan: " +channel+ " inputchan: " + chan + " hash: " + chan.hashCode());
return channel;
}
/**
* Calculate the distance from location */
public void calcDistancesFrom (LatLonZ loc)
{
Channel chan [] = getArray();
for ( int i=0; i < chan.length; i++) {
chan[i].calcDistance(loc);
}
}
/** Note: can't override to change returned type */
public Channel[] getArray() {
return (Channel[]) toArray(new Channel[size()]);
}
/** Sort/resort the Channel list by distance from the given LatLonZ. <p>
1) An in-place sort<br>
2) Order of components for a given station is not explicitely handled.<br>
*/
public void distanceSort(Solution sol)
{
distanceSort(sol.getLatLonZ());
}
/**
* Sort/resort the Channel list by distance from the given Solution. <p>
1) An in-place sort<br>
2) Order of components for a given station is not explicitely handled.<br>
*/
public void distanceSort(LatLonZ latlonz)
{
if (debug) System.out.println ("Distance sorting channel list....");
// calc. distances from solution to stations
calcDistancesFrom(latlonz);
// very weird syntax! to avoid compile error
Collections.sort(this, /*new ChannelList().*/ new DistanceSorter());
}
/* FILE READ/WRITE NOTES:
There are three types of files that you can read/write channel list to/from.
1) ASCII
2) Binary
3) Objects
These were done to compare speeds and space used.
Results: there is no dramatic speed difference but objects are a bit faster
to read. There was a scatter of up to 20% in different runs.
Test read/write of 2876 channels:
Source Read Write File
(sec) (sec) Size
dbase 34.0 --- --- (over 10Mbit link)
ASCII 20.5 4.5 224k
Binary 20.7 4.7 228k
Objects 14.8 5.9 650k
*/
/**
* Write serialized version of the channel list to a file.
* The list will be written to a file called "channelList.cache" in the user's
* home directory.
*/
public boolean writeToCache() {
return writeToCache(getCacheFilename());
}
/**
* Write serialized version of the channel list to a file.
* The filename should include the path.
*/
public boolean writeToCache (String filename) {
boolean status = true;
try {
FileOutputStream ostream = new FileOutputStream(filename);
ObjectOutputStream stream = new ObjectOutputStream(ostream);
stream.writeObject( (Object) this );
stream.flush();
stream.close();
} catch (IOException ex) {
ex.printStackTrace();
status = false ;
} finally {
return status;
}
}
/**
* Write serialized version of the channel list to a file in background thread.
* The list will be written to a file called "channelList.cache" in the user's
* home directory.
*/
public boolean writeToCacheInBackground() {
new CacheWriterThread(this);
return true;
}
/** Reads channel list from the data source and writes it to the cache file
* from a background thread. This keeps the cache current. */
public void refreshCache () {
new CacheRefreshThread();
}
//////// INNER CLASS
/**
* This is a one-shot thread that writes cache file in background.
*/
class CacheRefreshThread implements Runnable {
public CacheRefreshThread () { // constructor - also starts thread
Thread thread = new Thread(this);
thread.start();
}
public void run () {
BenchMark bm = new BenchMark();
ChannelList chanList = ChannelList.readCurrentList();
chanList.writeToCache();
bm.print("CacheRefreshThread finished");
}
} // end of CacheRefreshThread class
/**
* This is a one-shot thread that writes cache file in background.
*/
class CacheWriterThread implements Runnable {
ChannelList list;
// public Thread thread;
// constructor - also starts thread
public CacheWriterThread (ChannelList list ) {
this.list = list;
Thread thread = new Thread(this);
thread.start();
}
public void run () {
list.writeToCache();
}
} // end of CacheWriterThread class
/**
* Write ASCII version of the channel list to default file.
*/
public boolean writeAsciiFile() {
return writeAsciiFile(getAsciiFilename());
}
/**
* Write ASCII version of the channel list to a file.
* The filename should include the path.
*/
public boolean writeAsciiFile (String filename) {
boolean status = true;
try {
PrintWriter writer = new PrintWriter(
new FileOutputStream(filename)
);
Channel chan[] = getArray();
for (int i = 0; i<chan.length; i++) {
writer.println(chan[i].toCompleteString(" "));
}
writer.flush();
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
status = false ;
} finally {
return status;
}
}
public boolean writeBinaryFile () {
return writeBinaryFile(getFullDefaultBinaryFilename());
}
public boolean writeBinaryFile (String filename) {
boolean status = true;
try {
DataOutputStream out = new DataOutputStream(
new FileOutputStream(filename)
);
Channel chan[] = getArray();
for (int i = 0; i<chan.length; i++) {
chan[i].binaryWrite(out);
}
out.flush();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
status = false ;
} finally {
return status;
}
}
public boolean readBinaryFile () {
return readBinaryFile(getFullDefaultBinaryFilename());
}
/**
* Read Binary version of the channel list from a file.
* The filename should include the path.
*/
public boolean readBinaryFile (String filename) {
boolean status = true;
try {
DataInputStream in = new DataInputStream(
new FileInputStream(filename)
);
while ( in.available() > 0) {
// add new Channel objects to list
add(Channel.binaryRead(in));
}
in.close();
} catch (IOException ex) {
ex.printStackTrace();
status = false ;
} finally {
return status;
}
}
/**
* Read ASCII version of the channel list from a file.
* The filename should include the path.
*/
public boolean readAsciiFile () {
return readAsciiFile(getAsciiFilename());
}
/**
* Read ASCII version of the channel list from a file.
* The filename should include the path.
*/
public boolean readAsciiFile (String filename) {
boolean status = true;
try {
BufferedReader reader = new BufferedReader(
new FileReader(filename)
);
String line;
while ( (line = reader.readLine()) != null) {
// add new Channel objects to list
add(Channel.parseCompleteString(line, " "));
}
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
status = false ;
} finally {
return status;
}
}
/**
* Read serialized version of the channel list from a file.
* The list will be written to a file called "channelList.cache" in the user's
* home directory. Returns empty ChannelList if it fails.<p>
* The Java loader checks "version" number of the class so if the ChannelList class
* has been recompiled since the cache was written it will throw an exception and
* not load it.
*/
public static ChannelList readFromCache () {
return readFromCache(getCacheFilename());
}
/**
* Read serialized version of the channel list from a file.
* The filename should include the path.
* Returns empty ChannelList if it fails. <p>
*
* The Java loader checks "version" number of the class so if the ChannelList class
* has been recompiled since the cache was written it will throw an exception and
* not load it.
*/
public static ChannelList readFromCache (String filename) {
Object obj;
setCacheFilename(filename);
try {
FileInputStream instream = new FileInputStream(filename);
ObjectInputStream stream = new ObjectInputStream(instream);
obj = stream.readObject() ;
stream.close();
} catch (FileNotFoundException ex) {
return new ChannelList();
// This happens if ChannelList has been recompiled since last write
} catch (InvalidClassException ex) {
System.err.println ("Version of cached ChannelList class does not match code.");
return new ChannelList();
} catch (Exception ex) {
ex.printStackTrace();
return new ChannelList();
}
return (ChannelList) obj;
}
/** Returns the default ChannelList file name. */
public static String getCacheFilename() {
if (cacheFilename == null) setCacheFilename(getFullDefaultCacheFilename());
return cacheFilename;
}
/** Set filename of cache file. The path remains the default. */
public static void setCacheFilename(String filename) {
cacheFilename = filename;
}
/** Set the path and filename of the cache file. */
public static void setCacheFilename(String path, String filename) {
setCacheFilename(path + FileSep + filename);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -