1 package net.logAnalyzer.gui.messages.filters;
2
3 import net.logAnalyzer.converters.LAConverter;
4 import net.logAnalyzer.converters.UnknownOrLiteralConverterException;
5 import net.logAnalyzer.handlers.LAMessage;
6 import net.logAnalyzer.handlers.ParsingException;
7
8 /***
9 * This class implements a filter used to compare the value of messageLabel converter
10 * to specified value.
11 *
12 * @author Karim REFEYTON
13 * @version 0.1
14 */
15 public abstract class CompareFilter implements LAMessagesFilter {
16 /***
17 * Converter to compare.
18 */
19 private LAConverter converter;
20
21 /***
22 * Returns the converter to compare.
23 *
24 * @return converter.
25 */
26 public final LAConverter getConverter() {
27 return this.converter;
28 }
29
30 /***
31 * Value used to compare messages values.
32 */
33 private Comparable value;
34
35 /***
36 * Returns the value.
37 *
38 * @return value.
39 */
40 public final Comparable getValue() {
41 return this.value;
42 }
43
44 /***
45 * Constructs a new filter.
46 *
47 * @param converter
48 * Converter to compare.
49 * @param value
50 * Value used to accept or not the messageLabel (depending of
51 * {@link #accept(LAMessage)}implementation).
52 */
53 public CompareFilter(LAConverter converter, Comparable value) {
54 super();
55 this.converter = converter;
56 if (converter != null && value != null) {
57 try {
58 this.value = (Comparable) converter.parse((String) value);
59 } catch (ParsingException e) {
60 this.value = value;
61 }
62 } else {
63 this.value = null;
64 }
65 }
66
67 /***
68 * Index of the converter to compare. <tt>-1</tt> if not computed,
69 * {@link #CONVERTER_NOT_FOUND}if not found.
70 */
71 private int converterIndex = -1;
72
73 /***
74 * Column not found value of {@link #getConverterIndex(LAMessage)}.
75 */
76 protected static final int CONVERTER_NOT_FOUND = -2;
77
78 /***
79 * Returns the converter index to compare.
80 *
81 * @param message
82 * Message to compare.
83 * @return Index of the converter; {@link #CONVERTER_NOT_FOUND}if not
84 * found.
85 */
86 protected final int getConverterIndex(LAMessage message) {
87 if (converterIndex == -1) {
88 try {
89 converterIndex = message.getConverterIndex(converter
90 .getLiteral());
91 } catch (UnknownOrLiteralConverterException uolce) {
92
93 converterIndex = CONVERTER_NOT_FOUND;
94 }
95 }
96 return converterIndex;
97 }
98
99 /***
100 * Returns a string representation of the filter. Used to display filter.
101 *
102 * @return String representation of the filter.
103 */
104 public abstract String toString();
105 }