Repository: incubator-brooklyn Updated Branches: refs/heads/master 49eabd157 -> 94687d98a
Not anonymous inner classes in predicates - Anonymous inner classes makes persistence very brittle. - Make names more consistent across the different predicates classes (e.g. ânameSatisfiesâ when passing in a predicate). - Adds more tests. Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/74207bdc Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/74207bdc Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/74207bdc Branch: refs/heads/master Commit: 74207bdc9b7b81f04b0ab671dd87643fff535b10 Parents: b73c05b Author: Aled Sage <[email protected]> Authored: Mon Oct 5 20:42:18 2015 +0100 Committer: Aled Sage <[email protected]> Committed: Mon Oct 12 09:22:25 2015 +0100 ---------------------------------------------------------------------- .../brooklyn/core/config/ConfigPredicates.java | 96 +++++++++- .../core/location/LocationPredicates.java | 182 ++++++++++++++++++- .../mgmt/entitlement/EntitlementPredicates.java | 25 ++- .../brooklyn/util/core/task/TaskPredicates.java | 10 +- .../core/config/ConfigPredicatesTest.java | 87 +++++++++ .../entitlement/EntitlementsPredicatesTest.java | 36 ++++ .../util/core/task/TaskPredicatesTest.java | 10 + .../location/jclouds/JcloudsPredicates.java | 13 +- .../brooklyn/util/math/MathPredicates.java | 108 +++++++++-- .../brooklyn/util/math/MathPredicatesTest.java | 8 + 10 files changed, 535 insertions(+), 40 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74207bdc/core/src/main/java/org/apache/brooklyn/core/config/ConfigPredicates.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/config/ConfigPredicates.java b/core/src/main/java/org/apache/brooklyn/core/config/ConfigPredicates.java index de6f3c8..a2f5bec 100644 --- a/core/src/main/java/org/apache/brooklyn/core/config/ConfigPredicates.java +++ b/core/src/main/java/org/apache/brooklyn/core/config/ConfigPredicates.java @@ -23,13 +23,20 @@ import java.util.regex.Pattern; import javax.annotation.Nullable; import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.util.guava.SerializablePredicate; +import org.apache.brooklyn.util.text.StringPredicates; import org.apache.brooklyn.util.text.WildcardGlobs; import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +@SuppressWarnings("serial") public class ConfigPredicates { - public static Predicate<ConfigKey<?>> startingWith(final String prefix) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static Predicate<ConfigKey<?>> startingWithOld(final String prefix) { + // TODO PERSISTENCE WORKAROUND return new Predicate<ConfigKey<?>>() { @Override public boolean apply(@Nullable ConfigKey<?> input) { @@ -38,7 +45,10 @@ public class ConfigPredicates { }; } - public static Predicate<ConfigKey<?>> matchingGlob(final String glob) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static Predicate<ConfigKey<?>> matchingGlobOld(final String glob) { + // TODO PERSISTENCE WORKAROUND return new Predicate<ConfigKey<?>>() { @Override public boolean apply(@Nullable ConfigKey<?> input) { @@ -47,7 +57,10 @@ public class ConfigPredicates { }; } - public static Predicate<ConfigKey<?>> matchingRegex(final String regex) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static Predicate<ConfigKey<?>> matchingRegexOld(final String regex) { + // TODO PERSISTENCE WORKAROUND final Pattern p = Pattern.compile(regex); return new Predicate<ConfigKey<?>>() { @Override @@ -57,7 +70,10 @@ public class ConfigPredicates { }; } - public static Predicate<ConfigKey<?>> nameMatching(final Predicate<String> filter) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static Predicate<ConfigKey<?>> nameMatchingOld(final Predicate<String> filter) { + // TODO PERSISTENCE WORKAROUND return new Predicate<ConfigKey<?>>() { @Override public boolean apply(@Nullable ConfigKey<?> input) { @@ -65,5 +81,77 @@ public class ConfigPredicates { } }; } + + /** @deprecated since 0.9.0; use {@link #nameStartsWith(String)} */ + public static Predicate<ConfigKey<?>> startingWith(final String prefix) { + return nameStartsWith(prefix); + } + + /** @deprecated since 0.9.0; use {@link #nameMatchesGlob(String)} */ + public static Predicate<ConfigKey<?>> matchingGlob(final String glob) { + return nameMatchesGlob(glob); + } + + /** @deprecated since 0.9.0; use {@link #nameMatchesRegex(String)} */ + public static Predicate<ConfigKey<?>> matchingRegex(final String regex) { + return nameMatchesRegex(regex); + } + + /** @deprecated since 0.9.0; use {@link #nameSatisfies(Predicate)} */ + public static Predicate<ConfigKey<?>> nameMatching(final Predicate<String> filter) { + return nameSatisfies(filter); + } + + /** + * @since 0.9.0 + */ + public static Predicate<ConfigKey<?>> nameStartsWith(final String prefix) { + return nameSatisfies(StringPredicates.startsWith(prefix)); + } + + /** + * @since 0.9.0 + */ + public static Predicate<ConfigKey<?>> nameMatchesGlob(final String glob) { + return nameSatisfies(StringPredicates.matchesGlob(glob)); + } + + /** + * @since 0.9.0 + */ + public static Predicate<ConfigKey<?>> nameMatchesRegex(final String regex) { + return nameSatisfies(StringPredicates.matchesRegex(regex)); + } + + /** + * @since 0.9.0 + */ + public static Predicate<ConfigKey<?>> nameEqualTo(final String val) { + return nameSatisfies(Predicates.equalTo(val)); + } + /** + * @since 0.9.0 + */ + public static Predicate<ConfigKey<?>> nameSatisfies(final Predicate<? super String> condition) { + return new NameSatisfies(condition); + } + + /** + * @since 0.9.0 + */ + protected static class NameSatisfies implements SerializablePredicate<ConfigKey<?>> { + protected final Predicate<? super String> condition; + protected NameSatisfies(Predicate<? super String> condition) { + this.condition = condition; + } + @Override + public boolean apply(@Nullable ConfigKey<?> input) { + return (input != null) && condition.apply(input.getName()); + } + @Override + public String toString() { + return "displayNameSatisfies("+condition+")"; + } + } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74207bdc/core/src/main/java/org/apache/brooklyn/core/location/LocationPredicates.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/location/LocationPredicates.java b/core/src/main/java/org/apache/brooklyn/core/location/LocationPredicates.java index fc336ec..35cf54f 100644 --- a/core/src/main/java/org/apache/brooklyn/core/location/LocationPredicates.java +++ b/core/src/main/java/org/apache/brooklyn/core/location/LocationPredicates.java @@ -23,13 +23,19 @@ import javax.annotation.Nullable; import org.apache.brooklyn.api.location.Location; import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.config.ConfigKey.HasConfigKey; +import org.apache.brooklyn.util.guava.SerializablePredicate; import com.google.common.base.Objects; import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +@SuppressWarnings("serial") public class LocationPredicates { - public static <T> Predicate<Location> idEqualTo(final T val) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static <T> Predicate<Location> idEqualToOld(final T val) { + // TODO PERSISTENCE WORKAROUND return new Predicate<Location>() { @Override public boolean apply(@Nullable Location input) { @@ -38,7 +44,10 @@ public class LocationPredicates { }; } - public static <T> Predicate<Location> displayNameEqualTo(final T val) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static <T> Predicate<Location> displayNameEqualToOld(final T val) { + // TODO PERSISTENCE WORKAROUND return new Predicate<Location>() { @Override public boolean apply(@Nullable Location input) { @@ -47,7 +56,10 @@ public class LocationPredicates { }; } - public static <T> Predicate<Location> configEqualTo(final ConfigKey<T> configKey, final T val) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static <T> Predicate<Location> configEqualToOld(final ConfigKey<T> configKey, final T val) { + // TODO PERSISTENCE WORKAROUND return new Predicate<Location>() { @Override public boolean apply(@Nullable Location input) { @@ -56,7 +68,10 @@ public class LocationPredicates { }; } - public static <T> Predicate<Location> configEqualTo(final HasConfigKey<T> configKey, final T val) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static <T> Predicate<Location> configEqualToOld(final HasConfigKey<T> configKey, final T val) { + // TODO PERSISTENCE WORKAROUND return new Predicate<Location>() { @Override public boolean apply(@Nullable Location input) { @@ -68,7 +83,10 @@ public class LocationPredicates { /** * Returns a predicate that determines if a given location is a direct child of this {@code parent}. */ - public static <T> Predicate<Location> isChildOf(final Location parent) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static <T> Predicate<Location> isChildOfOld(final Location parent) { + // TODO PERSISTENCE WORKAROUND return new Predicate<Location>() { @Override public boolean apply(@Nullable Location input) { @@ -77,10 +95,10 @@ public class LocationPredicates { }; } - /** - * Returns a predicate that determines if a given location is a descendant of this {@code ancestor}. - */ - public static <T> Predicate<Location> isDescendantOf(final Location ancestor) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static <T> Predicate<Location> isDescendantOfOld(final Location ancestor) { + // TODO PERSISTENCE WORKAROUND return new Predicate<Location>() { @Override public boolean apply(@Nullable Location input) { @@ -97,7 +115,10 @@ public class LocationPredicates { }; } - public static <T> Predicate<Location> managed() { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static <T> Predicate<Location> managedOld() { + // TODO PERSISTENCE WORKAROUND return new Predicate<Location>() { @Override public boolean apply(@Nullable Location input) { @@ -105,4 +126,145 @@ public class LocationPredicates { } }; } + + public static Predicate<Location> idEqualTo(final String val) { + return idSatisfies(Predicates.equalTo(val)); + } + + public static Predicate<Location> idSatisfies(final Predicate<? super String> condition) { + return new IdSatisfies(condition); + } + + protected static class IdSatisfies implements SerializablePredicate<Location> { + protected final Predicate<? super String> condition; + protected IdSatisfies(Predicate<? super String> condition) { + this.condition = condition; + } + @Override + public boolean apply(@Nullable Location input) { + return (input != null) && condition.apply(input.getId()); + } + @Override + public String toString() { + return "idSatisfies("+condition+")"; + } + } + + public static Predicate<Location> displayNameEqualTo(final String val) { + return displayNameSatisfies(Predicates.equalTo(val)); + } + + public static Predicate<Location> displayNameSatisfies(final Predicate<? super String> condition) { + return new DisplayNameSatisfies(condition); + } + + protected static class DisplayNameSatisfies implements SerializablePredicate<Location> { + protected final Predicate<? super String> condition; + protected DisplayNameSatisfies(Predicate<? super String> condition) { + this.condition = condition; + } + @Override + public boolean apply(@Nullable Location input) { + return (input != null) && condition.apply(input.getDisplayName()); + } + @Override + public String toString() { + return "displayNameSatisfies("+condition+")"; + } + } + + public static <T> Predicate<Location> configEqualTo(final ConfigKey<T> configKey, final T val) { + return configSatisfies(configKey, Predicates.equalTo(val)); + } + + public static <T> Predicate<Location> configSatisfies(final ConfigKey<T> configKey, final Predicate<T> condition) { + return new ConfigKeySatisfies<T>(configKey, condition); + } + + public static <T> Predicate<Location> configEqualTo(final HasConfigKey<T> configKey, final T val) { + return configEqualTo(configKey.getConfigKey(), val); + } + + public static <T> Predicate<Location> configSatisfies(final HasConfigKey<T> configKey, final Predicate<T> condition) { + return new ConfigKeySatisfies<T>(configKey.getConfigKey(), condition); + } + + protected static class ConfigKeySatisfies<T> implements SerializablePredicate<Location> { + protected final ConfigKey<T> configKey; + protected final Predicate<T> condition; + private ConfigKeySatisfies(ConfigKey<T> configKey, Predicate<T> condition) { + this.configKey = configKey; + this.condition = condition; + } + @Override + public boolean apply(@Nullable Location input) { + return (input != null) && condition.apply(input.getConfig(configKey)); + } + @Override + public String toString() { + return "configKeySatisfies("+configKey.getName()+","+condition+")"; + } + } + + /** + * Returns a predicate that determines if a given location is a direct child of this {@code parent}. + */ + public static Predicate<Location> isChildOf(final Location parent) { + return new IsChildOf(parent); + } + + // if needed, could add parentSatisfies(...) + + protected static class IsChildOf implements SerializablePredicate<Location> { + protected final Location parent; + protected IsChildOf(Location parent) { + this.parent = parent; + } + @Override + public boolean apply(@Nullable Location input) { + return (input != null) && Objects.equal(input.getParent(), parent); + } + @Override + public String toString() { + return "isChildOf("+parent+")"; + } + } + + /** + * Returns a predicate that determines if a given location is a descendant of this {@code ancestor}. + */ + public static <T> Predicate<Location> isDescendantOf(final Location ancestor) { + return new IsDescendantOf(ancestor); + } + + protected static class IsDescendantOf implements SerializablePredicate<Location> { + protected final Location ancestor; + protected IsDescendantOf(Location ancestor) { + this.ancestor = ancestor; + } + @Override + public boolean apply(@Nullable Location input) { + // assumes impossible to have cycles in location-hierarchy + Location contenderAncestor = (input == null) ? input : input.getParent(); + while (contenderAncestor != null) { + if (Objects.equal(contenderAncestor, ancestor)) { + return true; + } + contenderAncestor = contenderAncestor.getParent(); + } + return false; + } + } + + public static <T> Predicate<Location> managed() { + return IsManaged.INSTANCE; + } + + protected static class IsManaged implements Predicate<Location> { + protected static final IsManaged INSTANCE = new IsManaged(); + @Override + public boolean apply(@Nullable Location input) { + return (input != null) && Locations.isManaged(input); + } + } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74207bdc/core/src/main/java/org/apache/brooklyn/core/mgmt/entitlement/EntitlementPredicates.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/entitlement/EntitlementPredicates.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/entitlement/EntitlementPredicates.java index c028533..3d2e981 100644 --- a/core/src/main/java/org/apache/brooklyn/core/mgmt/entitlement/EntitlementPredicates.java +++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/entitlement/EntitlementPredicates.java @@ -18,6 +18,8 @@ */ package org.apache.brooklyn.core.mgmt.entitlement; +import static com.google.common.base.Preconditions.checkNotNull; + import javax.annotation.Nullable; import org.apache.brooklyn.api.mgmt.entitlement.EntitlementClass; @@ -27,8 +29,10 @@ import com.google.common.base.Predicate; public class EntitlementPredicates { - public static <T> Predicate<T> isEntitled(final EntitlementManager entitlementManager, final EntitlementClass<T> entitlementClass) { - + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static <T> Predicate<T> isEntitledOld(final EntitlementManager entitlementManager, final EntitlementClass<T> entitlementClass) { + // TODO PERSISTENCE WORKAROUND return new Predicate<T>() { @Override public boolean apply(@Nullable T t) { @@ -37,4 +41,21 @@ public class EntitlementPredicates { }; } + public static <T> Predicate<T> isEntitled(final EntitlementManager entitlementManager, final EntitlementClass<T> entitlementClass) { + return new IsEntitled<>(checkNotNull(entitlementManager, "entitlementManager"), checkNotNull(entitlementClass, "entitlementClass")); + } + + protected static class IsEntitled<T> implements Predicate<T> { + private final EntitlementManager entitlementManager; + private final EntitlementClass<T> entitlementClass; + + protected IsEntitled(final EntitlementManager entitlementManager, final EntitlementClass<T> entitlementClass) { + this.entitlementManager = checkNotNull(entitlementManager, "entitlementManager"); + this.entitlementClass = checkNotNull(entitlementClass, "entitlementClass"); + } + @Override + public boolean apply(@Nullable T t) { + return Entitlements.isEntitled(entitlementManager, entitlementClass, t); + } + } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74207bdc/core/src/main/java/org/apache/brooklyn/util/core/task/TaskPredicates.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/util/core/task/TaskPredicates.java b/core/src/main/java/org/apache/brooklyn/util/core/task/TaskPredicates.java index 3e42760..8e46002 100644 --- a/core/src/main/java/org/apache/brooklyn/util/core/task/TaskPredicates.java +++ b/core/src/main/java/org/apache/brooklyn/util/core/task/TaskPredicates.java @@ -27,12 +27,20 @@ import com.google.common.base.Predicates; public class TaskPredicates { + /** @deprecated since 0.9.0; use {@link #displayNameSatisfies(Predicate)} */ public static Predicate<Task<?>> displayNameMatches(Predicate<? super String> matcher) { + return displayNameSatisfies(matcher); + } + + /** + * @since 0.9.0 + */ + public static Predicate<Task<?>> displayNameSatisfies(Predicate<? super String> matcher) { return new DisplayNameMatches(matcher); } public static Predicate<Task<?>> displayNameEqualTo(String name) { - return displayNameMatches(Predicates.equalTo(name)); + return displayNameSatisfies(Predicates.equalTo(name)); } private static class DisplayNameMatches implements Predicate<Task<?>> { http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74207bdc/core/src/test/java/org/apache/brooklyn/core/config/ConfigPredicatesTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/config/ConfigPredicatesTest.java b/core/src/test/java/org/apache/brooklyn/core/config/ConfigPredicatesTest.java new file mode 100644 index 0000000..e9bc9b5 --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/core/config/ConfigPredicatesTest.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.core.config; + +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; +import org.testng.annotations.Test; + +import com.google.common.base.Predicates; + +public class ConfigPredicatesTest extends BrooklynAppUnitTestSupport { + + private final ConfigKey<String> STR1 = ConfigKeys.newStringConfigKey("test.str1"); + + @Test + public void testNameMatchingPredicate() throws Exception { + assertTrue(ConfigPredicates.nameMatching(Predicates.equalTo("test.str1")).apply(STR1)); + assertFalse(ConfigPredicates.nameMatching(Predicates.equalTo("wrong")).apply(STR1)); + } + + @Test + public void testNameMatchingGlob() throws Exception { + assertTrue(ConfigPredicates.matchingGlob("*str*").apply(STR1)); + assertFalse(ConfigPredicates.matchingGlob("*wrong*").apply(STR1)); + } + + @Test + public void testNameMatchingRegex() throws Exception { + assertTrue(ConfigPredicates.matchingRegex(".*str.*").apply(STR1)); + assertFalse(ConfigPredicates.matchingRegex(".*wrong.*").apply(STR1)); + } + + @Test + public void testNameStartingWith() throws Exception { + assertTrue(ConfigPredicates.startingWith("test.s").apply(STR1)); + assertFalse(ConfigPredicates.startingWith("wrong.s").apply(STR1)); + } + + @Test + public void testNameEqualTo() throws Exception { + assertTrue(ConfigPredicates.nameEqualTo("test.str1").apply(STR1)); + assertFalse(ConfigPredicates.nameEqualTo("wrong").apply(STR1)); + } + + @Test + public void testNameSatisfies() throws Exception { + assertTrue(ConfigPredicates.nameSatisfies(Predicates.equalTo("test.str1")).apply(STR1)); + assertFalse(ConfigPredicates.nameSatisfies(Predicates.equalTo("wrong")).apply(STR1)); + } + + @Test + public void testNameMatchesGlob() throws Exception { + assertTrue(ConfigPredicates.nameMatchesGlob("*str*").apply(STR1)); + assertFalse(ConfigPredicates.nameMatchesGlob("*wrong*").apply(STR1)); + } + + @Test + public void testNameMatchesRegex() throws Exception { + assertTrue(ConfigPredicates.nameMatchesRegex(".*str.*").apply(STR1)); + assertFalse(ConfigPredicates.nameMatchesRegex(".*wrong.*").apply(STR1)); + } + + @Test + public void testNameStartsWith() throws Exception { + assertTrue(ConfigPredicates.nameStartsWith("test.s").apply(STR1)); + assertFalse(ConfigPredicates.nameStartsWith("wrong.s").apply(STR1)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74207bdc/core/src/test/java/org/apache/brooklyn/core/mgmt/entitlement/EntitlementsPredicatesTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/mgmt/entitlement/EntitlementsPredicatesTest.java b/core/src/test/java/org/apache/brooklyn/core/mgmt/entitlement/EntitlementsPredicatesTest.java new file mode 100644 index 0000000..6412f32 --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/core/mgmt/entitlement/EntitlementsPredicatesTest.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.core.mgmt.entitlement; + +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +import org.apache.brooklyn.api.mgmt.entitlement.EntitlementManager; +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; +import org.testng.annotations.Test; + +@Test +public class EntitlementsPredicatesTest extends BrooklynAppUnitTestSupport { + + public void testIsEntitled() { + EntitlementManager allowSeeEntity = Entitlements.FineGrainedEntitlements.allowing(Entitlements.ROOT); + assertTrue(EntitlementPredicates.isEntitled(allowSeeEntity, Entitlements.ROOT).apply(null)); + assertFalse(EntitlementPredicates.isEntitled(allowSeeEntity, Entitlements.SEE_ENTITY).apply(null)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74207bdc/core/src/test/java/org/apache/brooklyn/util/core/task/TaskPredicatesTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/util/core/task/TaskPredicatesTest.java b/core/src/test/java/org/apache/brooklyn/util/core/task/TaskPredicatesTest.java index f2709f2..fce6f0f 100644 --- a/core/src/test/java/org/apache/brooklyn/util/core/task/TaskPredicatesTest.java +++ b/core/src/test/java/org/apache/brooklyn/util/core/task/TaskPredicatesTest.java @@ -60,4 +60,14 @@ public class TaskPredicatesTest extends BrooklynAppUnitTestSupport { assertTrue(TaskPredicates.displayNameMatches(Predicates.equalTo("myname")).apply(task)); assertFalse(TaskPredicates.displayNameMatches(Predicates.equalTo("wrong")).apply(task)); } + + @Test + public void testDisplayNameSatisfies() throws Exception { + Task<Object> task = execManager.submit(TaskBuilder.builder() + .body(Callables.<Object>returning("val")) + .displayName("myname") + .build()); + assertTrue(TaskPredicates.displayNameSatisfies(Predicates.equalTo("myname")).apply(task)); + assertFalse(TaskPredicates.displayNameSatisfies(Predicates.equalTo("wrong")).apply(task)); + } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74207bdc/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsPredicates.java ---------------------------------------------------------------------- diff --git a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsPredicates.java b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsPredicates.java index 565425d..7fe8ee3 100644 --- a/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsPredicates.java +++ b/locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/JcloudsPredicates.java @@ -25,9 +25,17 @@ import com.google.common.base.Predicate; public class JcloudsPredicates { + public static Predicate<ComputeMetadata> nodeInLocation(String regionId, boolean matchNullLocations) { + return new NodeInLocation(regionId, matchNullLocations); + } + + /** + * @deprecated since 0.9.0; direct access strongly discouraged; will be made protected in future release; + * use {@link JcloudsPredicates#nodeInLocation(String, boolean)} + */ public static class NodeInLocation implements Predicate<ComputeMetadata> { - private String regionId; - private boolean matchNullLocations; + private final String regionId; + private final boolean matchNullLocations; public NodeInLocation(String regionId, boolean matchNullLocations) { this.regionId = regionId; this.matchNullLocations = matchNullLocations; @@ -49,5 +57,4 @@ public class JcloudsPredicates { return !exclude; } } - } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74207bdc/utils/common/src/main/java/org/apache/brooklyn/util/math/MathPredicates.java ---------------------------------------------------------------------- diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/math/MathPredicates.java b/utils/common/src/main/java/org/apache/brooklyn/util/math/MathPredicates.java index e03c379..3e5c9c2 100644 --- a/utils/common/src/main/java/org/apache/brooklyn/util/math/MathPredicates.java +++ b/utils/common/src/main/java/org/apache/brooklyn/util/math/MathPredicates.java @@ -25,11 +25,10 @@ import com.google.common.base.Predicate; public class MathPredicates { - /** - * Creates a predicate comparing a given number with {@code val}. - * A number of {@code null} passed to the predicate will always return false. - */ - public static <T extends Number> Predicate<T> greaterThan(final double val) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static <T extends Number> Predicate<T> greaterThanOld(final double val) { + // TODO PERSISTENCE WORKAROUND return new Predicate<T>() { public boolean apply(@Nullable T input) { return (input == null) ? false : input.doubleValue() > val; @@ -37,11 +36,10 @@ public class MathPredicates { }; } - /** - * Creates a predicate comparing a given number with {@code val}. - * A number of {@code null} passed to the predicate will always return false. - */ - public static <T extends Number> Predicate<T> greaterThanOrEqual(final double val) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static <T extends Number> Predicate<T> greaterThanOrEqualOld(final double val) { + // TODO PERSISTENCE WORKAROUND return new Predicate<T>() { public boolean apply(@Nullable T input) { return (input == null) ? false : input.doubleValue() >= val; @@ -49,11 +47,10 @@ public class MathPredicates { }; } - /** - * Creates a predicate comparing a given number with {@code val}. - * A number of {@code null} passed to the predicate will always return false. - */ - public static <T extends Number> Predicate<T> lessThan(final double val) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static <T extends Number> Predicate<T> lessThanOld(final double val) { + // TODO PERSISTENCE WORKAROUND return new Predicate<T>() { public boolean apply(@Nullable T input) { return (input == null) ? false : input.doubleValue() < val; @@ -61,17 +58,88 @@ public class MathPredicates { }; } - /** - * Creates a predicate comparing a given number with {@code val}. - * A number of {@code null} passed to the predicate will always return false. - */ - public static <T extends Number> Predicate<T> lessThanOrEqual(final double val) { + /** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */ + @SuppressWarnings("unused") @Deprecated + private static <T extends Number> Predicate<T> lessThanOrEqualOld(final double val) { + // TODO PERSISTENCE WORKAROUND return new Predicate<T>() { public boolean apply(@Nullable T input) { return (input == null) ? false : input.doubleValue() <= val; } }; } + + /** + * Creates a predicate comparing a given number with {@code val}. + * A number of {@code null} passed to the predicate will always return false. + */ + public static <T extends Number> Predicate<T> greaterThan(final double val) { + return new GreaterThan<T>(val); + } + + protected static class GreaterThan<T extends Number> implements Predicate<T> { + private final double val; + protected GreaterThan(double val) { + this.val = val; + } + public boolean apply(@Nullable T input) { + return (input == null) ? false : input.doubleValue() > val; + } + } + + /** + * Creates a predicate comparing a given number with {@code val}. + * A number of {@code null} passed to the predicate will always return false. + */ + public static <T extends Number> Predicate<T> greaterThanOrEqual(final double val) { + return new GreaterThanOrEqual<T>(val); + } + + protected static class GreaterThanOrEqual<T extends Number> implements Predicate<T> { + private final double val; + protected GreaterThanOrEqual(double val) { + this.val = val; + } + public boolean apply(@Nullable T input) { + return (input == null) ? false : input.doubleValue() >= val; + } + } + + /** + * Creates a predicate comparing a given number with {@code val}. + * A number of {@code null} passed to the predicate will always return false. + */ + public static <T extends Number> Predicate<T> lessThan(final double val) { + return new LessThan<T>(val); + } + + protected static class LessThan<T extends Number> implements Predicate<T> { + private final double val; + protected LessThan(double val) { + this.val = val; + } + public boolean apply(@Nullable T input) { + return (input == null) ? false : input.doubleValue() < val; + } + } + + /** + * Creates a predicate comparing a given number with {@code val}. + * A number of {@code null} passed to the predicate will always return false. + */ + public static <T extends Number> Predicate<T> lessThanOrEqual(final double val) { + return new LessThanOrEqual<T>(val); + } + + protected static class LessThanOrEqual<T extends Number> implements Predicate<T> { + private final double val; + protected LessThanOrEqual(double val) { + this.val = val; + } + public boolean apply(@Nullable T input) { + return (input == null) ? false : input.doubleValue() <= val; + } + } /** * Creates a predicate comparing a given number with {@code val}. http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/74207bdc/utils/common/src/test/java/org/apache/brooklyn/util/math/MathPredicatesTest.java ---------------------------------------------------------------------- diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/math/MathPredicatesTest.java b/utils/common/src/test/java/org/apache/brooklyn/util/math/MathPredicatesTest.java index 3eb2802..06faf4f 100644 --- a/utils/common/src/test/java/org/apache/brooklyn/util/math/MathPredicatesTest.java +++ b/utils/common/src/test/java/org/apache/brooklyn/util/math/MathPredicatesTest.java @@ -53,4 +53,12 @@ public class MathPredicatesTest { assertTrue(MathPredicates.lessThanOrEqual(2d).apply(1)); assertTrue(MathPredicates.lessThanOrEqual(2d).apply(2)); } + + @Test + public void testEqualsApproximately() throws Exception { + assertFalse(MathPredicates.equalsApproximately(2d).apply(3)); + assertTrue(MathPredicates.equalsApproximately(2d).apply(2d)); + assertTrue(MathPredicates.equalsApproximately(2d).apply(2.00000001d)); + assertTrue(MathPredicates.equalsApproximately(2).apply(2.00000001d)); + } }
