diff options
author | Scott Jackson <daneren2005@gmail.com> | 2016-02-01 09:11:43 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2016-02-01 09:11:43 -0800 |
commit | 14bf01b785d539e16536f335603852284c0d4c29 (patch) | |
tree | f7eb29def645f9808022af15b0195f0cb2054466 /app/src/main/java/github/daneren2005/dsub/domain | |
parent | 2153ff1bee4c7826ea902b1f1fddf2d2933b2b5b (diff) | |
parent | cb9f7d49ba13890872796c71b16b6f7e9f14625f (diff) | |
download | dsub-14bf01b785d539e16536f335603852284c0d4c29.tar.gz dsub-14bf01b785d539e16536f335603852284c0d4c29.tar.bz2 dsub-14bf01b785d539e16536f335603852284c0d4c29.zip |
Merge branch 'better-offline-search' of https://github.com/fxthomas/Subsonic into fxthomas-better-offline-search
Diffstat (limited to 'app/src/main/java/github/daneren2005/dsub/domain')
-rw-r--r-- | app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java b/app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java index 20d46aa0..12c641f2 100644 --- a/app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java +++ b/app/src/main/java/github/daneren2005/dsub/domain/SearchCritera.java @@ -18,6 +18,8 @@ */ package github.daneren2005.dsub.domain; +import java.util.regex.Pattern; + /** * The criteria for a music search. * @@ -29,6 +31,7 @@ public class SearchCritera { private final int artistCount; private final int albumCount; private final int songCount; + private Pattern pattern; public SearchCritera(String query, int artistCount, int albumCount, int songCount) { this.query = query; @@ -52,4 +55,38 @@ public class SearchCritera { public int getSongCount() { return songCount; } -}
\ No newline at end of file + + /** + * Returns and caches a pattern instance that can be used to check if a + * string matches the query. + */ + public Pattern getPattern() { + + // If the pattern wasn't already cached, create a new regular expression + // from the search string : + // * Surround the search string with ".*" (match anything) + // * Replace spaces and wildcard '*' characters with ".*" + // * All other characters are properly quoted + if (this.pattern == null) { + String regex = ".*"; + String currentPart = ""; + for (int i = 0; i < query.length(); i++) { + char c = query.charAt(i); + if (c == '*' || c == ' ') { + regex += Pattern.quote(currentPart); + regex += ".*"; + currentPart = ""; + } else { + currentPart += c; + } + } + if (currentPart.length() > 0) { + regex += Pattern.quote(currentPart); + } + regex += ".*"; + this.pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); + } + + return this.pattern; + } +} |