FileDocCategorySizeDatePackage
Stock.javaAPI DocJ2ME MIDP 2.011955Thu Nov 07 12:02:18 GMT 2002example.stock

Stock

public final class Stock extends Object

This is a utility class that is used to parse data obtained from either the quote server or the database and break it into fields for easy use rather than spreading the parsing code around to a thousand different places through out the program.

Fields Summary
private static String
name
Name of the Stock
private static String
time
Time of the last trade
private static int
price
Price of the Stock
private static int
change
$ change
private static int
high
52-week high
private static int
low
52-week low
private static int
open
opening price on the day
private static int
prev
previous high
Constructors Summary
Methods Summary
public static java.lang.Stringconvert(int intNum)

Convert an int into a String with the decimal placed back in

return
The String value of the int
param
intNum the int value to convert to a String

        String s = String.valueOf(intNum);
        String pre = s.substring(0, ((s.length() < 4) ?
				     s.length() : s.length()-4));
        String suf = s.substring(((s.length() == pre.length()) ?
				  0 : s.length()-4), s.length());
        if (Integer.valueOf(suf).intValue() == 0) {
            return pre;
        }
        while (Integer.valueOf(suf.substring(suf.length()-1,
					     suf.length())).intValue() == 0) {
            suf = suf.substring(0, suf.length()-1);
        }
        return (pre + "." + suf);
    
public static intgetChange(java.lang.String quoteString)

Return the $ change in the stock

return
$ change in the stock today
param
quoteString String to parse for the field data

        parse(quoteString);
        return change;
    
public static intgetHigh(java.lang.String quoteString)

Return the 52-week high for the stock

return
52-week high of the stock
param
quoteString String to parse for the field data

        parse(quoteString);
        return high;
    
public static intgetLow(java.lang.String quoteString)

Return the 52-week low of the stock

return
52-week low of the stock
param
quoteString String to parse for the field data

        parse(quoteString);
        return low;
    
public static java.lang.StringgetName(java.lang.String quoteString)

Return the name of the stock

return
name (ticker symbol) of the stock
param
quoteString String to parse for the field data

        parse(quoteString);
        return name;
    
public static intgetOpen(java.lang.String quoteString)

Return the opening price of the stock

return
opening price of the stock today
param
quoteString String to parse for the field data

        parse(quoteString);
        return open;
    
public static intgetPrevious(java.lang.String quoteString)

Return the previous high for the stock

return
previous high for the stock
param
quoteString String to parse for the field data

        parse(quoteString);
        return prev;
    
public static intgetPrice(java.lang.String quoteString)

Return the price of the last trade of the stock

return
price of the last trade of the stock
param
quoteString String to parse for the field data

        parse(quoteString);
        return price;
    
public static java.lang.StringgetStringChange(java.lang.String quoteString)

String representation of change with decimal placed back in the correct spot

return
change in stock price today
param
quoteString String to parse for the field data

        parse(quoteString);
        return convert(change);
    
public static java.lang.StringgetStringHigh(java.lang.String quoteString)

String representation of the 52-week high with decimal placed back in the correct spot

return
52-week high
param
quoteString String to parse for the field data

        parse(quoteString);
        return convert(high);
    
public static java.lang.StringgetStringLow(java.lang.String quoteString)

String representation of the 52-week low with decimal placed back in the correct spot

return
52-week low
param
quoteString String to parse for the field data

        parse(quoteString);
        return convert(low);
    
public static java.lang.StringgetStringOpen(java.lang.String quoteString)

String representation of the opening price with decimal placed back in the correct spot

return
opening stock price
param
quoteString String to parse for the field data

        parse(quoteString);
        return convert(open);
    
public static java.lang.StringgetStringPrevious(java.lang.String quoteString)

String representation of previous with decimal placed back in the correct spot

return
previous high for the stock
param
quoteString String to parse for the field data

        parse(quoteString);
        return convert(prev);
    
public static java.lang.StringgetStringPrice(java.lang.String quoteString)

String representation of price with decimal placed back in the correct spot

return
current stock price
param
quoteString String to parse for the field data

        parse(quoteString);
        return convert(price);
    
public static java.lang.StringgetTime(java.lang.String quoteString)

Return the time of the last trade

return
time of the last trade of the stock
param
quoteString String to parse for the field data

        parse(quoteString);
        return time;
    
