Appendix: Tools include some of the tools used in compiling this book (code). Some of them may be temporary, if the reference code is moved to CVS, they may disappear here.
ANT Extended Ant provides an extended API, you can use them to create your own tasks with Java. You can find detailed information from Ant's official documentation or published books about Ant. As another choice, you can simply write a Java program and call in Ant; use this method, you don't need to learn to extend the API. For example, in order to compile this book, we need to determine that the Java version used by the user is JDK 1.3 or higher, so there is the following procedure:
//: COM: Bruceeckel: Tools: CheckVersion.java
// {runbyhand}
Package com.bruceeckel.tools;
Public class checkversion {
Public static void main (String [] args) {
String Version = System.getProperty ("java.version");
Char minor = version.Charat (2);
CHAR POINT = Version.Charat (4);
IF (Minor <'3' || Point <'0')
Throw new runtimeException ("JDK 1.3.0 or Higher"
"IS Required to Run The Examples in this book.");
System.out.println ("JDK Version" Version "Found");
}
} ///: ~ This program simply uses System.getProperty () to get a Java version, and throw an exception if the version number is less than 1.3. When the ANT encounters this exception, it will stop. In this way, when you want to detect the version number, you can add the following lines of scripts in any BuildFile:
TaskName = "CheckVersion" ClassName = "com.bruceeckel.tools.checkversion" Classpath = "$ {basedir}" Fork = "true" Failοnerrοr = "true" /> Add new tools with this method, you can complete from encoding to testing very quickly. If they have proven to be reasonable, you can spend another effort to write an Ant extension. Array Utilities Despite it, (Java comes with it) ARRAYS class is still not comprehensive enough. For example, if you can print all elements of an array instead of each time you have to write for a loop, it is fine. You will see that the Fill () method can only put a value in an array. If you want to use a group of raised numbers to fill an array, the Fill () method is not powerful. In this way, some additional utilities (Utilities) have a significant meaning as an ARRAYS class, which is convenient, I put them in a com.bruceeckel.util this package. These small programs can print an arbitrary type array or fill a value or object generated by the Dongdong called Generator. Because (utility) code needs to support each basic type (Primitive Type) and Object type, there is a lot of almost repetitive code. For example, each type requires a "generator" interface because the return type of the next () method is different in each case. //: COM: Bruceeckel: util: generator.java Package com.bruceeckel.util; Public interface generator {Object next ();} ///: ~ //: COM: Bruceeckel: Util: Booleangenerator.java Package com.bruceeckel.util; Public interface boleangenerator {boolean next ();} / //: ~ //: com: Bruceeckel: Util: bytegenerator.java Package com.bruceeckel.util; Public interface bytegenerator {byte next ();} ///: ~ //: COM: Bruceeckel: Util: Chargenerator.java Package com.bruceeckel.util; Public interface chargenerator {char next ();} ///: ~ //: com: Bruceeckel: Util: Shortgenerator.java Package com.bruceeckel.util; Public interface shortgenerator {short next ();} ///: ~ //: com: Bruceeckel: util: intGnenerator.java Package com.bruceeckel.util; Public interface intGnenerator {int next ();} ///: ~ //: com: Bruceeckel: Util: longgenerator.java Package com.bruceeckel.util; Public interface longgenerator {long next ();} ///: ~ //: COM: Bruceeckel: Util: floatgenerator.java Package com.bruceeckel.util; Public interface floatGenerator {float next ();} ///: ~ //: com: Bruceeckel: Util: doublegenerator.java Package com.bruceeckel.util; Public interface doublegenerator {double next ();} ///: ~ Array2 includes a series of toString () methods for each type. These methods make it easy to print an array. The code of toString () uses the StringBuffer object instead of the String object. This is the reason for efficiency; when you are assembling a string in a method that may be called multiple times, more sensible practices are those with higher efficiency StringBuffer instead of using a relatively easy String type operating. Here, give it an initial value when creating a StringBuffger, then add the String object to it. Finally, convert the Result object into a String object as the return value of the function. //: com: Bruceeckel: Util: arrays2.java // a supplement to java.util.Arrays, To Provide Additional // USEful FunctionAlicity When Working with arrays. Allows // any array to be conveilt to a string, and to be filled // via a user-defined "generator" Object. Package com.bruceeckel.util; Import java.util. *; Public class arrays2 { Public static string toString (boolean [] a) { StringBuffer Result = New StringBuffer ("["); For (int i = 0; i Result.Append (a [i]); IF (i Result.Append (","); } Result.Append ("]"); Return Result.toString (); } Public static string toString (byte [] a) { StringBuffer Result = New StringBuffer ("["); For (int i = 0; i Result.Append (a [i]); IF (i Result.Append (","); } Result.Append ("]"); Return Result.toString (); } Public static string toString (char [] a) { StringBuffer Result = New StringBuffer ("["); For (int i = 0; i Result.Append (a [i]); IF (i Result.Append (","); } Result.Append ("]"); Return Result.toString (); } Public static string toString (short [] a) { StringBuffer Result = New StringBuffer ("["); For (int i = 0; i Result.Append (a [i]); IF (i Result.Append (","); Result.Append ("]"); Return Result.toString (); } Public Static String Tostring (int [] a) { StringBuffer Result = New StringBuffer ("["); For (int i = 0; i Result.Append (a [i]); IF (i Result.Append (","); } Result.Append ("]"); Return Result.toString (); } Public static string toString (long [] a) { StringBuffer Result = New StringBuffer ("["); For (int i = 0; i Result.Append (a [i]); IF (i Result.Append (","); } Result.Append ("]"); Return Result.toString (); } Public Static String Tostring (float [] a) { StringBuffer Result = New StringBuffer ("["); For (int i = 0; i Result.Append (a [i]); IF (i Result.Append (","); } Result.Append ("]"); Return Result.toString (); } Public static string toString (double [] a) { StringBuffer Result = New StringBuffer ("["); For (int i = 0; i Result.Append (a [i]); IF (i Result.Append (","); } Result.Append ("]"); Return Result.toString (); } // Fill An Array Using A Generator: Public Static Void Fill (Object [] a, generator gen) { Fill (a, 0, a.length, gen); } Public static void Fill (Object [] a, int from, int to, generator gen) { For (int i = from; i a [i] = gen.next (); } Public static void Fill (Boolean [] A, Booleangenerator Gen { Fill (a, 0, a.length, gen); } Public static void Fill (Boolean [] A, Int from, Int to, Booleangenerator Gen) { For (int i = from; i a [i] = gen.next (); Public Static Void Fill (Byte [] a, bytegenerator gen) { Fill (a, 0, a.length, gen); } Public static void Fill (Byte [] a, int from, int to, bytegenerator gen) { For (int i = from; i a [i] = gen.next (); } Public Static Void Fill (Char [] a, chargenerator gen) { Fill (a, 0, a.length, gen); } Public static void Fill (Char [] a, int from, int to, chargenerator gen) { For (int i = from; i a [i] = gen.next (); } Public Static Void Fill (Short [] a, shortgenerator gen) { Fill (a, 0, a.length, gen); } Public static void Fill (Short [] a, int from, int to, shortgenerator gen { For (int i = from; i a [i] = gen.next (); } Public Static Void Fill (int [] a, intGnenerator gen) { Fill (a, 0, a.length, gen); } Public static void Fill (int [】 a, int from, int to, intgenerator gen) { For (int i = from; i a [i] = gen.next (); } Public Static Void Fill (long [] a, longgenerator gen) { Fill (a, 0, a.length, gen); } Public static void Fill (Long [] A, Int from, int to, longgenerator gen) { For (int i = from; i a [i] = gen.next (); } Public Static Void Fill (Float [] a, floatgenerator gen { Fill (a, 0, a.length, gen); } Public static void Fill (float [] a, int from, int to, floatgenerator gen) { For (int i = from; i a [i] = gen.next (); } Public Static Void Fill (Double [] a, doublegenerator gen) { Fill (a, 0, a.length, gen); } Public static void Fill (Double [] A, int from, int to, doublegenerator gen { For (int i = from; i a [i] = gen.next (); } Private static random r = new random (); Public Static ClassRandBooleangenerator Implements Booleangenerator { Public boolean next () {return r.nextBoolean ();} } Public Static Class Randbytegenerator IMPLEments bytegenerator { Public Byte Next () {Return (Byte) R.NextINT (); } Private stat String ssource = "AbcdefghijklmnopqrStuvwxyzabcdefghijklmnopqrstuvwxyz"; Private static char [] src = ssource.tochararray (); Public Static Class Randchargenerator imports chargenerator { Public char next () { Return Src [R.NextINT (src.length)]; } } Public Static Class RandstringGenerator imports generator { PRIVATE INT LEN; Private randchargenerator cg = new randchargenerator (); Public randstringgenerator (int length) { Len = Length; } Public Object next () { Char [] buf = new char [len]; For (int i = 0; i BUF [i] = cg.next (); Return New String (BUF); } } Public Static Class RandshortGenerator Implements Shortgenerator { Public short next () {return (short) r.next (); } Public Static Class RandintGenerator imports intGnenerator { Private int mod = 10000; Public randintgenerator () {} Public RandintGenerator (int mod = modulo; Public int next () {return r.nextint (mod); } Public Static Class RandlongGenerator imports longgenerator { Public long next () {return r.nextlong (); } Public Static Class RandfloatGenerator imports floatgenerator { Public float next () {return r.nextfloat (); } Public Static Class RanddoubleGenerator Implements Doublegenerator { Public double next () {return r.nextdouble (); } } ///: ~ In order to populate all elements of an array using the Generator object, the Fill () method uses a suitable Generator interface, which generates a specific type of object in some way (this depends on how this interface is implemented) . The Fill () method is just a simple call next () method until the expected range is filled. Now you can create any Generator by implementing your appropriate interface and use your own Generator by calling the Fill () method. Random Data Generators is very useful for testing, all of which have a series of internal classes (Inner Classes) to implement all basic types of Generator interface, and there is also a String generator to represent Object type. You will see the random string generator (RandStringGenerator) uses a random character generator (RandChargenerator) to populate an array of characters and then this array will be converted into a string. The size of the array is determined by the constructor parameters of the randstringgenerator. If the numbers you need are not very large, RandintGenerator will generate random numbers at 10,000 as a factor by default, but the overloaded constructor allows you to choose a smaller value as a coefficient. table of Contents