From 4f6460eeae7541b53ab9a1f6c656810bdea827de Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Thu, 9 Jan 2014 20:16:03 +0100 Subject: [PATCH] replace filter with predicate --- scm-core/src/main/java/sonia/scm/Filter.java | 55 ------------------- .../src/main/java/sonia/scm/cache/Cache.java | 8 +-- .../sonia/scm/repository/CacheClearHook.java | 29 ++++------ .../scm/repository/PartCacheClearHook.java | 8 ++- ....java => RepositoryCacheKeyPredicate.java} | 13 +++-- ...ryFilter.java => RepositoryPredicate.java} | 16 +++--- .../api/RepositoryServiceFactory.java | 6 +- .../main/java/sonia/scm/cache/MapCache.java | 10 ++-- .../main/java/sonia/scm/cache/GuavaCache.java | 7 +-- .../java/sonia/scm/cache/CacheTestBase.java | 6 +- 10 files changed, 48 insertions(+), 110 deletions(-) delete mode 100644 scm-core/src/main/java/sonia/scm/Filter.java rename scm-core/src/main/java/sonia/scm/repository/{RepositoryCacheKeyFilter.java => RepositoryCacheKeyPredicate.java} (89%) rename scm-core/src/main/java/sonia/scm/repository/{RepositoryFilter.java => RepositoryPredicate.java} (86%) diff --git a/scm-core/src/main/java/sonia/scm/Filter.java b/scm-core/src/main/java/sonia/scm/Filter.java deleted file mode 100644 index 0121096dd1..0000000000 --- a/scm-core/src/main/java/sonia/scm/Filter.java +++ /dev/null @@ -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 - */ -public interface Filter -{ - - /** - * Tests whether the item is filtered. - * - * - * @param item for the accept test - * - * @return true if the object is accepted - */ - public boolean accept(T item); -} diff --git a/scm-core/src/main/java/sonia/scm/cache/Cache.java b/scm-core/src/main/java/sonia/scm/cache/Cache.java index 319cadcde1..dd064691c6 100644 --- a/scm-core/src/main/java/sonia/scm/cache/Cache.java +++ b/scm-core/src/main/java/sonia/scm/cache/Cache.java @@ -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 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 filter); + public boolean removeAll(Predicate predicate); //~--- get methods ---------------------------------------------------------- diff --git a/scm-core/src/main/java/sonia/scm/repository/CacheClearHook.java b/scm-core/src/main/java/sonia/scm/repository/CacheClearHook.java index 185c36e036..8ad08df9f1 100644 --- a/scm-core/src/main/java/sonia/scm/repository/CacheClearHook.java +++ b/scm-core/src/main/java/sonia/scm/repository/CacheClearHook.java @@ -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 { @@ -113,18 +110,12 @@ public class CacheClearHook if (logger.isDebugEnabled()) { logger.debug("clear cache because repository {} has changed", - event.getRepository().getName()); + 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; } diff --git a/scm-core/src/main/java/sonia/scm/repository/PartCacheClearHook.java b/scm-core/src/main/java/sonia/scm/repository/PartCacheClearHook.java index 2a2830501e..a3fcf44b49 100644 --- a/scm-core/src/main/java/sonia/scm/repository/PartCacheClearHook.java +++ b/scm-core/src/main/java/sonia/scm/repository/PartCacheClearHook.java @@ -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); } } diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKeyFilter.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKeyPredicate.java similarity index 89% rename from scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKeyFilter.java rename to scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKeyPredicate.java index c2c2c41b67..ab04b3c5a1 100644 --- a/scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKeyFilter.java +++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryCacheKeyPredicate.java @@ -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 */ -public class RepositoryCacheKeyFilter - implements Filter +public class RepositoryCacheKeyPredicate + implements Predicate { /** @@ -53,7 +54,7 @@ public class RepositoryCacheKeyFilter * * @param repositoryId */ - public RepositoryCacheKeyFilter(String repositoryId) + public RepositoryCacheKeyPredicate(String repositoryId) { this.repositoryId = repositoryId; } @@ -69,7 +70,7 @@ public class RepositoryCacheKeyFilter * @return */ @Override - public boolean accept(T item) + public boolean apply(T item) { return repositoryId.equals(item.getRepositoryId()); } @@ -77,5 +78,5 @@ public class RepositoryCacheKeyFilter //~--- fields --------------------------------------------------------------- /** Field description */ - private String repositoryId; + private final String repositoryId; } diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryFilter.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryPredicate.java similarity index 86% rename from scm-core/src/main/java/sonia/scm/repository/RepositoryFilter.java rename to scm-core/src/main/java/sonia/scm/repository/RepositoryPredicate.java index 58be4d3791..58ad366bc3 100644 --- a/scm-core/src/main/java/sonia/scm/repository/RepositoryFilter.java +++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryPredicate.java @@ -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 +public class RepositoryPredicate implements Predicate { /** @@ -52,7 +52,7 @@ public class RepositoryFilter implements Filter * * @param repository */ - public RepositoryFilter(Repository repository) + public RepositoryPredicate(Repository repository) { this(repository.getId()); } @@ -63,7 +63,7 @@ public class RepositoryFilter implements Filter * * @param event */ - public RepositoryFilter(RepositoryHookEvent event) + public RepositoryPredicate(RepositoryHookEvent event) { this(event.getRepository()); } @@ -74,7 +74,7 @@ public class RepositoryFilter implements Filter * * @param repositoryId */ - public RepositoryFilter(String repositoryId) + public RepositoryPredicate(String repositoryId) { this.repositoryId = repositoryId; } @@ -90,13 +90,13 @@ public class RepositoryFilter implements Filter * @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; } diff --git a/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java b/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java index 23543d0719..455ef4027f 100644 --- a/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java +++ b/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java @@ -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); diff --git a/scm-test/src/main/java/sonia/scm/cache/MapCache.java b/scm-test/src/main/java/sonia/scm/cache/MapCache.java index 24a0d7622c..98819fdc6a 100644 --- a/scm-test/src/main/java/sonia/scm/cache/MapCache.java +++ b/scm-test/src/main/java/sonia/scm/cache/MapCache.java @@ -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 implements Cache * @return */ @Override - public boolean removeAll(Filter filter) + public boolean removeAll(Predicate 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 implements Cache //~--- fields --------------------------------------------------------------- /** Field description */ - private Map map = Maps.newHashMap(); + private final Map map = Maps.newHashMap(); } diff --git a/scm-webapp/src/main/java/sonia/scm/cache/GuavaCache.java b/scm-webapp/src/main/java/sonia/scm/cache/GuavaCache.java index 9c761fd620..db700bc344 100644 --- a/scm-webapp/src/main/java/sonia/scm/cache/GuavaCache.java +++ b/scm-webapp/src/main/java/sonia/scm/cache/GuavaCache.java @@ -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 implements Cache * @return */ @Override - public boolean removeAll(Filter filter) + public boolean removeAll(Predicate filter) { Set keysToRemove = Sets.newHashSet(); for (K key : cache.asMap().keySet()) { - if (filter.accept(key)) + if (filter.apply(key)) { keysToRemove.add(key); } diff --git a/scm-webapp/src/test/java/sonia/scm/cache/CacheTestBase.java b/scm-webapp/src/test/java/sonia/scm/cache/CacheTestBase.java index 45d646f7f7..13b893cb57 100644 --- a/scm-webapp/src/test/java/sonia/scm/cache/CacheTestBase.java +++ b/scm-webapp/src/test/java/sonia/scm/cache/CacheTestBase.java @@ -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() + cache.removeAll(new Predicate() { @Override - public boolean accept(String item) + public boolean apply(String item) { return item.startsWith("test"); }