1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package examples;
19 
20 import org.apache.log4j.Logger;
21 import org.apache.log4j.NDC;
22 
23 /**
24    Example code for log4j to viewed in conjunction with the {@link
25    examples.Sort Sort} class.
26 
27    <p>SortAlgo uses the bubble sort algorithm to sort an integer
28    array. See also its <b><a href="doc-files/SortAlgo.java">source
29    code</a></b>.
30 
31    @author Ceki G&uuml;lc&uuml; */
32 public class SortAlgo {
33 
34   final static String className = SortAlgo.class.getName();
35   final static Logger LOG = Logger.getLogger(className);
36   final static Logger OUTER = Logger.getLogger(className + ".OUTER");
37   final static Logger INNER = Logger.getLogger(className + ".INNER");
38   final static Logger DUMP = Logger.getLogger(className + ".DUMP");
39   final static Logger SWAP = Logger.getLogger(className + ".SWAP");
40 
41   int[] intArray;
42 
SortAlgo(int[] intArray)43   SortAlgo(int[] intArray) {
44     this.intArray = intArray;
45   }
46 
bubbleSort()47   void bubbleSort() {
48     LOG.info( "Entered the sort method.");
49 
50     for(int i = intArray.length -1; i >= 0  ; i--) {
51       NDC.push("i=" + i);
52       OUTER.debug("in outer loop.");
53       for(int j = 0; j < i; j++) {
54 	NDC.push("j=" + j);
55 	// It is poor practice to ship code with log staments in tight loops.
56 	// We do it anyway in this example.
57 	INNER.debug( "in inner loop.");
58          if(intArray[j] > intArray[j+1])
59 	   swap(j, j+1);
60 	NDC.pop();
61       }
62       NDC.pop();
63     }
64   }
65 
dump()66   void dump() {
67     if(! (this.intArray instanceof int[])) {
68       DUMP.error("Tried to dump an uninitialized array.");
69       return;
70     }
71     DUMP.info("Dump of integer array:");
72     for(int i = 0; i < this.intArray.length; i++) {
73       DUMP.info("Element [" + i + "]=" + this.intArray[i]);
74     }
75   }
76 
swap(int l, int r)77   void swap(int l, int r) {
78     // It is poor practice to ship code with log staments in tight
79     // loops or code called potentially millions of times.
80     SWAP.debug( "Swapping intArray["+l+"]=" + intArray[l] +
81 	                     " and intArray["+r+"]=" + intArray[r]);
82     int temp = this.intArray[l];
83     this.intArray[l] = this.intArray[r];
84     this.intArray[r] = temp;
85   }
86 }
87 
88