KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > Sort


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package examples;
18
19 import org.apache.log4j.PropertyConfigurator;
20 import org.apache.log4j.Logger;
21 import org.apache.log4j.Priority;
22
23 /**
24    Example code for log4j to viewed in conjunction with the {@link
25    examples.SortAlgo SortAlgo} class.
26    
27    <p>This program expects a configuration file name as its first
28    argument, and the size of the array to sort as the second and last
29    argument. See its <b><a HREF="doc-files/Sort.java">source
30    code</a></b> for more details.
31
32    <p>Play around with different values in the configuration file and
33    watch the changing behavior.
34
35    <p>Example configuration files can be found in <a
36    href="doc-files/sort1.properties">sort1.properties</a>, <a
37    href="doc-files/sort2.properties">sort2.properties</a>, <a
38    href="doc-files/sort3.properties">sort3.properties</a> and <a
39    href="doc-files/sort4.properties">sort4.properties</a> are supplied with the
40    package.
41    
42    <p>If you are also interested in logging performance, then have
43    look at the {@link org.apache.log4j.performance.Logging} class.
44
45    @author Ceki G&uuml;lc&uuml; */

46
47 public class Sort {
48
49   static Logger logger = Logger.getLogger(Sort.class.getName());
50   
51   public static void main(String JavaDoc[] args) {
52     if(args.length != 2) {
53       usage("Incorrect number of parameters.");
54     }
55     int arraySize = -1;
56     try {
57       arraySize = Integer.valueOf(args[1]).intValue();
58       if(arraySize <= 0)
59     usage("Negative array size.");
60     }
61     catch(java.lang.NumberFormatException JavaDoc e) {
62       usage("Could not number format ["+args[1]+"].");
63     }
64
65     PropertyConfigurator.configure(args[0]);
66
67     int[] intArray = new int[arraySize];
68
69     logger.info("Populating an array of " + arraySize + " elements in" +
70          " reverse order.");
71     for(int i = arraySize -1 ; i >= 0; i--) {
72       intArray[i] = arraySize - i - 1;
73     }
74
75     SortAlgo sa1 = new SortAlgo(intArray);
76     sa1.bubbleSort();
77     sa1.dump();
78
79     // We intentionally initilize sa2 with null.
80
SortAlgo sa2 = new SortAlgo(null);
81     logger.info("The next log statement should be an error message.");
82     sa2.dump();
83     logger.info("Exiting main method.");
84   }
85   
86   static
87   void usage(String JavaDoc errMsg) {
88     System.err.println(errMsg);
89     System.err.println("\nUsage: java org.apache.examples.Sort " +
90                "configFile ARRAY_SIZE\n"+
91       "where configFile is a configuration file\n"+
92       " ARRAY_SIZE is a positive integer.\n");
93     System.exit(1);
94   }
95 }
96
Popular Tags