1 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 ; 24 import java.io.Serializable ; 25 26 33 public class LevelImpl implements Level, Serializable { 34 int value = 0; 35 String name = null;; 36 String stringValue = null; 37 38 public LevelImpl(String n, int val) { 39 value = val; 40 name = n; 41 stringValue = String.valueOf(val); 42 } 43 44 public LevelImpl(String n, String val, LevelFactory lf) { 45 stringValue = val; 47 value = evaluate(val, lf); 48 name = n; 49 } 50 51 54 public int hashCode() { 55 return value; 56 } 57 58 61 public String getStringValue() { 62 return stringValue; 63 } 64 65 79 public static int evaluate(String expr, LevelFactory lf) { 80 int firstOperande = 0; 81 int secondOperande = 0; 82 byte operator = 0; for ( 84 StringTokenizer st = new StringTokenizer (expr, " +-", true); 85 st.hasMoreTokens();) { 86 87 String elem = st.nextToken(); 88 if (Character.isDigit(elem.charAt(0))) { 89 try { 90 secondOperande = Integer.parseInt(elem); 91 } catch (NumberFormatException e) { 92 continue; 93 } 94 } else if (Character.isLetter(elem.charAt(0))) { 95 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 toString() { 132 return "(name=" + name + ", val=" + value + ", sval=" + stringValue + ")"; 133 } 134 135 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 getName() { 151 return name; 152 } 153 154 public void setName(String n) { 155 name = n; 156 } 157 } 158 | Popular Tags |