replace filter with predicate

This commit is contained in:
Sebastian Sdorra
2014-01-09 20:16:03 +01:00
parent 908f2fe6c8
commit 4f6460eeae
10 changed files with 48 additions and 110 deletions

View File

@@ -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);
}

View File

@@ -35,7 +35,7 @@ package sonia.scm.cache;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import sonia.scm.Filter; import com.google.common.base.Predicate;
/** /**
* The main interface for the cache. * The main interface for the cache.
@@ -86,16 +86,16 @@ public interface Cache<K, V>
public boolean remove(K key); 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. * The method returns true if the operation was successful.
* *
* @since 1.9 * @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 * @return true if the operation was successful
*/ */
public boolean removeAll(Filter<K> filter); public boolean removeAll(Predicate<K> predicate);
//~--- get methods ---------------------------------------------------------- //~--- get methods ----------------------------------------------------------

View File

@@ -36,17 +36,14 @@ package sonia.scm.repository;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import com.github.legman.Subscribe; import com.github.legman.Subscribe;
import com.google.common.base.Predicate;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import sonia.scm.Filter;
import sonia.scm.cache.Cache; import sonia.scm.cache.Cache;
//~--- JDK imports ------------------------------------------------------------
import java.util.Arrays;
import java.util.Collection;
/** /**
* *
* @author Sebastian Sdorra * @author Sebastian Sdorra
@@ -77,18 +74,18 @@ public class CacheClearHook
* *
* @since 1.9 * @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()) if (logger.isDebugEnabled())
{ {
logger.debug("clear cache, with filter"); logger.debug("clear cache, with filter");
} }
cache.removeAll(filter); cache.removeAll(predicate);
} }
else else
{ {
@@ -113,18 +110,12 @@ public class CacheClearHook
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
{ {
logger.debug("clear cache because repository {} has changed", logger.debug("clear cache because repository {} has changed",
event.getRepository().getName()); event.getRepository().getName());
} }
Filter filter = createFilter(event); clearCache(createPredicate(event));
clearCache(filter);
} }
//~--- get methods ----------------------------------------------------------
//~--- methods --------------------------------------------------------------
/** /**
* Method description * Method description
* *
@@ -134,7 +125,7 @@ public class CacheClearHook
* @param event * @param event
* @return * @return
*/ */
protected Filter<?> createFilter(RepositoryHookEvent event) protected Predicate<?> createPredicate(RepositoryHookEvent event)
{ {
return null; return null;
} }

View File

@@ -35,7 +35,9 @@ package sonia.scm.repository;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import sonia.scm.Filter; import com.google.common.base.Predicate;
/** /**
* *
@@ -54,8 +56,8 @@ public class PartCacheClearHook extends CacheClearHook
* @return * @return
*/ */
@Override @Override
protected Filter<?> createFilter(RepositoryHookEvent event) protected Predicate<?> createPredicate(RepositoryHookEvent event)
{ {
return new RepositoryFilter(event); return new RepositoryPredicate(event);
} }
} }

View File

@@ -30,11 +30,12 @@
*/ */
package sonia.scm.repository; package sonia.scm.repository;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import sonia.scm.Filter; import com.google.common.base.Predicate;
/** /**
* *
@@ -43,8 +44,8 @@ import sonia.scm.Filter;
* *
* @param <T> * @param <T>
*/ */
public class RepositoryCacheKeyFilter<T extends RepositoryCacheKey> public class RepositoryCacheKeyPredicate<T extends RepositoryCacheKey>
implements Filter<T> implements Predicate<T>
{ {
/** /**
@@ -53,7 +54,7 @@ public class RepositoryCacheKeyFilter<T extends RepositoryCacheKey>
* *
* @param repositoryId * @param repositoryId
*/ */
public RepositoryCacheKeyFilter(String repositoryId) public RepositoryCacheKeyPredicate(String repositoryId)
{ {
this.repositoryId = repositoryId; this.repositoryId = repositoryId;
} }
@@ -69,7 +70,7 @@ public class RepositoryCacheKeyFilter<T extends RepositoryCacheKey>
* @return * @return
*/ */
@Override @Override
public boolean accept(T item) public boolean apply(T item)
{ {
return repositoryId.equals(item.getRepositoryId()); return repositoryId.equals(item.getRepositoryId());
} }
@@ -77,5 +78,5 @@ public class RepositoryCacheKeyFilter<T extends RepositoryCacheKey>
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------
/** Field description */ /** Field description */
private String repositoryId; private final String repositoryId;
} }

View File

