mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 07:55:47 +01:00
added search api
This commit is contained in:
178
scm-core/src/main/java/sonia/scm/search/SearchRequest.java
Normal file
178
scm-core/src/main/java/sonia/scm/search/SearchRequest.java
Normal file
@@ -0,0 +1,178 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.search;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class SearchRequest
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*/
|
||||
public SearchRequest() {}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param query
|
||||
*/
|
||||
public SearchRequest(String query)
|
||||
{
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param query
|
||||
* @param ignoreCase
|
||||
*/
|
||||
public SearchRequest(String query, boolean ignoreCase)
|
||||
{
|
||||
this.query = query;
|
||||
this.ignoreCase = ignoreCase;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getMaxResults()
|
||||
{
|
||||
return maxResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getQuery()
|
||||
{
|
||||
return query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getStartWith()
|
||||
{
|
||||
return startWith;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isIgnoreCase()
|
||||
{
|
||||
return ignoreCase;
|
||||
}
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param ignoreCase
|
||||
*/
|
||||
public void setIgnoreCase(boolean ignoreCase)
|
||||
{
|
||||
this.ignoreCase = ignoreCase;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param maxResults
|
||||
*/
|
||||
public void setMaxResults(int maxResults)
|
||||
{
|
||||
this.maxResults = maxResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param query
|
||||
*/
|
||||
public void setQuery(String query)
|
||||
{
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param startWith
|
||||
*/
|
||||
public void setStartWith(int startWith)
|
||||
{
|
||||
this.startWith = startWith;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private boolean ignoreCase = false;
|
||||
|
||||
/** Field description */
|
||||
private int maxResults = -1;
|
||||
|
||||
/** Field description */
|
||||
private String query;
|
||||
|
||||
/** Field description */
|
||||
private int startWith = 0;
|
||||
}
|
||||
150
scm-core/src/main/java/sonia/scm/search/SearchUtil.java
Normal file
150
scm-core/src/main/java/sonia/scm/search/SearchUtil.java
Normal file
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.search;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class SearchUtil
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param value
|
||||
* @param other
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static boolean matchesAll(SearchRequest request, String value,
|
||||
String... other)
|
||||
{
|
||||
boolean result = false;
|
||||
String query = createStringQuery(request);
|
||||
|
||||
if (value.matches(query))
|
||||
{
|
||||
result = true;
|
||||
|
||||
if (Util.isNotEmpty(other))
|
||||
{
|
||||
for (String o : other)
|
||||
{
|
||||
if (!o.matches(query))
|
||||
{
|
||||
result = false;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param value
|
||||
* @param other
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static boolean matchesOne(SearchRequest request, String value,
|
||||
String... other)
|
||||
{
|
||||
boolean result = false;
|
||||
String query = createStringQuery(request);
|
||||
|
||||
if (!value.matches(query))
|
||||
{
|
||||
if (Util.isNotEmpty(other))
|
||||
{
|
||||
for (String o : other)
|
||||
{
|
||||
if (o.matches(query))
|
||||
{
|
||||
result = true;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static String createStringQuery(SearchRequest request)
|
||||
{
|
||||
String query = request.getQuery().trim();
|
||||
|
||||
if (request.isIgnoreCase())
|
||||
{
|
||||
query = query.toLowerCase();
|
||||
}
|
||||
|
||||
query = query.replace("*", ".*").replace("?", ".");
|
||||
query = ".*".concat(query).concat(".*");
|
||||
|
||||
if (request.isIgnoreCase())
|
||||
{
|
||||
query = "(?i)".concat(query);
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
58
scm-core/src/main/java/sonia/scm/search/Searchable.java
Normal file
58
scm-core/src/main/java/sonia/scm/search/Searchable.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.search;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public interface Searchable<T>
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param searchRequest
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Collection<T> search(SearchRequest searchRequest);
|
||||
}
|
||||
177
scm-core/src/test/java/sonia/scm/search/SearchUtilTest.java
Normal file
177
scm-core/src/test/java/sonia/scm/search/SearchUtilTest.java
Normal file
@@ -0,0 +1,177 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.search;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class SearchUtilTest
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testMultiMatchesAll()
|
||||
{
|
||||
assertTrue(SearchUtil.matchesAll(new SearchRequest("test"), "test",
|
||||
"test hello", "hello test",
|
||||
"hello test hello"));
|
||||
assertFalse(SearchUtil.matchesAll(new SearchRequest("test"), "test",
|
||||
"test hello", "hello test",
|
||||
"hello test hello", "ka"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testMultiMatchesOne()
|
||||
{
|
||||
assertTrue(SearchUtil.matchesOne(new SearchRequest("test"), "test",
|
||||
"test hello", "hello test",
|
||||
"hello test hello"));
|
||||
assertTrue(SearchUtil.matchesOne(new SearchRequest("test"), "test",
|
||||
"test hello", "hello test",
|
||||
"hello test hello", "ka"));
|
||||
assertTrue(SearchUtil.matchesOne(new SearchRequest("test"), "hans", "uew",
|
||||
"klaus", "hello test hello", "ka"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testSingleMatchesAll()
|
||||
{
|
||||
assertTrue(SearchUtil.matchesAll(new SearchRequest("test"), "test"));
|
||||
assertTrue(SearchUtil.matchesAll(new SearchRequest("test"), "hello test"));
|
||||
assertTrue(SearchUtil.matchesAll(new SearchRequest("test"), "test hello"));
|
||||
assertTrue(SearchUtil.matchesAll(new SearchRequest("test"),
|
||||
"hello test hello"));
|
||||
assertFalse(SearchUtil.matchesAll(new SearchRequest("test"),
|
||||
"hello hello"));
|
||||
assertFalse(SearchUtil.matchesAll(new SearchRequest("test"),
|
||||
"hello te hello"));
|
||||
assertFalse(SearchUtil.matchesAll(new SearchRequest("test"),
|
||||
"hello TEST hello"));
|
||||
assertFalse(SearchUtil.matchesAll(new SearchRequest("test"),
|
||||
"hello TesT hello"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testSingleMatchesAllIgnoreCase()
|
||||
{
|
||||
assertTrue(SearchUtil.matchesAll(new SearchRequest("test", true), "tEsT"));
|
||||
assertTrue(SearchUtil.matchesAll(new SearchRequest("test", true),
|
||||
"heLLo teSt"));
|
||||
assertTrue(SearchUtil.matchesAll(new SearchRequest("test", true),
|
||||
"TEST hellO"));
|
||||
assertTrue(SearchUtil.matchesAll(new SearchRequest("test", true),
|
||||
"hEllO tEsT hEllO"));
|
||||
assertFalse(SearchUtil.matchesAll(new SearchRequest("test", true),
|
||||
"heLLo heLLo"));
|
||||
assertFalse(SearchUtil.matchesAll(new SearchRequest("test", true),
|
||||
"heLLo te heLLo"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testSingleMatchesOne()
|
||||
{
|
||||
assertTrue(SearchUtil.matchesOne(new SearchRequest("test"), "test"));
|
||||
assertTrue(SearchUtil.matchesOne(new SearchRequest("test"), "hello test"));
|
||||
assertTrue(SearchUtil.matchesOne(new SearchRequest("test"), "test hello"));
|
||||
assertTrue(SearchUtil.matchesOne(new SearchRequest("test"),
|
||||
"hello test hello"));
|
||||
assertFalse(SearchUtil.matchesOne(new SearchRequest("test"),
|
||||
"hello hello"));
|
||||
assertFalse(SearchUtil.matchesOne(new SearchRequest("test"),
|
||||
"hello te hello"));
|
||||
assertFalse(SearchUtil.matchesOne(new SearchRequest("test"),
|
||||
"hello TEST hello"));
|
||||
assertFalse(SearchUtil.matchesOne(new SearchRequest("test"),
|
||||
"hello TesT hello"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testSingleMatchesOneIgnoreCase()
|
||||
{
|
||||
assertTrue(SearchUtil.matchesOne(new SearchRequest("test", true), "tEsT"));
|
||||
assertTrue(SearchUtil.matchesOne(new SearchRequest("test", true),
|
||||
"heLLo teSt"));
|
||||
assertTrue(SearchUtil.matchesOne(new SearchRequest("test", true),
|
||||
"TEST hellO"));
|
||||
assertTrue(SearchUtil.matchesOne(new SearchRequest("test", true),
|
||||
"hEllO tEsT hEllO"));
|
||||
assertFalse(SearchUtil.matchesOne(new SearchRequest("test", true),
|
||||
"heLLo heLLo"));
|
||||
assertFalse(SearchUtil.matchesOne(new SearchRequest("test", true),
|
||||
"heLLo te heLLo"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testWildcardMatches()
|
||||
{
|
||||
assertTrue(SearchUtil.matchesAll(new SearchRequest("*test*"),
|
||||
"hello test hello"));
|
||||
assertTrue(SearchUtil.matchesAll(new SearchRequest("?es?"), "test"));
|
||||
assertTrue(SearchUtil.matchesAll(new SearchRequest("t*t"), "test"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user