public static intmakeInt(java.lang.String source)

Take a String representation of an int and the number of decimal places that the String carries and make an int out of it

Since there is no floating point support in MIDP/CLDC, we have to convert the decimal numbers into Integers. We do this by:

  • Looking at only the first 7 significant characters which, because of the decimal, means the first 6 numbers from left to right.
  • Looking at a maximum of 4 decimal places
  • We remove the decimal character (if there is one) and concatenate the numbers before and after the decimal so that we can convert to an integer and manipulate the value as a number
  • After doing this for each number, we have int values but no notion of where the decimal place was. To alleviate this, we make sure that each number has EXACTLY 4 decimal place holders. Therefore, we can divide by 10000 to put the decimal place back in the same spot.
    Example: 100 -> 1000000 -> /10000 = 100
    Example: 345.67 -> 34567 -> 3456700 -> /10000 = 345.67
    Example: 3.4526 -> 34526 -> /10000 = 3.4526
    
  • return
    the int value of the string
    param
    length
    param
    source the String value to convert to an int

    
            // cut the entire string down to 6 characters
            if (source.length() > 7) {
                source = source.substring(0, 6);
            }
    
            // cut the string down to 4 decimal places
            while (source.length() - (source.indexOf('.") + 1) > 4) {
                source = source.substring(0, source.length()-1);
            }
    
            // convert to an int
            int value  = (source.indexOf('.") == -1) ?
    	    Integer.valueOf(source).intValue()
    	    : Integer.valueOf(new String(
    	    source.substring(0, source.indexOf('.")) +
    	    source.substring(source.indexOf('.")+1,
    	    source.length()))).intValue();
    
            // offset to 4 decimal placeholders
            int length = (source.indexOf('.") == -1) ? 0
                         : source.substring(source.indexOf('.")+1,
    					source.length()).length();
            if (length < 4) {
                int diff = 4 - length;
                while (diff-- > 0) { value *= 10; }
            }
            return value;
        
    public static voidparse(java.lang.String quoteString)

    Takes a String from the quote server or database and parses the string into each field. We first have to split it into small strings and then parse each string that should be a number.

    param
    quoteString the String to parse into the fields
    throws
    NumberFormatException is thrown if data that is not of the correct format is passed in -- where the number parts of the string cannot be converted to Integers because there are non-numeric characters in positions where numbers are expected
    throws
    StringIndexOutOfBoundsException is thrown if data that is not of the correct format is passed in -- where the deliminaters between the fields are not in the right spots, or the data is not of the correct length

    
            // get our starting index
            int index = quoteString.indexOf('"");
    	if (index == -1) {
    	    name = quoteString;
    	    return;
    	}
    
            // split the string up into it's fields
            name    = quoteString.substring(++index, 
    			    	(index = quoteString.indexOf('"", index)));
            index += 3;
            time    = quoteString.substring(index, 
    				(index = quoteString.indexOf('-", index))-1);
            index += 5;
            String Sprice  = quoteString.substring(index,
    				(index = quoteString.indexOf('<", index)));
            index += 6;
            String Schange = quoteString.substring(index,
    			       (index = quoteString.indexOf(',", index)));
            index += 2;
            String Slow    = quoteString.substring(index,
    				(index = quoteString.indexOf(' ", index)));
            index += 3;
            String Shigh   = quoteString.substring(index,
    				(index = quoteString.indexOf('"", index)));
            index += 2;
            String Sopen   = quoteString.substring(index,
    				(index = quoteString.indexOf(',", index)));
            ++index;
            String Sprev   = quoteString.substring(index, quoteString.length()-2);
    
            // convert the strings that should be numbers into ints
            price = makeInt(Sprice);
            // remove the '+' sign if it exists
            Schange = (Schange.indexOf('+") == -1) ? 
    	    Schange : Schange.substring(1, Schange.length());
            change = makeInt(Schange);
            prev   = makeInt(Sprev);
    	if ("N/A".equals(Slow)) {
    	    low = prev;
    	} else {
    	    low = makeInt(Slow);
    	}
    	if ("N/A".equals(Shigh)) {
    	    high = prev;
    	} else {
    	    high = makeInt(Shigh);
    	}
    	if ("N/A".equals(Sopen)) {
    	    open = prev; 
    	} else {
    	    open = makeInt(Sopen);
    	}