This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Help with program-Params in a method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help with program-Params in a method?" Watch "Help with program-Params in a method?" New topic
Author

Help with program-Params in a method?

tim mahoney
Ranch Hand

Joined: Apr 19, 2005
Posts: 33
Hey guys, I must not be seeing something in the following program. The program works correctly, but I am having problems understanding how the format method below is called. I have this line of code:
//
String lsFileContents = NmvtisFileFormat.getInstance().format(poNmvtisFile);
//
The val for poNmvtisFile is "C:\JAVA\VINADD_TEST20050525095837_0.txt"

Here is the code for the NmvtisFileFormat class. I don't understand how the format method can be called with just (1) parameter. The method is defined to take (3) params. Here is the code:

package com.ams.atpsii.elt.nmvtisformat;

import java.util.Iterator;
import java.util.ListIterator;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Map.Entry;
import com.ams.atpsii.elt.Trace;
import java.security.InvalidParameterException;

import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.ParseException;
import java.text.Format;

import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class NmvtisFileFormat extends NmvtisAbstractFormat
{

private static String DELIMITER = "\n";

private static NmvtisFileFormat moNmvtisFileFormat;

private static final HashMap moClassToFormatMap;
private static final HashMap moRecordLengthToFormatMap;

private static final int RECORD_TYPE_OFFSET = 0;

private static final int VIN_ADD_RECORD_LENGTH = VinAddRecordFormat.RECORD_LENGTH;
private static final int VIN_MODIFY_RECORD_LENGTH = VinModifyRecordFormat.RECORD_LENGTH;
private static final int VIN_UNDO_RECORD_LENGTH = VinUndoRecordFormat.RECORD_LENGTH;
private static final int BRAND_ADD_RECORD_LENGTH = BrandAddRecordFormat.RECORD_LENGTH;
private static final int BRAND_UNDO_RECORD_LENGTH = BrandUndoRecordFormat.RECORD_LENGTH;


static
{
moNmvtisFileFormat = new NmvtisFileFormat();

moClassToFormatMap = new HashMap();
moRecordLengthToFormatMap = new HashMap();
moClassToFormatMap.put(BrandAddFile.class, new Format[] { BrandAddControlRecordFormat.getInstance(),
BrandAddRecordFormat.getInstance() } );

moClassToFormatMap.put(BrandUndoFile.class, new Format[] { BrandUndoControlRecordFormat.getInstance(),
BrandUndoRecordFormat.getInstance() } );
Trace.info("IN NM FILE FORMAT");
moClassToFormatMap.put(VinAddFile.class, new Format[] { VinAddControlRecordFormat.getInstance(),
VinAddRecordFormat.getInstance() } );

moClassToFormatMap.put(VinModifyFile.class, new Format[] { VinModifyControlRecordFormat.getInstance(),
(VinModifyRecordFormat) VinModifyRecordFormat.getInstance() } );

moClassToFormatMap.put(VinUndoFile.class, new Format[] {VinUndoControlRecordFormat.getInstance(),
VinUndoRecordFormat.getInstance() } );


moRecordLengthToFormatMap.put(new Integer(BRAND_ADD_RECORD_LENGTH),
moClassToFormatMap.get(BrandAddFile.class));

moRecordLengthToFormatMap.put(new Integer(BRAND_UNDO_RECORD_LENGTH),
moClassToFormatMap.get(BrandUndoFile.class));

moRecordLengthToFormatMap.put(new Integer(VIN_ADD_RECORD_LENGTH),
moClassToFormatMap.get(VinAddFile.class));

moRecordLengthToFormatMap.put(new Integer(VIN_MODIFY_RECORD_LENGTH),
moClassToFormatMap.get(VinModifyFile.class));

moRecordLengthToFormatMap.put(new Integer(VIN_UNDO_RECORD_LENGTH),
moClassToFormatMap.get(VinUndoFile.class));
}

NmvtisFileFormat()
{
}



public static NmvtisFileFormat getInstance()
{
return moNmvtisFileFormat;
}



public StringBuffer format(Object poObject, StringBuffer psbAppendTo, FieldPosition pos)
{
Trace.write("BS");

NmvtisFile loNmvtisFile = (NmvtisFile) poObject;

// Get the control record and data record formatters from the map,
// based on the class of poObject
Class loClass = poObject.getClass();
Format[] laFormatArray = (Format[]) moClassToFormatMap.get(loClass);

// Format the control record and add to the buffer.
laFormatArray[0].format(loNmvtisFile, psbAppendTo, pos);

// Add a delimiter to the control record
psbAppendTo.append(DELIMITER);

// Format all the data records
Iterator loRecordIterator = loNmvtisFile.getDataRecords().listIterator();



while(loRecordIterator.hasNext())
{

NmvtisDataRecord loRecord = (NmvtisDataRecord) loRecordIterator.next();

laFormatArray[1].format(loRecord, psbAppendTo, pos);

if(loRecordIterator.hasNext())
{
psbAppendTo.append(DELIMITER);
}
}

return psbAppendTo;
}

/*
* Expected input is a Nmvtis-format file. Output is NmvtisFile with NmvtisDataRecords
*
* @param psSource - String representing input file in one of the NMVTIS batch formats.
* @param pos - ParsePosition used to keep track of progress as we consume psSource.
* @return NmvtisFile with NmvtisDataRecords.
*/
public Object parseObject(String psSource, ParsePosition pos) throws InvalidParameterException
{
//First we need to find the length of records in the file. All records
//are the same length, according to the spec, or there is an error.
int liRecordLength = psSource.indexOf(DELIMITER, pos.getIndex() ) - pos.getIndex();

//Record length is really the only indicator of file type. If this were
//ever to be not true (i.e., a new NMVTIS file type of a length already
//in use) were to be created, this section will have to be reworked.

//The Array contains one control format and one data format.
Format[] laFormats = (Format[]) moRecordLengthToFormatMap.get(new Integer(liRecordLength));
if (laFormats == null)
{
throw new InvalidParameterException(this + ": arseObject: file has invalid record length: " +
liRecordLength );
}

NmvtisFile loNmvtisFile = null;

ArrayList llList = new ArrayList(psSource.length()/liRecordLength);

while((pos.getIndex() < psSource.length()) & (pos.getIndex() >= 0) )
{
char lcRecordType = psSource.charAt(pos.getIndex() + RECORD_TYPE_OFFSET);

switch(lcRecordType)
{
case RECORD_TYPE_CONTROL:
{
if (loNmvtisFile != null)
{
throw new InvalidParameterException(this +
": arseObject: second control record found in file before " +
"data record " + (llList.size() + 1) );
}

//Control records contain mostly file-related information. Parsing a
//NMVTIS Control record results in a NMVTIS file that contains no records.
loNmvtisFile = (NmvtisFile) laFormats[0].parseObject(psSource, pos);
break;
}

case RECORD_TYPE_DATA:
{
llList.add((NmvtisDataRecord) laFormats[1].parseObject(psSource, pos));
break;
}

default:
{
throw new InvalidParameterException(this +
": arseObject: invalid record type found in file before " +
"data record " + (llList.size() + 1) );
}

}

//If there are no more delimiters, then we have reached the end of the string.
//IndexOf() returns -1 if delimiter not found.
int liNewIndex = psSource.indexOf(DELIMITER, pos.getIndex());

if (liNewIndex >= 0) //leave pos alone if delimiter not found.
{
pos.setIndex(liNewIndex + 1);
}
}

return validate(loNmvtisFile, llList);
}


/*
* Perform what checks we can on the file.
*
* @param poNmvtisFile - Nmvtis file to be formatted.
* @param plList - List of data records.
*/
public NmvtisFile validate(NmvtisFile poNmvtisFile, ArrayList plList) throws InvalidParameterException
{
if (poNmvtisFile == null)
{
throw new InvalidParameterException(this +
"::validate: no control record found in file");
}

if (plList.size() == 0)
{
throw new InvalidParameterException(this +
"::validate: no data records found in file");
}

if ( plList.size() != poNmvtisFile.getStatedNumberOfDataRecords( ).intValue() )
{
throw new InvalidParameterException(this +
"::validate: stated number of data records does not match actual. " +
"Stated=" + poNmvtisFile.getStatedNumberOfDataRecords( ) +
", Actual=" + plList.size() );
}

poNmvtisFile.setRecordList(plList);

return poNmvtisFile;
}


/*
* Format the file and write it to the OS.
* Because of the file level control record of a NmvtisFile, NmvtisFiles of different types
* (and therefore different lengths) cannot be concatenated without doing several
* validations. Since this should not be needed, there will be no method to append
* Nmvtis files, and writeToFile will replace the existing file, not append to it.
*
* @param poFile - File to which data will be written.
* @param poNmvtisFile - Nmvtis file to be formatted.
*/
public static void writeToFile(File poFile, NmvtisFile poNmvtisFile) throws IOException, ParseException
{
if(poFile == null)
{
throw new IOException();
}

if(poFile.isDirectory())
{
throw new IOException();
}

if(poFile.exists())
{
// poNmvtisFile.append(readFromFile(poFile));

poFile.delete();
}

poFile.createNewFile();

OutputStreamWriter loWriter = new OutputStreamWriter(new FileOutputStream(poFile));

loWriter.write(getInstance().format(poNmvtisFile));

loWriter.close();
}



public static NmvtisFile readFromFile(File poFile) throws IOException, ParseException
{
if(poFile == null)
{
throw new IOException();
}

if(poFile.isDirectory())
{
throw new IOException();
}

if(!poFile.exists())
{
throw new IOException();
}



BufferedReader loBufferedReader =
new BufferedReader(
new InputStreamReader(
new FileInputStream(poFile)));


StringBuffer lsbContents = new StringBuffer();

String lsReadLine = loBufferedReader.readLine();

while(lsReadLine != null)
{
// The buffered reader tokenizes on carriage return.
// Therefore, we must reappend carriage return.

lsbContents.append(lsReadLine).append(DELIMITER);

lsReadLine = loBufferedReader.readLine();
}

loBufferedReader.close();

return (NmvtisFile) getInstance().parseObject(lsbContents.toString());
}

}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

