org.hackystat.sensorbase.uripattern
Class UriPattern

java.lang.Object
  extended by org.hackystat.sensorbase.uripattern.UriPattern
All Implemented Interfaces:
java.lang.Comparable<UriPattern>

public class UriPattern
extends java.lang.Object
implements java.lang.Comparable<UriPattern>

Implements a UriPattern, such as "file://foo/*.java", which can then be matched against a concrete URI string, such as "file://foo/Bar.java". UriPatterns can be "atomic" or "compound".

An atomic UriPattern matches against a single string that can contain wildcard characters like "*", "**", or "?".

A compound UriPattern consists of atomic UriPatterns that are composed together using the "+" and "-" operators. For example, (UriPattern1) + (UriPattern2) means (UriPattern1 OR UriPattern2). (UriPattern1) - (UriPattern2) means (UriPattern1 AND (NOT UriPattern2)). Note that in compound UriPatterns, all atomic patterns must be enclosed in parentheses, and only one level of parentheses is supported.

Note: Matching is case-sensitive, and only the forward slash is supported as a path separator. So, Windows-based sensors must convert their file paths before sending them!

Author:
Philip Johnson (adapted from code originally written for Hackystat 7 by Qin Zhang).

Constructor Summary
UriPattern(java.lang.String pattern)
          Create a UriPattern instance.
 
Method Summary
 int compareTo(UriPattern another)
          Compares two objects.
 boolean equals(java.lang.Object o)
          Tests whether two objects contain the same pattern.
static java.util.List<UriPattern> getPatterns(Project project)
          Returns a List of UriPatterns extracted from the passed Project.
 java.lang.String getRawPattern()
          Returns the 'raw' pattern, which some clients may find a better string representation.
 int hashCode()
          Gets the hash code of this object.
(package private)  boolean isTopLevel()
          Returns true if this UriPattern is top-level.
 boolean matches(java.lang.String path)
          Returns true if the passed path matches this UriPattern.
static boolean matches(java.lang.String resource, java.util.List<UriPattern> uriPatterns)
          Returns true if resource matches any of the UriPatterns.
 java.lang.String toString()
          Gets the string representation of this file path pattern.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UriPattern

public UriPattern(java.lang.String pattern)
Create a UriPattern instance. There are three possible wildcard characters:

Parameters:
pattern - The UriPattern. If null is passed, the pattern defaults to "**".
Method Detail

matches

public static boolean matches(java.lang.String resource,
                              java.util.List<UriPattern> uriPatterns)
Returns true if resource matches any of the UriPatterns.

Parameters:
resource - The resource of interest.
uriPatterns - The list of UriPatterns.
Returns:
True if there is a match.

getPatterns

public static java.util.List<UriPattern> getPatterns(Project project)
Returns a List of UriPatterns extracted from the passed Project.

Parameters:
project - The project containing a list of UriPattern strings.
Returns:
The List of UriPattern instances.

matches

public boolean matches(java.lang.String path)
Returns true if the passed path matches this UriPattern.

Matching is case sensitive.

This implemementation is optimized for contexts in which a high percentage of the UriPatterns in use are "top-level". A "top-level" UriPattern is a UriPattern like "file://hackyCore_Kernel/**", where the only wildcard is a trailing "/**". This implementation tests to see if this UriPattern is a top-level, and if so determines the match without recourse to the underlying Ant-based pattern matching machinery. The overhead of checking for top-level is not high, but the performance advantages of this implementation are significant whenthere are a high number of calls to "matches()" with top-level UriPatterns.

For interesting information on File: URLs, see http://www.cs.tut.fi/~jkorpela/fileurl.html.

Parameters:
path - The path to be tested against this UriPattern.
Returns:
True if it matches, false otherwise.

isTopLevel

boolean isTopLevel()
Returns true if this UriPattern is top-level. Package private because this method exists for testing purposes only.

Returns:
True if the UriPattern is top-level.

compareTo

public int compareTo(UriPattern another)
Compares two objects.

Specified by:
compareTo in interface java.lang.Comparable<UriPattern>
Parameters:
another - The other object.
Returns:
An integer value indicates the relative magnitude of two objects compared.

equals

public boolean equals(java.lang.Object o)
Tests whether two objects contain the same pattern.

Overrides:
equals in class java.lang.Object
Parameters:
o - The other object.
Returns:
True if they are equal.

hashCode

public int hashCode()
Gets the hash code of this object.

Overrides:
hashCode in class java.lang.Object
Returns:
The hash code.

getRawPattern

public java.lang.String getRawPattern()
Returns the 'raw' pattern, which some clients may find a better string representation.

Returns:
The 'raw' pattern as a string.

toString

public java.lang.String toString()
Gets the string representation of this file path pattern.

Overrides:
toString in class java.lang.Object
Returns:
The string representation.