FileDocCategorySizeDatePackage
SyntaxIssues.javaAPI DocExample7414Sun Dec 14 22:47:40 GMT 2003oreilly.hcj.review

SyntaxIssues

public class SyntaxIssues extends Object
A class used to check code on the sytax issues section of the java review.
author
Robert (Kraythe) Simmons jr.

Fields Summary
Constructors Summary
Methods Summary
public static voidcontainsNested(java.util.List list, java.lang.Object target)
Demo of unnecessary if evaluation.

param
list __UNDOCUMENTED__
param
target __UNDOCUMENTED__

		Iterator iter = list.iterator();
		for (Set inner = null; iter.hasNext(); inner = (Set)iter.next()) {
			if (inner != null) {
				if (inner.contains(target)) {
					// do code.
				}
			}
		}
	
public static voidcontainsNested2(java.util.List list, java.lang.Object target)
Demo of abbreviated if evaluation.

param
list __UNDOCUMENTED__
param
target __UNDOCUMENTED__

		Iterator iter = list.iterator();
		for (Set inner = null; iter.hasNext(); inner = (Set)iter.next()) {
			if ((inner != null) && (inner.contains(target))) {
				// do code.
			}
		}
	
public static voidcontainsNested3(java.util.List list, java.lang.Object target)
Demo of abbreviated if evaluation.

param
list __UNDOCUMENTED__
param
target __UNDOCUMENTED__

		boolean flag = false;
		Iterator iter = list.iterator();
		for (Set inner = null; iter.hasNext(); inner = (Set)iter.next()) {
			if ((inner != null) && (flag = iter.hasNext())) {
				// do code.
				target.hashCode();  // used just to supress warnings.
				if (flag) {
					// do some extra code.
				}
			}
		}
	
public static voidcontinueFunc()
__UNDOCUMENTED__

		for (int idx = 0; idx < 1000; idx++) {
			// ... do some complex code. 
			if (idx == 555) {
				break;
			}

			// ... processing code
		}

		for (int idx = 0; idx < 1000; idx++) {
			// ... do some complex code. 
			if (idx == 555) {
				continue;
			}

			// ... processing code
		}
	
public static java.lang.Stringdecode(int input)
__UNDOCUMENTED__

param
input __UNDOCUMENTED__
return
__UNDOCUMENTED__

		String decoded = null;
		switch (input) {
			case 1:
				decoded = "Option 2";
				break;
			case 2:
			case 3:
				decoded = "Option 2";
				break;
			case 4:
				decoded = "Option 2";
				break;
			default:
				return "Option 3";
		}
		return decoded;
	
public static int[]extractXCoords(java.util.List points)
Extract x coordinates from a list of Points.

param
points The list of points.
return
The extracted coordinates

		int[] results = new int[points.size()];
		Point element = null;
		int idx = 0;
		Iterator iter = points.iterator();
		while (iter.hasNext()) {
			element = (Point)iter.next();
			results[idx] = element.x;
			idx++;
		}
		return results;
	
public static int[]extractXCoords2(java.util.List points)
Extract x coordinates from a list of Points.

param
points The list of points.
return
The extracted coordinates

		int[] results = new int[points.size()];
		Point element = null;
		Iterator iter = points.iterator();
		for (int idx = 0; iter.hasNext(); idx++) {
			element = (Point)iter.next();
			results[idx] = element.x;
		}
		return results;
	
public static final voidmain(java.lang.String[] args)
Main method.

param
args Arguments.

		// -- Test the extract points methods.
		List points = new LinkedList();
		points.add(new Point(22, 1));
		points.add(new Point(38, 34));
		points.add(new Point(27, 22));
		points.add(new Point(12, 103));
		points.add(new Point(9, 5));

		int[] intPts = extractXCoords(points);
		for (int idx = 0; idx < intPts.length; idx++) {
			System.out.print(intPts[idx] + ", ");
		}

		System.out.println();

		int[] intPts2 = extractXCoords2(points);
		for (int idx = 0; idx < intPts2.length; idx++) {
			System.out.print(intPts2[idx] + ", ");
		}

		System.out.println();

		// -- Test ternary
		System.out.println(someMethod(new Point(5, 9)));
		System.out.println(someElegantMethod(new Point(5, 9)));
	
public static voidmatrixMeth(java.awt.Point[][] values)
__UNDOCUMENTED__

param
values __UNDOCUMENTED__

		for (int x = 0; x < values[0].length; x++) {
			for (int y = 0; y < values.length; y++) {
				if ((values[x][y].x < 0) || (values[x][y].y < 0)) {
					break;  // exit to error logging line.
				}

				// do something with the value
			}
		}
		System.err.println("Invalid Point in Matrix");
		// continue processing
	
public static voidmatrixMeth2(java.awt.Point[][] values)
__UNDOCUMENTED__

param
values __UNDOCUMENTED__

		RESTART:  {
			for (int x = 0; x < values[0].length; x++) {
				for (int y = 0; y < values.length; y++) {
					if ((values[x][y].x < 0) || (values[x][y].y < 0)) {
						values[x][y].x = Math.max(values[x][y].x, 0);
						values[x][y].y = Math.max(values[x][y].y, 0);
						break RESTART;  // Try to process again!
					}

					// do something with the value
				}
			}
		}

		// continue processing
	
public static voidmatrixMeth3(java.awt.Point[][] values)
__UNDOCUMENTED__

param
values __UNDOCUMENTED__

		LINE: 
		for (int x = 0; x < values[0].length; x++) {
			COLUMN: 
			for (int y = 0; y < values.length; y++) {
				if ((values[x][y].x < 0) || (values[x][y].y < 0)) {
					continue LINE;  // skip the rest of the line;
				}

				// do something with the value
			}
		}
		LOGLINE: 
		System.err.println("Invalid Point in Matrix");

		// continue processing
		PASS_TWO: 
		for (int x = 0; x < values[0].length; x++) {
			// do some code.
		}
	
public static intsomeElegantMethod(java.awt.Point p)
__UNDOCUMENTED__

param
p __UNDOCUMENTED__
return
__UNDOCUMENTED__

		return (p == null) ? 0 : (p.x + p.y);
	
protected java.lang.ObjectsomeHelperMethod()
__UNDOCUMENTED__

return
__UNDOCUMENTED__

		Object result = null;

		// ... do some code that sets result.
		assert (result != null);  // check post condition.
		return result;
	
protected voidsomeLoop(java.util.Set set)
__UNDOCUMENTED__

param
set __UNDOCUMENTED__

		Iterator iter = set.iterator();
		while (iter.hasNext()) {
			String element = (String)iter.next();
			System.out.println(element.length());
		}
	
public static intsomeMethod(java.awt.Point p)
__UNDOCUMENTED__

param
p __UNDOCUMENTED__
return
__UNDOCUMENTED__

		if (p == null) {
			return 0;
		} else {
			return p.x + p.y;
		}