Methods Summary |
---|
public java.util.List | getTorrents(java.util.List torrentsToMatch, java.lang.String filter)tries our two different matching algorithms using the
supplied filter against the list of torrents in the ConsoleInput object
List torrents = new ArrayList();
torrents.addAll(matchRange(torrentsToMatch, filter) );
torrents.addAll(matchWildcard(torrentsToMatch, filter) );
return torrents;
|
public java.util.List | getTorrents(java.util.List torrentsToMatch, java.util.List args)first tries to match torrents by concatenating all of the arguments.
if that doesn't work, attempts to match each argument individually.
// first, try to match the whole list concatenated as a string
StringBuffer allArgs = new StringBuffer();
boolean first = true;
for (Iterator iter = args.iterator(); iter.hasNext();) {
if( ! first )
allArgs.append(",");
else
first = false;
allArgs.append(iter.next());
}
List torrents;
torrents = matchWildcard(torrentsToMatch, allArgs.toString());
if( torrents.size() > 0 )
return torrents;
torrents = matchRange(torrentsToMatch, allArgs.toString());
if( torrents.size() > 0 )
return torrents;
// if no torrents then handle each argument individually
for (Iterator iter = args.iterator(); iter.hasNext();) {
torrents.addAll(getTorrents(torrentsToMatch, (String)iter.next()) );
}
return torrents;
|
private java.util.List | matchRange(java.util.List torrents, java.lang.String filter)matches a range of torrents. eg: 3-5 or a single torrent. eg: 3. or from 3 onwards: 3-
Matcher matcher = rangePattern.matcher(filter);
List list = new ArrayList();
if( matcher.matches() )
{
int minId = Integer.parseInt(matcher.group(1));
if( minId == 0 )
throw new AzureusCoreException("lower range must be greater than 0");
if( minId > torrents.size() )
throw new AzureusCoreException("lower range specified (" + minId + ") is outside number of torrents (" + torrents.size() + ")");
if( matcher.group(2) == null )
{
// received a single number. eg: 3
list.add(torrents.get(minId-1));
return list;
}
int maxId;
if( matcher.group(3) == null )
// received bound range. eg: 3-5
maxId = Integer.parseInt(matcher.group(5));
else
// received open ended range. eg: 3-
maxId = torrents.size();
if( minId > maxId )
throw new AzureusCoreException("when specifying a range, the max value must be greater than or equal to the min value");
for( int i = (minId-1) ; i < maxId && i < torrents.size() ; i++ )
{
list.add(torrents.get(i));
}
}
return list;
|
private java.util.List | matchWildcard(java.util.List torrents, java.lang.String filter)attempst to match a wildcard against the list of torrents by
checking their display name
Pattern pattern = Pattern.compile(wildcardToPattern(filter), Pattern.CASE_INSENSITIVE);
List list = new ArrayList();
for (Iterator iter = torrents.iterator(); iter.hasNext();) {
DownloadManager dm = (DownloadManager) iter.next();
if( pattern.matcher(dm.getDisplayName()).matches() )
list.add(dm);
}
return list;
|
private java.lang.String | wildcardToPattern(java.lang.String wild)converts the wildcard (eg: tran*) into a regular expression - (tran.*)
if (wild == null)
return null;
StringBuffer buffer = new StringBuffer();
char[] chars = wild.toCharArray();
for (int i = 0; i < chars.length; ++i) {
if (chars[i] == '*")
buffer.append(".*");
else if (chars[i] == '?")
buffer.append(".");
else if ("+()^$.{}[]|\\".indexOf(chars[i]) != -1)
buffer.append('\\").append(chars[i]); // prefix all metacharacters
// with backslash
else
buffer.append(chars[i]);
}
return buffer.toString().toLowerCase();
|