Currently showing src/Delegate.java Line Numbers: [show|hide]

//Delegate.java
package com.tanksoftware.util;

import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.awt.Color;

import com.tanksoftware.io.*;

/**
 * Some useful stand-alone miscellaneous methods.
 *
 * <p>Delegate was created by William Denniss on 5 April 2001.
 * <p>This class has a range of handy methods
 *
 * <p>William Denniss has asserted his right under the Copyright, Designs and Patents Act 1988 to be identified as the author of this work.
 *
 * This text is distributed under the WIL license.  I
 * If you don't agree with its terms, then please remove all WIL protected software now.
 * @version 1.3 - 4 April 2002
 * @author William Denniss
 *
 */
public class Delegate {

	public static final String PROGRAM_NAME = "Delegate";
	public static final String VERSION_NUMBER = "1.2";
	public static final String DATE_CREATED = "5 April 2001";
	public static final String DATE_UPDATED = "21 June 2001";
	public static final String AUTHOR = "William Denniss";


	/** Causes the current thread to temporarily cease execution for the given number of milliseconds.
	 * Unlike the wait or sleep methods, no InterrupedException is thrown.
	 * @param milliseconds the number of milliseconds you want the current thread to temporarily cease execution.
	 */
	public static void doze (int milliseconds) {

		try	{
			Thread.sleep(milliseconds);
		} catch (InterruptedException ie) {
		}
	}

	/** Adjusts a integer to a given scale, returning an integer.
	 * This method is a quick way to scale and round an integer.  Normally, when an integer is scaled the returned result is a double, which is not always desired.
	 * @param number the integer you want to scale.
	 * @param scale the number the integer is to be scaled too.
	 * @return The given number that has been scaled, and rounded to the nearest integer.
	 */
	public static int scaleAndRound (int number, double scale) {
		return (int) Math.round(number * scale);
	}


	/** Compares a single character of one string, with the entire string of another ignoreing case
	 *
	 *@depreciated Due to the fact, it is better to add "" to a char, rarther than using Character.toString()
	 *
	 */
	public static boolean equals (String parsedString, int position, String compareString) {
		boolean resault = new Character(parsedString.charAt(position)).toString().equalsIgnoreCase(compareString);
		return new Character(parsedString.charAt(position)).toString().equalsIgnoreCase(compareString);

	}

	/** Return the string at the given position, using the default delimeter
	 */
	public static String carvePos(String uncarved, int position) {
		return carvePos(uncarved, position, " ");
	}

	/** Return the string at the given position, using the parsed delimeter
	 */
	public static String carvePos(String uncarved, int position, String delimeter) throws NullPointerException, NoSuchElementException{
		String carved = "";

		try {
			StringTokenizer st = new StringTokenizer(uncarved,delimeter);
			for (int i=0; i<=position; i++) {
				carved = st.nextToken();
			}
		} catch (NoSuchElementException e) {
			carved = "";
		}
		return carved;
	}

	/** Return an array of Strings, split using the default delimeter
	 */
	public static String [] stringCarver (String uncarved) throws NullPointerException{
		return stringCarver(uncarved, " ");
	}

	/** Return an array of Strings, split using the parsed delimeter
	 */
	public static String [] stringCarver (String uncarved, String delimeter) throws NullPointerException{
		int counter=0;
		String waste;
		StringTokenizer st = new StringTokenizer(uncarved,delimeter);

		while (st.hasMoreTokens()){
			waste = st.nextToken();
			counter++;
		}

		st = new StringTokenizer(uncarved,delimeter);
		String [] carved = new String [counter];

		for (int i = 0; i<counter; i++) {
			carved[i] = st.nextToken();
		}

		return carved;

	}

	/** Convert a LinkedList containing only String objects, into an array of String objects
	 */
	public static String [] linkedList2StringArray (LinkedList toConvert) {

		String [] converted = new String [toConvert.size()];

		for (int i = 0; i<toConvert.size(); i++) {
			converted[i] = (String) toConvert.get(i);
		}
		return converted;

	}

