Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 1996-2001
   * Logica Mobile Networks Limited
   * All rights reserved.
   *
   * This software is distributed under Logica Open Source License Version 1.0
   * ("Licence Agreement"). You shall use it and distribute only in accordance
   * with the terms of the License Agreement.
   *
  */
 package org.smpp.smscsim.util;
 
 import java.util.List;
 
Represents table of Records. Users can add, search, replace and remove records as well as read table from file and write it to a file. Different records in the table can have different attributes, however if the search for record with certain value of given attribute is required, then the attribute must be present in all the records. Single attribute search is supported, i.e. if the key is naturally represented by more than one attribute, there must be an attribute which contains bothe the attributes in some way.

The table can be read and written from and to input and output stream using an implementation of TableParser class.

Author(s):
Logica Mobile Networks SMPP Open Source Team
Version:
$Revision: 1.2 $
See also:
Record
Attribute
TableParser
BasicTableParser
 
 public class Table {
Holds all records currently present in the table.
 
 	private List<Recordrecords;

The name (path) of the file to load the records from. Re-user in reload function.

 
 	private String fileName;

Constructs an empty table.
 
 	public Table() {
 		 = null;
 		 = new LinkedList<Record>();
 	}

Constructs a table by reading it from the disk file. For parsin of the table data read from the file uses parser returned by method getParser.

 
 	public Table(String fileNamethrows FileNotFoundExceptionIOException {
 		this. = fileName;
 		read(fileName);
 	}

Adds one record to the table. No checking on duplicates is performed as the name of the key attribute is not provided.

Parameters:
record the record to add
 
 	public synchronized void add(Record record) {
 		.add(record);
 	}

Adds one record to the table. The checking on duplicates is performed; if a record with the same key is already present in the table, it's replaced with this record. Order of the records isn't ensured.

Parameters:
record the record to add
key the key attribute for checking the uniquenes
See also:
replace(org.smpp.smscsim.util.Record,org.smpp.smscsim.util.Attribute)
 
 	public synchronized void add(Record recordAttribute key) {
 		replace(recordkey);
 	}

Replaces a record with the given attribute with the provided record. If no record with the same attribute is present in the table, the provided record is added to the table.

Parameters:
record the record which replaces the existing record
oldKey the key attribute for finding the record in the table
	public synchronized void replace(Record recordAttribute oldKey) {
		Record old = find(oldKey);
		if (old != null) {
			remove(oldKey);
		}
		add(record);
	}

Returns a record whose one of the attributes matches to the provided attribute. If none found, returns null.

Parameters:
key the attribute used for matching
Returns:
the found record
	public synchronized Record find(Attribute key) {
		if (key != null) {
			return find(key.getName(), key.getValue());
else {
			return null;
		}
	}

Returns record which contains an attribute with the same name as provided equal to the value as provided. If none found, returns null. The comparison of the value is case sensitive.

Parameters:
name the name of attribute to check
value the required value of the attribute
Returns:
the found record
	public synchronized Record find(String nameString value) {
		Record current;
		String currKeyValue;
		while (iter.hasNext()) {
			current = (Recorditer.next();
			currKeyValue = current.getValue(name);
			if ((currKeyValue != null) && (currKeyValue.equals(value))) {
				return current;
			}
		}
		return null;
	}

Removes a record whose one attribute matches to the given attribute. Nothing will happen if none is found.

Parameters:
key the attribute used for matching
	public synchronized void remove(Attribute key) {
		remove(key.getName(), key.getValue());
	}

Removes a record whose attribute with the same key as provided is equal to the provided value. Nothing will happen if none is found.

Parameters:
key the name of the key attribute
value the required value of the key attribute
	public synchronized void remove(String keyString value) {
		Record toRemove = find(keyvalue);
		if (toRemove != null) {
			.remove(toRemove);
		}
	}

Returns count of records currently present in the table.

Returns:
the count of the records
See also:
get(int)
	public int count() {
		return .size();
	}

Returns a record on the given position. Useful for listing of all records from the table. Records are numbered from 0 to count()-1.

Parameters:
i the index of the record to return
Returns:
the record on the given position
See also:
count()
	public Record get(int i) {
		return (Record.get(i);
	}

Loads the table from the disk file. For parsing the file uses TableParser implementation returned by getParser.

Parameters:
fileName the name of the file with the table data
See also:
read(java.io.InputStream)
write(java.lang.String)
getParser()
BasicTableParser
TableParser
	public synchronized void read(String fileNamethrows FileNotFoundExceptionIOException {
		FileInputStream is = new FileInputStream(fileName);
		 = new LinkedList<Record>(); // clear current list of records
		read(is);
		is.close();
	}

Loads the table from the input stream. For parsing the file uses TableParser implementation returned by getParser.

Parameters:
is the input stream with the data
See also:
write(java.io.OutputStream)
getParser()
BasicTableParser
TableParser
	public synchronized void read(InputStream isthrows IOException {
		TableParser parser = getParser();
		parser.parse(is);
	}

Re-reads the table from disk file as set up by constructor.

	public synchronized void reload() throws IOException {
	}
Writes table data to the disk file. For composing the data uses TableParser implementation returned by getParser. The contents of the file is deleted before writing.

Parameters:
fileName the name of file to write the data to
See also:
write(java.io.OutputStream)
read(java.lang.String)
getParser()
BasicTableParser
TableParser
	public synchronized void write(String fileNamethrows FileNotFoundExceptionIOException {
		FileOutputStream os = new FileOutputStream(fileName);
		write(os);
		os.close();
	}

Writes table data to the oputput stream. For composing the data uses TableParser implementation returned by getParser.

Parameters:
os the output stream to write the data to
See also:
write(java.lang.String)
read(java.io.InputStream)
getParser()
BasicTableParser
TableParser
	public synchronized void write(OutputStream osthrows IOException {
		TableParser parser = getParser();
		parser.compose(os);
	}

Returns parser to use for parsing and composing table data form and to input and output stream. By default returns instance of BasicTableParser. If necessary, another implemantation of TableParser can be created and used.

Returns:
the parser to use for reading and writting of the table data
See also:
TableParser
BasicTableParser
	public TableParser getParser() {
		return new BasicTableParser(this);
	}
/*
 * $Log: not supported by cvs2svn $
 * Revision 1.1  2003/07/23 00:28:39  sverkera
 * Imported
 *
 */
New to GrepCode? Check out our FAQ X