4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SubscribedSource.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz
4  * File: SubscribedSource.java
5  * Description: Class representing subscribed source of annotations
6  */
7 
8 /**
9  * @file SubscribedSource.java
10  *
11  * @brief Subscribed source of annotations
12  */
13 
14 package cz.vutbr.fit.knot.annotations.comet;
15 
16 /**
17  * Class representing subscribed source of annotations
18  *
19  * @brief Subscribed source of annotations
20  * @author idytrych
21  */
22 public class SubscribedSource {
23  /** Filter for annotation types */
24  private String type = null;
25  /** Filter for user (must be exact matching) */
26  private String user = null;
27  /** Filter for URI (must be exact matching) */
28  private String uri = null;
29 
30  /**
31  * Constructor
32  *
33  * @param type Filter for annotation types
34  * @param user URI of user (author of annotation)
35  * @param uri URI of source of annotation
36  */
37  public SubscribedSource (String type, String user, String uri) {
38  if (type != null && type.length() > 0) {
39  this.type = type;
40  }
41  if (user != null && user.length() > 0) {
42  this.user = user;
43  }
44  if (uri != null && uri.length() > 0) {
45  this.uri = uri;
46  }
47  }
48 
49  /**
50  * Gets filter for annotation types
51  *
52  * @return Returns filter for annotation types
53  */
54  public String getType() {
55  return type;
56  }
57 
58  /**
59  * Gets URI of user (author of annotation)
60  *
61  * @return Returns URI of user (author of annotation)
62  */
63  public String getUri() {
64  return uri;
65  }
66 
67  /**
68  * Gets URI of source of annotations
69  *
70  * @return Returns URI of source of annotations
71  */
72  public String getUser() {
73  return user;
74  }
75 
76  /**
77  * Converts string with wildcards (only usable wildcard is "*") to regular
78  * expression
79  *
80  * @param wildStr String with wildcards (only usable wildcard is "*")
81  * @return Regular expression
82  */
83  public static String wildcardToRegex(String wildStr){
84  StringBuilder re = new StringBuilder(wildStr.length());
85  int wL = wildStr.length();
86  re.append('^');
87  for (int i = 0; i < wL; i++) {
88  char c = wildStr.charAt(i);
89  switch(c) {
90  case '*':
91  re.append(".*");
92  break;
93  case '(': case ')': case '[': case ']': case '{': case '}':
94  case '^': case '$': case '.': case '+': case '|': case '\\':
95  case '?':
96  re.append('\\');
97  re.append(c);
98  break;
99  default:
100  re.append(c);
101  break;
102  }
103  }
104  re.append('$');
105  return(re.toString());
106  }
107 
108  /**
109  * Checks whether annotation with given type, user and URI matches this
110  * subscription
111  *
112  * @param type Annotation type
113  * @param user Author of annotation
114  * @param uri URI of source of annotation
115  * @return If informations matches this subscription, returns true, false otherwise
116  */
117  public boolean matches(String type, String user, String uri) {
118  if (this.type != null)
119  if (!type.matches(wildcardToRegex(this.type)))
120  return false;
121  if (this.user != null)
122  if (!this.user.equals(user))
123  return false;
124  if (this.uri != null)
125  if (!this.uri.equals(uri) && uri != null)
126  return false;
127  return true;
128  }
129 
130  /**
131  * Compares this with other object and returns, whether objects are same type
132  * and have same content (id is irelevant).
133  *
134  * @param obj Object to compare with
135  * @return If object is same type and have same content, returns true, false otherwise
136  */
137  @Override
138  public boolean equals(Object obj) {
139  if (this == obj) return true;
140  if (!(obj instanceof SubscribedSource)) return false;
141  SubscribedSource su = (SubscribedSource)obj;
142  if (su.getType() != null && this.type != null)
143  if (!su.getType().equals(this.type))
144  return false;
145  if (su.getUser() != null && this.user != null)
146  if (!su.getUser().equals(this.user))
147  return false;
148  if (su.getUri() != null && this.uri != null)
149  if (!su.getUri().equals(this.uri))
150  return false;
151  if ((su.getType() != null) && (this.type == null))
152  return false;
153  if ((su.getUser() != null) && (this.user == null))
154  return false;
155  if ((su.getUri() != null) && (this.uri == null))
156  return false;
157  if ((su.getType() == null) && (this.type != null))
158  return false;
159  if ((su.getUser() == null) && (this.user != null))
160  return false;
161  if ((su.getUri() == null) && (this.uri != null))
162  return false;
163  return true;
164  }
165 
166  /**
167  * Returns hash code of this object. Hash is based on type, user and uri attribute.
168  *
169  * @return Hash code
170  */
171  @Override
172  public int hashCode() {
173  int hash = 7;
174  hash = 47 * hash + (this.type != null ? this.type.hashCode() : 0);
175  hash = 47 * hash + (this.user != null ? this.user.hashCode() : 0);
176  hash = 47 * hash + (this.uri != null ? this.uri.hashCode() : 0);
177  return hash;
178  }
179 
180 } // class SubscribedSource
boolean matches(String type, String user, String uri)
SubscribedSource(String type, String user, String uri)