	/** Convert an array of Strings (contining ints) to an array of ints
	 */
	public static int [] string2int (String [] strings) {
		int [] ints = new int [strings.length];
		for (int i = 0; i<strings.length; i++) {

			try {
				ints[i] = Integer.parseInt(strings[i]);
			} catch (Exception e) {
			}

		}

		return ints;

	}

	/** Convert an array of Strings (contining doubles) to an array of doubles
	 */
	public static double [] string2double (String [] strings) {
		double [] ints = new double [strings.length];
		for (int i = 0; i<strings.length; i++) {

			try {
				ints[i] = Double.parseDouble(strings[i]);
			} catch (Exception e) {
			}

		}

		return ints;

	}

  	/**
     * Returns the hexidecimal colour code of a given colour.
	 */
	public static String betterToRGB(Color colour) {
		String [] rgb = {Integer.toString(colour.getRed(),16), Integer.toString(colour.getGreen(),16), Integer.toString(colour.getBlue(),16)};
		for (int i = 0; i<rgb.length; i++) {
			if (rgb[i].length() == 1)
				rgb[i] = "0" + rgb[i];
		}

		return rgb[0]+rgb[1]+rgb[2];
	}

  	/**
     * Rounds a number to a set number of digits.
	 */
	public static double significentDigits (double toRound, int noDigits) {
		toRound = toRound * (noDigits * 10);
		toRound = Math.round(toRound);
		toRound = toRound / (noDigits * 10);

		return toRound;
	}

	/* Load from a file all the lines of that file that don't start with "<", or ";" and contain something.
	 *@deprecated It is better to use the LinkedList loadList (TSFS fileSystem, String filename);
	 *
	public static LinkedList loadList (String filename) throws IOException{

		BufferedReader br = null;// new BufferedReader(new FileReader(filename));

		// Cater for applets, which will have http:// or file:/ in their paths.
		if (filename.lastIndexOf(":/") == -1) {
			br = new BufferedReader(new FileReader(filename));
		} else {
			try {
				//Get the BufferedReader to read an URL
				URL url = new URL(filename);

				URLConnection conn = null;
				InputStreamReader data = null;
				conn = url.openConnection();
				conn.connect();
				br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

				//br = new BufferedReader(new FileReader(new File(new URI(filename))));
			} catch (MalformedURLException e) {
			}
		}

		return loadList(br);
	}
	*/

  	/**
     * Loads a flat list from a specified file, with a Tank Software File System (TSFS).
     *
     *@param filename The name of the file to load
     *@param fileSystem The File System load the file through
     *@returns A Linked List containing a String object for each of the lines in the file
     *@throws IOException Error reading file.
     */
	public static LinkedList loadList (String filename, TSFS fileSystem) throws IOException {
		return loadList (fileSystem.loadFile(filename));
	}

    /**
     * Loads a flat list from a BufferedReader, which could point to a file, string or whatever.
     *
     *@param br The BufferedReader through which the list will be constructed.
     *@returns A Linked List containing a String object for each of the lines in the file
     *@throws IOException Error reading file.
	 */
	public static LinkedList loadList (BufferedReader br) throws IOException{
		LinkedList fileList = new LinkedList();
		String tmp;
		try {
			while (true) {
				tmp = br.readLine();
				try {
					if (!(new Character(tmp.charAt(0)).toString().equalsIgnoreCase("<") || tmp.equals("") || new Character(tmp.charAt(0)).toString().equalsIgnoreCase(";"))) {
						fileList.add(tmp);
					}
				} catch (StringIndexOutOfBoundsException e) {
					//ignore blank lines
				}

			}

		} catch (NullPointerException e) {
			//end of the file, no problem
		}

		return fileList;
	}

}

Total 265 Lines of Code.
Source code formatted using showsrc by William Denniss