View Javadoc

1   /*
2    * Copyright 2004-2005 Germinus XXI
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.germinus.easyconf;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  /***
22   * Builds filters from arrays of strings or up to three string paramters
23   * 
24   * @author jferrer
25   */
26  public class Filter {
27  
28      private String[] selectors;
29      private Map variables;
30  
31      // ............. Constructors ................
32      
33      private Filter(String[] selectors) {
34          this.selectors = selectors;
35      }
36      private Filter(Map variables) {
37          this.variables = variables;
38      }
39  
40      
41      // ......... static methods ...........
42      
43      public static Filter by(String first) {
44          return new Filter(new String[]{first});
45      }
46  
47      public static Filter by(String first, String second) {
48          return new Filter(new String[]{first, second});
49      }
50  
51      public static Filter by(String first, String second, String third) {
52          return new Filter(new String[]{first, second, third});
53      }
54      
55      public static Filter by(String[] selectors) {
56          return new Filter(selectors);
57      }
58  
59      public static Filter usingVariables(String var1, String value1) {
60          Map vars = new HashMap();
61          vars.put(var1, value1);
62          return new Filter(vars);
63      }
64      public static Filter usingVariables(String var1, String value1, 
65                                          String var2, String value2) {
66          Map vars = new HashMap();
67          vars.put(var1, value1);
68          vars.put(var2, value2);
69          return new Filter(vars);
70      }
71      public static Filter usingVariables(String var1, String value1, 
72                                          String var2, String value2,
73                                          String var3, String value3) {
74          Map vars = new HashMap();
75          vars.put(var1, value1);
76          vars.put(var2, value2);
77          vars.put(var3, value3);
78          return new Filter(vars);
79      }
80      public static Filter usingVariables(Map vars) {
81          return new Filter(vars);
82      }
83  
84      // .............. instance methods ................
85      
86      public boolean hasVariables() {
87          return (variables != null) && (!variables.isEmpty());
88      }
89  
90      public Filter setVariables(Map newVars) {
91          this.variables = newVars;
92          return this;
93      }
94      public Map getVariables() {
95          return variables;
96      }
97  
98      public String[] getSelectors() {
99          return selectors;
100     }
101     
102     public Filter setSelectors(String[] newSelectors) {
103         this.selectors = newSelectors;
104         return this;
105     }
106     
107     public int numOfSelectors() {
108         if (selectors == null) {
109             return 0;
110         }
111         return selectors.length;
112     }
113 
114     /***
115      * Get a fragment of the filter which includes the first 'n' selectors
116      * concatenated.
117      *
118      * Example: if the filter has two selectors (bar and foo). Fragments would
119      * be:
120      * <ul>
121      *  <li>For n=2: "[bar][foo]"
122      *  <li>For n=1: "[bar]"
123      *  <li>For n=0: ""
124      *  <li>Otherwise: throws IllegalArgumentException
125      * </ul>
126      * @param n
127      * @return
128      * @throws IllegalArgumentException if n < 1 or n > size()
129      */
130     public String getFilterSuffix(int n) {
131         if ((n<0) || (n > numOfSelectors())) {
132             throw new IllegalArgumentException("Imposible to obtain filter fragment for n="+n);
133         }
134         if (n==0) {
135             return "";
136         }
137         StringBuffer filter = new StringBuffer();
138         for (int i = 0; i < n; i++) {
139             filter.append(Conventions.SELECTOR_START);
140             filter.append(selectors[i]);
141             filter.append(Conventions.SELECTOR_END);
142         }
143         return filter.toString();
144     }
145 
146     public String toString() {
147         return getFilterSuffix(numOfSelectors());
148     }
149 
150 
151 }