1 package org.jdiseq.filter;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.jdom.Element;
7 import org.jdom.Text;
8 import org.apache.commons.lang.builder.ToStringBuilder;
9 import org.apache.commons.lang.builder.HashCodeBuilder;
10 import org.apache.commons.lang.builder.EqualsBuilder;
11
12 /***
13 * Filter is a filter rule defined by the user. These rules might be defined either
14 * by using a gui or through configuration files.
15 * @author Trond Andersen <trondandersen@c2i.net>
16 * @version $Id: Filter.java,v 1.7 2003/08/17 12:05:53 trondandersen Exp $
17 * @since 1.0
18 */
19 public abstract class Filter {
20
21 /*** The actual filter string */
22 private final Element element;
23 /*** Cached toString value */
24 private String toString;
25
26 /***
27 * When a filter rule is defined in XML files it will use this constructor
28 * to initialize the rules. Typically loaded on startup of the application.
29 * @param element jdom configuration
30 * @see FilterPersistence#loadMethodFilter
31 * @see FilterPersistence#loadPackageFilter
32 */
33 Filter(Element element) {
34 this.element = element;
35 }
36
37 /***
38 * Created an undefined filtering mekanism. Typically created from
39 * a GUI interface with a default chose of type filtering defined by
40 * the subclass.
41 * @param elementName describing the xml bound name of the element
42 */
43 protected Filter(String elementName) {
44 element = new Element(elementName, Persister.NAMESPACE);
45 }
46
47 public final FilterType getType() {
48 assert (element != null);
49 /* Should I cache the filter type instead of getting
50 it from the JDOM element ? The FilterType.getEnum
51 method isn't very efficient since it's a string comparision*/
52 return FilterType.getEnum(element.getAttributeValue(getAttributeName()).toString());
53 }
54
55 public void setFiltering(String filtering) {
56 this.toString = null;
57 List content = new ArrayList(1);
58 content.add(new Text(filtering));
59 element.setContent(content);
60 }
61
62 public String getFiltering() {
63 assert (element != null);
64 Text text = (Text) element.getContent().get(0);
65 /* cached ? see getType()*/
66 return text.getTextTrim();
67 }
68
69 /*** {@inheritDoc} */
70 public String toString() {
71 if (toString == null) {
72 toString = new ToStringBuilder(this).
73 append("Filter type", this.getType()).
74 append("filter value", this.getFiltering()).
75 toString();
76 }
77 return toString;
78 }
79
80 /*** {@inheritDoc} */
81 public boolean equals(Object object) {
82 if (object == this) {
83 return true;
84 }
85 if (!(getClass() == object.getClass())) {
86 return false;
87 }
88
89 Filter cmpFilter = (Filter) object;
90
91 return new EqualsBuilder().
92 append(getType(), cmpFilter.getType()).
93 append(getFiltering(), cmpFilter.getFiltering()).isEquals();
94 }
95
96 /*** {@inheritDoc} */
97 public int hashCode() {
98 return new HashCodeBuilder(17, 37).
99 append(getFiltering()).
100 append(getType()).toHashCode();
101 }
102
103 /***
104 * Returns the subclasses attribute name since the super
105 * class isn't aware of them.
106 * @return Name of the subclasses attribute
107 */
108 protected abstract String getAttributeName();
109
110 protected Element getElement() {
111 return this.element;
112 }
113 }
This page was automatically generated by Maven