View Javadoc

1   package com.atlassian.sal.core.search.parameter;
2   
3   import org.apache.commons.httpclient.URIException;
4   import org.apache.commons.httpclient.util.URIUtil;
5   import org.apache.commons.lang.StringUtils;
6   import com.atlassian.sal.api.search.parameter.SearchParameter;
7   
8   /**
9    * Basic name value pair search parameter.
10   */
11  public class BasicSearchParameter implements SearchParameter
12  {
13      private String name;
14      private String value;
15  
16      public BasicSearchParameter(String queryString)
17      {
18          initFromQueryString(queryString);
19      }
20  
21      public BasicSearchParameter(String name, String value)
22      {
23          this.name = name;
24          this.value = value;
25      }
26  
27      public String getName()
28      {
29          return name;
30      }
31  
32      public String getValue()
33      {
34          return value;
35      }
36  
37      public String buildQueryString()
38      {
39          try
40          {
41              return URIUtil.encodeWithinQuery(name) + "=" + URIUtil.encodeWithinQuery(value);
42          }
43          catch (URIException e)
44          {
45              throw new RuntimeException(e);
46          }
47      }
48  
49      private void initFromQueryString(String queryString)
50      {
51          if (StringUtils.isEmpty(queryString) || queryString.indexOf("=") == -1)
52          {
53              throw new IllegalArgumentException("QueryString '" + queryString + "' does not appear to be a valid query string");
54          }
55  
56          final String[] strings;
57          try
58          {
59              strings = URIUtil.decode(queryString).split("=");
60          }
61          catch (URIException e)
62          {
63              throw new RuntimeException(e);
64          }
65          this.name = strings[0];
66          this.value = strings[1];
67      }
68  
69      public boolean equals(Object o)
70      {
71          if (this == o)
72          {
73              return true;
74          }
75          if (o == null || getClass() != o.getClass())
76          {
77              return false;
78          }
79  
80          BasicSearchParameter that = (BasicSearchParameter) o;
81  
82          if (!name.equals(that.name))
83          {
84              return false;
85          }
86          if (!value.equals(that.value))
87          {
88              return false;
89          }
90  
91          return true;
92      }
93  
94      public int hashCode()
95      {
96          int result;
97          result = name.hashCode();
98          result = 31 * result + value.hashCode();
99          return result;
100     }
101 }