KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > common > LevelImpl


1 /**
2  * Copyright (C) 2001-2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.util.monolog.wrapper.common ;
19
20 import org.objectweb.util.monolog.api.Level;
21 import org.objectweb.util.monolog.api.LevelFactory;
22
23 import java.util.StringTokenizer JavaDoc;
24 import java.io.Serializable JavaDoc;
25
26 /**
27  * This class is the basic implementation of the Level interface. It proviedes
28  * also a static method 'evaluate' which permits to obtain the integer value
29  * of a level expression.
30  *
31  * @author Sebastien Chassande-Barrioz
32  */

33 public class LevelImpl implements Level, Serializable JavaDoc {
34     int value = 0;
35     String JavaDoc name = null;;
36     String JavaDoc stringValue = null;
37
38     public LevelImpl(String JavaDoc n, int val) {
39         value = val;
40         name = n;
41         stringValue = String.valueOf(val);
42     }
43
44     public LevelImpl(String JavaDoc n, String JavaDoc val, LevelFactory lf) {
45         //System.out.println("LevelImpl("+n+", "+val+", lf)");
46
stringValue = val;
47         value = evaluate(val, lf);
48         name = n;
49     }
50
51     /**
52      * It retrieves the integer value of the level.
53      */

54     public int hashCode() {
55         return value;
56     }
57
58     /**
59      * It retrieves the string expression of the level. ex: 'DEBUG + 1'
60      */

61     public String JavaDoc getStringValue() {
62         return stringValue;
63     }
64
65     /**
66      * It analyzes a string expression to obtain its integer value. The allowed
67      * expression type are the following:
68      * <ul>
69      * <li>an integer value</li>
70      * <li>another level name</li>
71      * <li>levelName + integerValue</li>
72      * <li>levelName - integerValue</li>
73      * </ul>
74      * @param expr is the string expression which must be evaluated.
75      * @param lf is the LevelFactory which permits to obtain the referenced level.
76      * @return an integer value or 0 if it is impossible to evaluate the
77      * expression.
78      */

79     public static int evaluate(String JavaDoc expr, LevelFactory lf) {
80         int firstOperande = 0;
81         int secondOperande = 0;
82         byte operator = 0; // 1=>+ 2=>-
83
for (
84             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(expr, " +-", true);
85             st.hasMoreTokens();) {
86
87             String JavaDoc elem = st.nextToken();
88             if (Character.isDigit(elem.charAt(0))) {
89                 try {
90                     secondOperande = Integer.parseInt(elem);
91                 } catch (NumberFormatException JavaDoc e) {
92                     continue;
93                 }
94             } else if (Character.isLetter(elem.charAt(0))) {
95                 // The token is another intermediate level
96
Level l = lf.getLevel(elem);
97                 if (l == null) {
98                     l = lf.getLevel(elem.toUpperCase());
99                     if (l == null) {
100                         return 0;
101                     }
102                 }
103                 firstOperande = l.getIntValue();
104             } else if (elem.charAt(0) == '+') {
105                 operator = 1;
106             } else if (elem.charAt(0) == '-') {
107                 operator = 2;
108             } else if (Character.isSpaceChar(elem.charAt(0))) {
109                 continue;
110             }
111         }
112         if (firstOperande != 0) {
113             if (secondOperande != 0 && operator != 0) {
114                 switch (operator) {
115                 case 1:
116                     return firstOperande + secondOperande;
117                 case 2:
118                     return firstOperande - secondOperande;
119                 }
120             }
121             else {
122                 return firstOperande;
123             }
124         }
125         else if (secondOperande != 0) {
126             return secondOperande;
127         }
128         return 0;
129     }
130
131     public String JavaDoc toString() {
132         return "(name=" + name + ", val=" + value + ", sval=" + stringValue + ")";
133     }
134
135     // IMPLEMENTATION OF THE Level INTERFACE //
136
//---------------------------------------//
137

138     public boolean isComparableWith(Level o) {
139         return o instanceof LevelImpl;
140     }
141     
142     public int compareTo(Level o) {
143         return value - o.getIntValue();
144     }
145
146     public int getIntValue() {
147         return value;
148     }
149
150     public String JavaDoc getName() {
151         return name;
152     }
153
154     public void setName(String JavaDoc n) {
155         name = n;
156     }
157 }
158
Popular Tags