@@ -35,7 +35,7 @@ package sonia.scm.repository;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import sonia.scm.Filter; import com.google.common.base.Predicate;
/** /**
* *
@@ -43,7 +43,7 @@ import sonia.scm.Filter;
* @since 1.9 * @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 * @param repository
*/ */
public RepositoryFilter(Repository repository) public RepositoryPredicate(Repository repository)
{ {
this(repository.getId()); this(repository.getId());
} }
@@ -63,7 +63,7 @@ public class RepositoryFilter implements Filter<RepositoryCacheKey>
* *
* @param event * @param event
*/ */
public RepositoryFilter(RepositoryHookEvent event) public RepositoryPredicate(RepositoryHookEvent event)
{ {
this(event.getRepository()); this(event.getRepository());
} }
@@ -74,7 +74,7 @@ public class RepositoryFilter implements Filter<RepositoryCacheKey>
* *
* @param repositoryId * @param repositoryId
*/ */
public RepositoryFilter(String repositoryId) public RepositoryPredicate(String repositoryId)
{ {
this.repositoryId = repositoryId; this.repositoryId = repositoryId;
} }
@@ -90,13 +90,13 @@ public class RepositoryFilter implements Filter<RepositoryCacheKey>
* @return * @return
*/ */
@Override @Override
public boolean accept(RepositoryCacheKey key) public boolean apply(RepositoryCacheKey key)
{ {
return repositoryId.equals(key.getRepositoryId()); return repositoryId.equals(key.getRepositoryId());
} }
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------
/** Field description */ /** repository id */
private String repositoryId; private final String repositoryId;
} }

View File

@@ -60,7 +60,7 @@ import sonia.scm.repository.PermissionUtil;
import sonia.scm.repository.PostReceiveRepositoryHookEvent; import sonia.scm.repository.PostReceiveRepositoryHookEvent;
import sonia.scm.repository.PreProcessorUtil; import sonia.scm.repository.PreProcessorUtil;
import sonia.scm.repository.Repository; import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryCacheKeyFilter; import sonia.scm.repository.RepositoryCacheKeyPredicate;
import sonia.scm.repository.RepositoryEvent; import sonia.scm.repository.RepositoryEvent;
import sonia.scm.repository.RepositoryManager; import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryNotFoundException; import sonia.scm.repository.RepositoryNotFoundException;
@@ -365,8 +365,8 @@ public final class RepositoryServiceFactory
logger.debug("clear caches for repository id {}", repositoryId); logger.debug("clear caches for repository id {}", repositoryId);
} }
RepositoryCacheKeyFilter filter = RepositoryCacheKeyPredicate filter =
new RepositoryCacheKeyFilter(repositoryId); new RepositoryCacheKeyPredicate(repositoryId);
blameCache.removeAll(filter); blameCache.removeAll(filter);
browseCache.removeAll(filter); browseCache.removeAll(filter);

View File

@@ -30,14 +30,14 @@
*/ */
package sonia.scm.cache; package sonia.scm.cache;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Predicate;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import sonia.scm.Filter;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import java.util.Map; import java.util.Map;
@@ -113,13 +113,13 @@ public class MapCache<K, V> implements Cache<K, V>
* @return * @return
*/ */
@Override @Override
public boolean removeAll(Filter<K> filter) public boolean removeAll(Predicate<K> filter)
{ {
boolean result = false; boolean result = false;
for (K key : map.keySet()) for (K key : map.keySet())
{ {
if (filter.accept(key)) if (filter.apply(key))
{ {
if (remove(key)) if (remove(key))
{ {
@@ -150,5 +150,5 @@ public class MapCache<K, V> implements Cache<K, V>
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------
/** Field description */ /** Field description */
private Map<K, V> map = Maps.newHashMap(); private final Map<K, V> map = Maps.newHashMap();
} }

View File

@@ -35,13 +35,12 @@ package sonia.scm.cache;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import sonia.scm.Filter;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import java.util.Set; import java.util.Set;
@@ -178,13 +177,13 @@ public class GuavaCache<K, V> implements Cache<K, V>
* @return * @return
*/ */
@Override @Override
public boolean removeAll(Filter<K> filter) public boolean removeAll(Predicate<K> filter)
{ {
Set<K> keysToRemove = Sets.newHashSet(); Set<K> keysToRemove = Sets.newHashSet();
for (K key : cache.asMap().keySet()) for (K key : cache.asMap().keySet())
{ {
if (filter.accept(key)) if (filter.apply(key))
{ {
keysToRemove.add(key); keysToRemove.add(key);
} }

View File

@@ -34,11 +34,11 @@ package sonia.scm.cache;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Predicate;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import sonia.scm.Filter;
import sonia.scm.util.IOUtil; import sonia.scm.util.IOUtil;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@@ -142,10 +142,10 @@ public abstract class CacheTestBase
cache.put("test-2", "test123"); cache.put("test-2", "test123");
cache.put("a-1", "test123"); cache.put("a-1", "test123");
cache.put("a-2", "test123"); cache.put("a-2", "test123");
cache.removeAll(new Filter<String>() cache.removeAll(new Predicate<String>()
{ {
@Override @Override
public boolean accept(String item) public boolean apply(String item)
{ {
return item.startsWith("test"); return item.startsWith("test");
} }