mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 15:05:44 +01:00
replace filter with predicate
This commit is contained in:
@@ -1,55 +0,0 @@
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Util class for filtering of objects.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public interface Filter<T>
|
||||
{
|
||||
|
||||
/**
|
||||
* Tests whether the item is filtered.
|
||||
*
|
||||
*
|
||||
* @param item for the accept test
|
||||
*
|
||||
* @return true if the object is accepted
|
||||
*/
|
||||
public boolean accept(T item);
|
||||
}
|
||||
@@ -35,7 +35,7 @@ package sonia.scm.cache;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.Filter;
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
/**
|
||||
* The main interface for the cache.
|
||||
@@ -86,16 +86,16 @@ public interface Cache<K, V>
|
||||
public boolean remove(K key);
|
||||
|
||||
/**
|
||||
* Remove all elements with matching {@link Filter} from this cache.
|
||||
* Remove all elements with matching {@link Predicate} from this cache.
|
||||
* The method returns true if the operation was successful.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param filter - The filter to match cache keys
|
||||
* @param predicate - predicate to match cache keys
|
||||
*
|
||||
* @return true if the operation was successful
|
||||
*/
|
||||
public boolean removeAll(Filter<K> filter);
|
||||
public boolean removeAll(Predicate<K> predicate);
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
|
||||
@@ -36,17 +36,14 @@ package sonia.scm.repository;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.github.legman.Subscribe;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.Filter;
|
||||
import sonia.scm.cache.Cache;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
@@ -77,18 +74,18 @@ public class CacheClearHook
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param filter
|
||||
* @param predicate
|
||||
*/
|
||||
public void clearCache(Filter filter)
|
||||
public void clearCache(Predicate predicate)
|
||||
{
|
||||
if (filter != null)
|
||||
if (predicate != null)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("clear cache, with filter");
|
||||
}
|
||||
|
||||
cache.removeAll(filter);
|
||||
cache.removeAll(predicate);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -116,15 +113,9 @@ public class CacheClearHook
|
||||
event.getRepository().getName());
|
||||
}
|
||||
|
||||
Filter filter = createFilter(event);
|
||||
|
||||
clearCache(filter);
|
||||
clearCache(createPredicate(event));
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -134,7 +125,7 @@ public class CacheClearHook
|
||||
* @param event
|
||||
* @return
|
||||
*/
|
||||
protected Filter<?> createFilter(RepositoryHookEvent event)
|
||||
protected Predicate<?> createPredicate(RepositoryHookEvent event)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,9 @@ package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.Filter;
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -54,8 +56,8 @@ public class PartCacheClearHook extends CacheClearHook
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected Filter<?> createFilter(RepositoryHookEvent event)
|
||||
protected Predicate<?> createPredicate(RepositoryHookEvent event)
|
||||
{
|
||||
return new RepositoryFilter(event);
|
||||
return new RepositoryPredicate(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,11 +30,12 @@
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.Filter;
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -43,8 +44,8 @@ import sonia.scm.Filter;
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class RepositoryCacheKeyFilter<T extends RepositoryCacheKey>
|
||||
implements Filter<T>
|
||||
public class RepositoryCacheKeyPredicate<T extends RepositoryCacheKey>
|
||||
implements Predicate<T>
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -53,7 +54,7 @@ public class RepositoryCacheKeyFilter<T extends RepositoryCacheKey>
|
||||
*
|
||||
* @param repositoryId
|
||||
*/
|
||||
public RepositoryCacheKeyFilter(String repositoryId)
|
||||
public RepositoryCacheKeyPredicate(String repositoryId)
|
||||
{
|
||||
this.repositoryId = repositoryId;
|
||||
}
|
||||
@@ -69,7 +70,7 @@ public class RepositoryCacheKeyFilter<T extends RepositoryCacheKey>
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean accept(T item)
|
||||
public boolean apply(T item)
|
||||
{
|
||||
return repositoryId.equals(item.getRepositoryId());
|
||||
}
|
||||
@@ -77,5 +78,5 @@ public class RepositoryCacheKeyFilter<T extends RepositoryCacheKey>
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private String repositoryId;
|
||||
private final String repositoryId;
|
||||
}
|
||||
@@ -35,7 +35,7 @@ package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.Filter;
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -43,7 +43,7 @@ import sonia.scm.Filter;
|
||||
* @since 1.9
|
||||
*
|
||||
*/
|
||||
public class RepositoryFilter implements Filter<RepositoryCacheKey>
|
||||
public class RepositoryPredicate implements Predicate<RepositoryCacheKey>
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -52,7 +52,7 @@ public class RepositoryFilter implements Filter<RepositoryCacheKey>
|
||||
*
|
||||
* @param repository
|
||||
*/
|
||||
public RepositoryFilter(Repository repository)
|
||||
public RepositoryPredicate(Repository repository)
|
||||
{
|
||||
this(repository.getId());
|
||||
}
|
||||
@@ -63,7 +63,7 @@ public class RepositoryFilter implements Filter<RepositoryCacheKey>
|
||||
*
|
||||
* @param event
|
||||
*/
|
||||
public RepositoryFilter(RepositoryHookEvent event)
|
||||
public RepositoryPredicate(RepositoryHookEvent event)
|
||||
{
|
||||
this(event.getRepository());
|
||||
}
|
||||
@@ -74,7 +74,7 @@ public class RepositoryFilter implements Filter<RepositoryCacheKey>
|
||||
*
|
||||
* @param repositoryId
|
||||
*/
|
||||
public RepositoryFilter(String repositoryId)
|
||||
public RepositoryPredicate(String repositoryId)
|
||||
{
|
||||
this.repositoryId = repositoryId;
|
||||
}
|
||||
@@ -90,13 +90,13 @@ public class RepositoryFilter implements Filter<RepositoryCacheKey>
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean accept(RepositoryCacheKey key)
|
||||
public boolean apply(RepositoryCacheKey key)
|
||||
{
|
||||
return repositoryId.equals(key.getRepositoryId());
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private String repositoryId;
|
||||
/** repository id */
|
||||
private final String repositoryId;
|
||||
}
|
||||
@@ -60,7 +60,7 @@ import sonia.scm.repository.PermissionUtil;
|
||||
import sonia.scm.repository.PostReceiveRepositoryHookEvent;
|
||||
import sonia.scm.repository.PreProcessorUtil;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.RepositoryCacheKeyFilter;
|
||||
import sonia.scm.repository.RepositoryCacheKeyPredicate;
|
||||
import sonia.scm.repository.RepositoryEvent;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.repository.RepositoryNotFoundException;
|
||||
@@ -365,8 +365,8 @@ public final class RepositoryServiceFactory
|
||||
logger.debug("clear caches for repository id {}", repositoryId);
|
||||
}
|
||||
|
||||
RepositoryCacheKeyFilter filter =
|
||||
new RepositoryCacheKeyFilter(repositoryId);
|
||||
RepositoryCacheKeyPredicate filter =
|
||||
new RepositoryCacheKeyPredicate(repositoryId);
|
||||
|
||||
blameCache.removeAll(filter);
|
||||
browseCache.removeAll(filter);
|
||||
|
||||
@@ -30,14 +30,14 @@
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cache;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import sonia.scm.Filter;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Map;
|
||||
@@ -113,13 +113,13 @@ public class MapCache<K, V> implements Cache<K, V>
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean removeAll(Filter<K> filter)
|
||||
public boolean removeAll(Predicate<K> filter)
|
||||
{
|
||||
boolean result = false;
|
||||
|
||||
for (K key : map.keySet())
|
||||
{
|
||||
if (filter.accept(key))
|
||||
if (filter.apply(key))
|
||||
{
|
||||
if (remove(key))
|
||||
{
|
||||
@@ -150,5 +150,5 @@ public class MapCache<K, V> implements Cache<K, V>
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private Map<K, V> map = Maps.newHashMap();
|
||||
private final Map<K, V> map = Maps.newHashMap();
|
||||
}
|
||||
|
||||
@@ -35,13 +35,12 @@ package sonia.scm.cache;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.Filter;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.util.Set;
|
||||
@@ -178,13 +177,13 @@ public class GuavaCache<K, V> implements Cache<K, V>
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean removeAll(Filter<K> filter)
|
||||
public boolean removeAll(Predicate<K> filter)
|
||||
{
|
||||
Set<K> keysToRemove = Sets.newHashSet();
|
||||
|
||||
for (K key : cache.asMap().keySet())
|
||||
{
|
||||
if (filter.accept(key))
|
||||
if (filter.apply(key))
|
||||
{
|
||||
keysToRemove.add(key);
|
||||
}
|
||||
|
||||
@@ -34,11 +34,11 @@ package sonia.scm.cache;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import sonia.scm.Filter;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
@@ -142,10 +142,10 @@ public abstract class CacheTestBase
|
||||
cache.put("test-2", "test123");
|
||||
cache.put("a-1", "test123");
|
||||
cache.put("a-2", "test123");
|
||||
cache.removeAll(new Filter<String>()
|
||||
cache.removeAll(new Predicate<String>()
|
||||
{
|
||||
@Override
|
||||
public boolean accept(String item)
|
||||
public boolean apply(String item)
|
||||
{
|
||||
return item.startsWith("test");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user