My guess is that NmvtisAbstractFormat has a one-argument format() method which supplies defaults for the second two arguments and invokes the three-argument version. NmvtisFileFormat inherits this method.


[Jess in Action][AskingGoodQuestions]
tim mahoney
Ranch Hand

Joined: Apr 19, 2005
Posts: 33
No, it doesnt. I looked down the classes and didnt see it. Here are the classes:
package com.ams.atpsii.elt.nmvtisformat;

import com.ams.atpsii.elt.formatting.AbstractFormat;

import java.text.Format;
import java.text.FieldPosition;
import java.text.ParsePosition;

public abstract class NmvtisAbstractFormat extends AbstractFormat
{

public static final char RECORD_TYPE_CONTROL = 'C';
public static final char RECORD_TYPE_DATA = 'D';

}
**********************************************************************
package com.ams.atpsii.elt.formatting;

import java.util.Iterator;

import java.text.Format;
import java.text.FieldPosition;
import java.text.ParsePosition;

public abstract class AbstractFormat extends Format
{
public static final int LEFT = 0;

public static final int RIGHT = 1;

/*
* Take a string and a target length; pad the string to the right or left
* as indicated with the provided character, until the String is the
* length provided; append the String to the StringBuffer provided.
*
* @param psPadMe - the string to be extended.
* @param psbAppendTo - the string buffer to which the extended string will be appended.
* @param piDirection - int indicating which way the String is to be extended" (left/right).
* @param piLength - int specifying the desired final length of the String, before appending.
* @param pcPadChar - the character used to extend the String.
*
* @return StringBuffer provided, with padded string appended.
*/
public static StringBuffer pad(String psPadMe,
StringBuffer psbAppendTo,
int piDirection,
int piLength,
char pcPadChar)
{
int liDifference = piLength - psPadMe.length();

return padBy(psPadMe, psbAppendTo, piDirection, liDifference, pcPadChar);
}


/*
* Take a string and the number of bytes it needs to be extended by;
* pad the string to the right or left as indicated,
* with the provided character, until the number of desired characters is added;
* append the String to the StringBuffer provided.
*
* @param psPadMe - the string to be extended.
* @param psbAppendTo - the string buffer to which the extended string will be appended.
* @param piDirection - int indicating which way the String is to be extended" (left/right).
* @param piAddLength - int specifying the number or characters to add.
* @param pcPadChar - the character used to extend the String.
*
* @return StringBuffer provided, with padded string appended.
*/
public static StringBuffer padBy(String psPadMe,
StringBuffer psbAppendTo,
int piDirection,
int piAddLength,
char pcPadChar)

{
if((psPadMe == null) ||
(psbAppendTo == null))
{
throw new NullPointerException();
}

if(piAddLength < 0)
{
throw new IllegalArgumentException();
}

if( !((piDirection == RIGHT) || (piDirection == LEFT)) )
{
throw new IllegalArgumentException();
}

if(piDirection == RIGHT)
{
psbAppendTo.append(psPadMe);
}

for(int i = 0; i < piAddLength; i++)
{
psbAppendTo.append(pcPadChar);
}

if(piDirection == LEFT)
{
psbAppendTo.append(psPadMe);
}

return psbAppendTo;

}

}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Your class ultimately extends java.text.Format, which does indeed have a single-argument format(Object) method. That's the method you're calling.
tim mahoney
Ranch Hand

Joined: Apr 19, 2005
Posts: 33
Sounds good. Thanks for posting.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with program-Params in a method?
 
Similar Threads
(B&S)My Data.java and read() !
Need to speed this algorithm up
database lock
suncertify.db.Data