From: <cg...@us...> - 2009-09-06 07:34:46
|
Revision: 6759 http://jython.svn.sourceforge.net/jython/?rev=6759&view=rev Author: cgroves Date: 2009-09-06 07:34:32 +0000 (Sun, 06 Sep 2009) Log Message: ----------- Add a class to do simple argument validity checking, Preconditions. Like util.Generic, this is a reimplementation of some stuff from Google collections. It'd be nice to get proguard and jarjar set up to include little bits like this, but that's way more work. I wanted to use it in a couple places in antlr.PythonTree and antlr.PythonTokenSource, but those are in some non-standard format and use tabs for indentation. Are we keeping them pristine as they come from antlr, or have they just not been brought into our style? Modified Paths: -------------- branches/customizable-proxymaker/src/org/python/core/AbstractArray.java branches/customizable-proxymaker/src/org/python/core/FilelikeInputStream.java branches/customizable-proxymaker/src/org/python/core/PyList.java branches/customizable-proxymaker/src/org/python/core/PyString.java branches/customizable-proxymaker/src/org/python/core/PyTuple.java branches/customizable-proxymaker/src/org/python/core/io/TextIOInputStream.java branches/customizable-proxymaker/src/org/python/expose/generate/Exposer.java branches/customizable-proxymaker/src/org/python/jsr223/PyScriptEngine.java branches/customizable-proxymaker/src/org/python/modules/sre/SRE_STATE.java branches/customizable-proxymaker/src/org/python/util/Generic.java Added Paths: ----------- branches/customizable-proxymaker/src/org/python/util/Preconditions.java Modified: branches/customizable-proxymaker/src/org/python/core/AbstractArray.java =================================================================== --- branches/customizable-proxymaker/src/org/python/core/AbstractArray.java 2009-09-06 00:24:01 UTC (rev 6758) +++ branches/customizable-proxymaker/src/org/python/core/AbstractArray.java 2009-09-06 07:34:32 UTC (rev 6759) @@ -1,6 +1,9 @@ package org.python.core; import java.io.Serializable; + +import org.python.util.Preconditions; + import java.lang.reflect.Array; import java.util.Arrays; @@ -169,7 +172,7 @@ this.modCountIncr = 0; if (this.size != 0) { this.modCountIncr = 1; - clearRange(0, this.size); + clearRange(0, this.size); setSize(0); } @@ -354,9 +357,7 @@ } } else { - if (this.size == 0) { - throw new IllegalStateException("Cannot remove data from an empty array"); - } + Preconditions.checkState(!isEmpty(), "Cannot remove data from an empty array"); throw new IndexOutOfBoundsException("Index must be between 0 and " + (this.size - 1) + ", but was " + index); @@ -400,43 +401,41 @@ int arrayLen = Array.getLength(array); replaceSubArray(atIndex, Math.min(this.size, atIndex + arrayLen), array, 0, arrayLen); } - + /** * Replace a range of this array with another subarray. - * @param thisStart the start index (inclusive) of the subarray in this + * @param thisStart the start index (inclusive) of the subarray in this * array to be replaced - * @param thisStop the stop index (exclusive) of the subarray in this + * @param thisStop the stop index (exclusive) of the subarray in this * array to be replaced * @param srcArray the source array from which to copy * @param srcStart the start index (inclusive) of the replacement subarray * @param srcStop the stop index (exclusive) of the replacement subarray */ - public void replaceSubArray(int thisStart, int thisStop, Object srcArray, + public void replaceSubArray(int thisStart, int thisStop, Object srcArray, int srcStart, int srcStop) { - + this.modCountIncr = 0; - if (!srcArray.getClass().isArray()) { - throw new IllegalArgumentException("'array' must be an array type"); - } - + Preconditions.checkArgument(srcArray.getClass().isArray(), "'array' must be an array type"); + int replacedLen = thisStop - thisStart; if (thisStart < 0 || replacedLen < 0 || thisStop > this.size) { String message = null; if (thisStart < 0) { message = "thisStart < 0 (thisStart = " + thisStart + ")"; } else if (replacedLen < 0) { - message = "thisStart > thistStop (thisStart = " + thisStart + + message = "thisStart > thistStop (thisStart = " + thisStart + ", thisStop = " + thisStop + ")"; } else if (thisStop > this.size) { - message = "thisStop > size (thisStop = " + thisStop + + message = "thisStop > size (thisStop = " + thisStop + ", size = " + this.size + ")"; } else { throw new InternalError("Incorrect validation logic"); } - + throw new ArrayIndexOutOfBoundsException(message); } - + int srcLen = Array.getLength(srcArray); int replacementLen = srcStop - srcStart; if (srcStart < 0 || replacementLen < 0 || srcStop > srcLen) { @@ -444,38 +443,39 @@ if (srcStart < 0) { message = "srcStart < 0 (srcStart = " + srcStart +")"; } else if (replacementLen < 0) { - message = "srcStart > srcStop (srcStart = " + srcStart + + message = "srcStart > srcStop (srcStart = " + srcStart + ", srcStop = " + srcStop + ")"; } else if (srcStop > srcLen) { - message = "srcStop > srcArray length (srcStop = " + srcStop + + message = "srcStop > srcArray length (srcStop = " + srcStop + ", srcArray length = " +srcLen + ")"; } else { throw new InternalError("Incorrect validation logic"); } - + throw new IllegalArgumentException("start, stop and array must follow:\n\t" + "0 <= start <= stop <= array length\nBut found\n\t" + message); } - + int lengthChange = replacementLen - replacedLen; - + // Adjust array size if needed. if (lengthChange < 0) { remove(thisStop + lengthChange, thisStop); } else if (lengthChange > 0) { makeInsertSpace(thisStop, lengthChange); } - + try { this.modCountIncr = 1; System.arraycopy(srcArray, srcStart, getArray(), thisStart, replacementLen); } catch (ArrayStoreException e) { - throw new IllegalArgumentException("'ofArrayType' must be compatible with existing array type of " + - getArray().getClass().getName() + "\tsee java.lang.Class.getName()."); + throw new IllegalArgumentException( + "'ofArrayType' must be compatible with existing array type of " + + getArray().getClass().getName() + "\tsee java.lang.Class.getName()."); } } - + /** * Set the backing array. This method is used by the type-agnostic base * class code to set the array used for type-specific storage by the @@ -513,7 +513,7 @@ if (count > this.capacity) { ensureCapacity(count); } else if (count < this.size) { - clearRange(count, this.size); + clearRange(count, this.size); } this.size = count; } @@ -585,9 +585,9 @@ public int getModCountIncr() { return this.modCountIncr; } - + /** * @return an array of the given size for the type used by this abstract array. - */ + */ protected abstract Object createArray(int size); } Modified: branches/customizable-proxymaker/src/org/python/core/FilelikeInputStream.java =================================================================== --- branches/customizable-proxymaker/src/org/python/core/FilelikeInputStream.java 2009-09-06 00:24:01 UTC (rev 6758) +++ branches/customizable-proxymaker/src/org/python/core/FilelikeInputStream.java 2009-09-06 07:34:32 UTC (rev 6759) @@ -4,6 +4,7 @@ import java.io.InputStream; import org.python.core.util.StringUtil; +import org.python.util.Preconditions; public class FilelikeInputStream extends InputStream { @@ -23,9 +24,8 @@ } public int read(byte b[], int off, int len) throws IOException { - if(b == null) { - throw new NullPointerException(); - } else if((off < 0) || (off > b.length) || (len < 0) + Preconditions.checkNotNull(b, "Can't be given null array to read into"); + if((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if(len == 0) { Modified: branches/customizable-proxymaker/src/org/python/core/PyList.java =================================================================== --- branches/customizable-proxymaker/src/org/python/core/PyList.java 2009-09-06 00:24:01 UTC (rev 6758) +++ branches/customizable-proxymaker/src/org/python/core/PyList.java 2009-09-06 07:34:32 UTC (rev 6759) @@ -142,7 +142,7 @@ } if (value instanceof PyList) { if (value == this) { // copy - value = new PyList((PySequence) value); + value = new PyList(value); } setslicePyList(start, stop, step, (PyList) value); } else if (value instanceof PySequence) { Modified: branches/customizable-proxymaker/src/org/python/core/PyString.java =================================================================== --- branches/customizable-proxymaker/src/org/python/core/PyString.java 2009-09-06 00:24:01 UTC (rev 6758) +++ branches/customizable-proxymaker/src/org/python/core/PyString.java 2009-09-06 07:34:32 UTC (rev 6759) @@ -9,6 +9,7 @@ import org.python.expose.ExposedNew; import org.python.expose.ExposedType; import org.python.expose.MethodType; +import org.python.util.Preconditions; /** * A builtin python string. @@ -27,10 +28,7 @@ public PyString(PyType subType, String string) { super(subType); - if (string == null) { - throw new IllegalArgumentException( - "Cannot create PyString from null!"); - } + Preconditions.checkNotNull(string, "Cannot create PyString from null!"); this.string = string; } @@ -45,7 +43,7 @@ PyString(StringBuilder buffer) { this(TYPE, new String(buffer)); } - + /** * Creates a PyString from an already interned String. Just means it won't * be reinterned if used in a place that requires interned Strings. @@ -82,16 +80,16 @@ } return codePoints; } - + public String substring(int start, int end) { return string.substring(start, end); } - + public PyString __str__() { return str___str__(); } - @ExposedMethod(doc = BuiltinDocs.str___str___doc) + @ExposedMethod(doc = BuiltinDocs.str___str___doc) final PyString str___str__() { if (getClass() == PyString.class) { return this; @@ -212,7 +210,7 @@ private static ucnhashAPI pucnHash = null; - + public static String decode_UnicodeEscape(String str, int start, int end, @@ -468,7 +466,7 @@ } return ret; } - + //XXX: need doc @ExposedMethod(defaults = "null") final PyObject str___getslice__(PyObject start, PyObject stop, PyObject step) { @@ -511,7 +509,7 @@ return null; return string.equals(s) ? Py.False : Py.True; } - + public PyObject __lt__(PyObject other) { return str___lt__(other); } @@ -633,8 +631,8 @@ protected PyString createInstance(String str, boolean isBasic) { // ignore isBasic, doesn't apply to PyString, just PyUnicode return new PyString(str); - } - + } + public boolean __contains__(PyObject o) { return str___contains__(o); } @@ -678,7 +676,7 @@ } return repeat(o.asIndex(Py.OverflowError)); } - + @Override public PyObject __rmul__(PyObject o) { return str___rmul__(o); @@ -695,7 +693,7 @@ public PyObject __add__(PyObject other) { return str___add__(other); } - + @ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.str___add___doc) final PyObject str___add__(PyObject other) { if (other instanceof PyUnicode) { @@ -720,7 +718,7 @@ public PyObject __mod__(PyObject other) { return str___mod__(other); } - + @ExposedMethod(doc = BuiltinDocs.str___mod___doc) public PyObject str___mod__(PyObject other){ StringFormatter fmt = new StringFormatter(string, false); @@ -892,7 +890,7 @@ public String lower() { return str_lower(); } - + @ExposedMethod(doc = BuiltinDocs.str_lower_doc) final String str_lower() { return string.toLowerCase(); @@ -993,7 +991,7 @@ public String lstrip() { return str_lstrip(null); } - + public String lstrip(String sep) { return str_lstrip(sep); } @@ -1016,7 +1014,7 @@ public String rstrip(String sep) { return str_rstrip(sep); } - + @ExposedMethod(defaults = "null", doc = BuiltinDocs.str_rstrip_doc) final String str_rstrip(String sep) { char[] chars = string.toCharArray(); @@ -1415,7 +1413,7 @@ public int count(String sub, int start, int end) { return str_count(sub, start, Py.newInteger(end)); } - + @ExposedMethod(defaults = {"0", "null"}, doc = BuiltinDocs.str_count_doc) final int str_count(String sub, int start, PyObject end) { int[] indices = translateIndices(start, end); @@ -1506,7 +1504,7 @@ if (lowSval.equals("nan")) return Double.NaN; else if (lowSval.equals("inf")) return Double.POSITIVE_INFINITY; else if (lowSval.equals("-inf")) return Double.NEGATIVE_INFINITY; - + if (lowSval.endsWith("d") || lowSval.endsWith("f")) { throw new NumberFormatException("format specifiers not allowed"); } @@ -1628,10 +1626,10 @@ // if the base >= 22, then an 'l' or 'L' is a digit! if (base < 22 && e > b && (str.charAt(e-1) == 'L' || str.charAt(e-1) == 'l')) e--; - + if (b > 0 || e < str.length()) str = str.substring(b, e); - + try { java.math.BigInteger bi = null; if (sign == '-') @@ -1670,7 +1668,7 @@ } return fillchar.charAt(0); } - + public String ljust(int width) { return str_ljust(width, null); } @@ -1678,7 +1676,7 @@ public String ljust(int width, String padding) { return str_ljust(width, padding); } - + @ExposedMethod(defaults="null", doc = BuiltinDocs.str_ljust_doc) final String str_ljust(int width, String fillchar) { char pad = parse_fillchar("ljust", fillchar); @@ -1714,7 +1712,7 @@ int half = n/2; if (n%2 > 0 && width%2 > 0) half += 1; - + return padding(half, pad)+string+padding(n-half, pad); } @@ -1803,7 +1801,7 @@ return replace((PyString)oldPiece, (PyString)newPiece, maxsplit == null ? -1 : maxsplit.asInt()); } - + protected PyString replace(PyString oldPiece, PyString newPiece, int maxsplit) { int len = string.length(); int old_len = oldPiece.string.length(); @@ -1813,7 +1811,7 @@ } return createInstance(string, true); } - + if (old_len == 0 && newPiece.string.length() != 0 && maxsplit !=0) { // old="" and new != "", interleave new piece with each char in original, taking in effect maxsplit StringBuilder buffer = new StringBuilder(); @@ -1826,7 +1824,7 @@ buffer.append(string.substring(i)); return createInstance(buffer.toString(), true); } - + if(maxsplit == -1) { if(old_len == 0) { maxsplit = len + 1; @@ -1834,7 +1832,7 @@ maxsplit = len; } } - + return newPiece.join(splitfields(oldPiece.string, maxsplit)); } @@ -1979,16 +1977,16 @@ @ExposedMethod(defaults = {"0", "null"}, doc = BuiltinDocs.str_startswith_doc) final boolean str_startswith(PyObject prefix, int start, PyObject end) { int[] indices = translateIndices(start, end); - + if (prefix instanceof PyString) { String strPrefix = ((PyString)prefix).string; if (indices[1] - indices[0] < strPrefix.length()) return false; - + return string.startsWith(strPrefix, indices[0]); } else if (prefix instanceof PyTuple) { PyObject[] prefixes = ((PyTuple)prefix).getArray(); - + for (int i = 0 ; i < prefixes.length ; i++) { if (!(prefixes[i] instanceof PyString)) throw Py.TypeError("expected a character buffer object"); @@ -1996,7 +1994,7 @@ String strPrefix = ((PyString)prefixes[i]).string; if (indices[1] - indices[0] < strPrefix.length()) continue; - + if (string.startsWith(strPrefix, indices[0])) return true; } @@ -2027,7 +2025,7 @@ return substr.endsWith(((PyString)suffix).string); } else if (suffix instanceof PyTuple) { PyObject[] suffixes = ((PyTuple)suffix).getArray(); - + for (int i = 0 ; i < suffixes.length ; i++) { if (!(suffixes[i] instanceof PyString)) throw Py.TypeError("expected a character buffer object"); @@ -2039,16 +2037,16 @@ } else { throw Py.TypeError("expected a character buffer object or tuple"); } - } + } /** * Turns the possibly negative Python slice start and end into valid indices * into this string. - * + * * @return a 2 element array of indices into this string describing a * substring from [0] to [1]. [0] <= [1], [0] >= 0 and [1] <= * string.length() - * + * */ protected int[] translateIndices(int start, PyObject end) { int iEnd; @@ -2569,7 +2567,7 @@ // A magic number. Larger than in CPython. throw Py.OverflowError("formatted " + type + " is too long (precision too long?)"); } - + } private String formatLong(PyObject arg, char type, boolean altFlag) { @@ -2955,7 +2953,7 @@ string = formatFloatExponential(arg, c, false); break; case 'f': - case 'F': + case 'F': string = formatFloatDecimal(arg, false); // if (altFlag && string.indexOf('.') == -1) // string += '.'; Modified: branches/customizable-proxymaker/src/org/python/core/PyTuple.java =================================================================== --- branches/customizable-proxymaker/src/org/python/core/PyTuple.java 2009-09-06 00:24:01 UTC (rev 6758) +++ branches/customizable-proxymaker/src/org/python/core/PyTuple.java 2009-09-06 07:34:32 UTC (rev 6759) @@ -13,6 +13,7 @@ import org.python.expose.ExposedNew; import org.python.expose.ExposedType; import org.python.expose.MethodType; +import org.python.util.Preconditions; /** * A builtin python tuple. @@ -326,10 +327,9 @@ } public List subList(int fromIndex, int toIndex) { + Preconditions.checkArgument(fromIndex <= toIndex, "to can't be greater than from"); if (fromIndex < 0 || toIndex > size()) { throw new IndexOutOfBoundsException(); - } else if (fromIndex > toIndex) { - throw new IllegalArgumentException(); } PyObject elements[] = new PyObject[toIndex - fromIndex]; for (int i = 0, j = fromIndex; i < elements.length; i++, j++) { Modified: branches/customizable-proxymaker/src/org/python/core/io/TextIOInputStream.java =================================================================== --- branches/customizable-proxymaker/src/org/python/core/io/TextIOInputStream.java 2009-09-06 00:24:01 UTC (rev 6758) +++ branches/customizable-proxymaker/src/org/python/core/io/TextIOInputStream.java 2009-09-06 07:34:32 UTC (rev 6759) @@ -1,9 +1,11 @@ /* Copyright (c) Jython Developers */ package org.python.core.io; +import java.io.IOException; import java.io.InputStream; -import java.io.IOException; +import org.python.util.Preconditions; + /** * An InputStream tie-in to a TextIOBase. */ @@ -26,14 +28,13 @@ if (result.length() == 0) { return -1; } - return (int)result.charAt(0); + return result.charAt(0); } @Override public int read(byte[] b, int off, int len) throws IOException { - if (b == null) { - throw new NullPointerException(); - } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) + Preconditions.checkNotNull(b, "Can't be given null array to read into"); + if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { Modified: branches/customizable-proxymaker/src/org/python/expose/generate/Exposer.java =================================================================== --- branches/customizable-proxymaker/src/org/python/expose/generate/Exposer.java 2009-09-06 00:24:01 UTC (rev 6758) +++ branches/customizable-proxymaker/src/org/python/expose/generate/Exposer.java 2009-09-06 07:34:32 UTC (rev 6759) @@ -10,11 +10,12 @@ import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; import org.python.core.BytecodeLoader; +import org.python.util.Preconditions; /** * Base class that handles the basics of generating a single class with asm. Subclass to supply the * actual functionality of the generated class. - * + * */ public abstract class Exposer implements Opcodes, PyTypes { @@ -29,7 +30,7 @@ /** The type that will be generated. */ protected Type thisType; - + protected Type[] interfacesImplemented; /** Maps from a primitive type to its wrapper */ @@ -125,11 +126,9 @@ * Push args onto the stack corresponding to the types passed to the constructor. */ public void pushArgs() { - if(types.length > 0) { - throw new IllegalStateException("If the constuctor takes types as indicated by " - + "passing their types to Instantiator, pushArgs must be overriden to put " - + "those args on the stack before the call"); - } + Preconditions.checkState(types.length == 0, "If the constuctor takes types as " + + "indicated by passing their types to Instantiator, pushArgs must be overriden to " + + "put those args on the stack before the call"); }; public Type[] getTypes() { Modified: branches/customizable-proxymaker/src/org/python/jsr223/PyScriptEngine.java =================================================================== --- branches/customizable-proxymaker/src/org/python/jsr223/PyScriptEngine.java 2009-09-06 00:24:01 UTC (rev 6758) +++ branches/customizable-proxymaker/src/org/python/jsr223/PyScriptEngine.java 2009-09-06 07:34:32 UTC (rev 6759) @@ -15,6 +15,8 @@ import javax.script.ScriptEngineFactory; import javax.script.ScriptException; import javax.script.SimpleBindings; + +import org.python.util.Preconditions; import org.python.util.PythonInterpreter; public class PyScriptEngine extends AbstractScriptEngine implements Compilable, Invocable { @@ -120,12 +122,8 @@ } public <T> T getInterface(Object obj, Class<T> clazz) { - if (obj == null) { - throw new IllegalArgumentException("object expected"); - } - if (clazz == null || !clazz.isInterface()) { - throw new IllegalArgumentException("interface expected"); - } + Preconditions.checkNotNull(obj, "object expected"); + Preconditions.checkArgument(clazz != null && clazz.isInterface(), "interface expected"); final PyObject thiz = Py.java2py(obj); @SuppressWarnings("unchecked") T proxy = (T) Proxy.newProxyInstance( Modified: branches/customizable-proxymaker/src/org/python/modules/sre/SRE_STATE.java =================================================================== --- branches/customizable-proxymaker/src/org/python/modules/sre/SRE_STATE.java 2009-09-06 00:24:01 UTC (rev 6758) +++ branches/customizable-proxymaker/src/org/python/modules/sre/SRE_STATE.java 2009-09-06 07:34:32 UTC (rev 6759) @@ -17,18 +17,19 @@ package org.python.modules.sre; import org.python.core.PyString; +import org.python.util.Preconditions; public class SRE_STATE { - + /* * Generated from Python-2.4.5 like 'python headerToJava.py < Modules/sre_constants.h' - * where headerToJava.py contains the following code + * where headerToJava.py contains the following code import sys for line in sys.stdin: if line.startswith('#define'): line = line.replace('#define', 'public static final int').strip() segs = line.split(' ') - print '%s = %s;' % (' '.join(segs[:-1]), segs[-1]) + print '%s = %s;' % (' '.join(segs[:-1]), segs[-1]) */ //BEGIN generated code public static final int SRE_MAGIC = 20031017; @@ -108,7 +109,7 @@ //From here we're including things from _sre.c in the order they're defined there public static final int USE_RECURSION_LIMIT = 5000; - + /* error codes */ public static final int SRE_ERROR_ILLEGAL = -1; public static final int SRE_ERROR_STATE = -2; @@ -188,7 +189,7 @@ return false; } } - + final boolean sre_category(int category, int ch) { switch (category) { @@ -287,7 +288,7 @@ } private void mark_restore(int lo, int hi, int mark_stack_base) { - + if (hi <= lo) return; @@ -299,7 +300,7 @@ System.arraycopy(mark_stack, this.mark_stack_base, mark, lo, size); } - + final boolean SRE_AT(int ptr, int at) { /* check if pointer is at given position. */ @@ -370,7 +371,7 @@ case SRE_OP_FAILURE: TRACE(setidx, ch, "CHARSET FAILURE"); return !ok; - + case SRE_OP_LITERAL: TRACE(setidx, ch, "CHARSET LITERAL " + set[setidx]); /* <LITERAL> <code> */ @@ -378,7 +379,7 @@ return ok; setidx++; break; - + case SRE_OP_CATEGORY: /* <CATEGORY> <code> */ TRACE(setidx, ch, "CHARSET CHARSET " + set[setidx]); @@ -394,13 +395,13 @@ // (set[setidx + (ch >> 4)] & (1 << (ch & 15))) != 0) // return ok; // setidx += 16; - + /* <CHARSET> <bitmap> (32 bits per code word) */ if (ch < 256 && (set[setidx + (ch >> 5)] & (1 << (ch & 31))) != 0) return ok; setidx += 8; break; - + case SRE_OP_RANGE: /* <RANGE> <lower> <upper> */ TRACE(setidx, ch, "CHARSET RANGE " + set[setidx] + " " + set[setidx+1]); @@ -413,11 +414,11 @@ TRACE(setidx, ch, "CHARSET NEGATE"); ok = !ok; break; - + case SRE_OP_BIGCHARSET: /* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */ TRACE(setidx, ch, "CHARSET BIGCHARSET "); - + // count = *(set++); // if (!(ch & ~65535)) // block = ((unsigned char*)set)[ch >> 8]; @@ -428,7 +429,7 @@ // (set[block*8 + ((ch & 255)>>5)] & (1 << (ch & 31)))) // return ok; // set += count*8; - + int count = set[setidx++]; int block; if (ch < 65536) @@ -438,7 +439,7 @@ setidx += 64; if (block >= 0 && (set[setidx + block*8 + ((ch & 255)>>5)] & (1 << (ch & 31))) != 0) return ok; - setidx += count * 8; + setidx += count * 8; break; default: @@ -449,7 +450,7 @@ } } } - + private int SRE_COUNT(int[] pattern, int pidx, int maxcount, int level) { int chr; int ptr = this.ptr; @@ -468,7 +469,7 @@ while (ptr < end && SRE_CHARSET(pattern, pidx + 2, str[ptr])) ptr++; break; - + case SRE_OP_ANY: /* repeated dot wildcard. */ TRACE(pidx, ptr, "COUNT ANY"); @@ -594,7 +595,7 @@ pidx++; ptr++; break; - + case SRE_OP_SUCCESS: /* end of pattern */ TRACE(pidx, ptr, "SUCCESS"); @@ -752,7 +753,7 @@ } lastmark = this.lastmark; lastindex = this.lastindex; - + if (pattern[pidx + pattern[pidx]] == SRE_OP_LITERAL) { /* tail starts with a literal. skip positions where the rest of the pattern cannot possibly match */ @@ -790,7 +791,7 @@ } } return 0; - + case SRE_OP_MIN_REPEAT_ONE: /* match repeated sequence (minimizing regexp) */ @@ -845,9 +846,7 @@ return c; if (c == 0) break; - if(c != 1){ - throw new IllegalStateException("c should be 1!"); - } + Preconditions.checkState(c == 1, "c should be 1!"); ptr++; count++; LASTMARK_RESTORE(lastmark, lastindex); @@ -956,7 +955,7 @@ this.ptr = ptr; return 0; } - + lastmark = this.lastmark; lastindex = this.lastindex; @@ -983,7 +982,7 @@ this.ptr = ptr; return 0; - + case SRE_OP_GROUPREF: /* match backreference */ i = pattern[pidx]; @@ -1017,7 +1016,7 @@ } pidx++; break; - + case SRE_OP_GROUPREF_EXISTS: i = pattern[pidx]; TRACE(pidx, ptr, "GROUPREF_EXISTS " + i); @@ -1029,7 +1028,7 @@ } pidx += 2; break; - + case SRE_OP_ASSERT: /* assert subpattern */ /* args: <skip> <back> <pattern> */ @@ -1058,7 +1057,7 @@ } pidx += pattern[pidx]; break; - + case SRE_OP_FAILURE: /* immediate failure */ TRACE(pidx, ptr, "FAILURE"); @@ -1081,7 +1080,7 @@ this.lastindex = lastindex; } } - + int SRE_SEARCH(int[] pattern, int pidx) { int ptr = this.start; int end = this.end; @@ -1282,7 +1281,7 @@ } // XXX - this is not UTF-16 compliant; also depends on whether from PyString or PyUnicode - + String getslice(int index, String string, boolean empty) { int i, j; Modified: branches/customizable-proxymaker/src/org/python/util/Generic.java =================================================================== --- branches/customizable-proxymaker/src/org/python/util/Generic.java 2009-09-06 00:24:01 UTC (rev 6758) +++ branches/customizable-proxymaker/src/org/python/util/Generic.java 2009-09-06 07:34:32 UTC (rev 6759) @@ -33,6 +33,7 @@ public static <T> List<T> list() { return new ArrayList<T>(); } + /** * Makes a List with its generic type inferred from whatever it's being assigned to filled with * the items in <code>contents</code>. Added: branches/customizable-proxymaker/src/org/python/util/Preconditions.java =================================================================== --- branches/customizable-proxymaker/src/org/python/util/Preconditions.java (rev 0) +++ branches/customizable-proxymaker/src/org/python/util/Preconditions.java 2009-09-06 07:34:32 UTC (rev 6759) @@ -0,0 +1,53 @@ +package org.python.util; + +/** + * Static methods to be called at the beginning of a method to verify arguments and state. Inspired + * by <a href="http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/base/Preconditions.html"> + * Google Collections</a>. + */ +public class Preconditions { + + /** + * Ensures the truth of an expression involving one or more parameters to the calling method. + * + * @param errorMessage + * - the exception message to use if the check fails; will be converted to a string + * using {@link String#valueOf}. + * @throws IllegalArgumentException - if <code>expression</code> is false. + */ + public static void checkArgument(boolean expression, Object errorMessage) { + if (!expression) { + throw new IllegalArgumentException(String.valueOf(errorMessage)); + } + } + + /** + * Ensures that a reference is not to null. + * + * @param errorMessage + * - the exception message to use if the check fails; will be converted to a string + * using {@link String#valueOf}. + * @throws NullPointerException - if <code>toCheck</code> is null. + */ + public static void checkNotNull(Object toCheck, Object errorMessage) { + if (toCheck == null) { + throw new NullPointerException(errorMessage.toString()); + } + } + + /** + *Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + * @param errorMessage + * - the exception message to use if the check fails; will be converted to a string + * using {@link String#valueOf}. + * @throws IllegalStateException + * - if <code>expression</code> is false. + */ + public static void checkState(boolean expression, Object errorMessage) { + if (!expression) { + throw new IllegalStateException(errorMessage.toString()); + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |