Repository: brooklyn-server Updated Branches: refs/heads/master 39301e0f7 -> 110482861
Add DurationPredicates Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/070c6f54 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/070c6f54 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/070c6f54 Branch: refs/heads/master Commit: 070c6f54abb0c689e55656661f7f699c14c571e3 Parents: 6571bab Author: Mike Zaccardo <mike.zacca...@cloudsoftcorp.com> Authored: Thu Feb 9 11:16:29 2017 -0500 Committer: Mike Zaccardo <mike.zacca...@cloudsoftcorp.com> Committed: Thu Feb 9 16:52:20 2017 -0500 ---------------------------------------------------------------------- .../brooklyn/util/time/DurationPredicates.java | 162 +++++++++++++++++++ .../util/time/DurationPredicatesTest.java | 150 +++++++++++++++++ 2 files changed, 312 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/070c6f54/utils/common/src/main/java/org/apache/brooklyn/util/time/DurationPredicates.java ---------------------------------------------------------------------- diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/time/DurationPredicates.java b/utils/common/src/main/java/org/apache/brooklyn/util/time/DurationPredicates.java new file mode 100644 index 0000000..162f6f5 --- /dev/null +++ b/utils/common/src/main/java/org/apache/brooklyn/util/time/DurationPredicates.java @@ -0,0 +1,162 @@ +/* + * 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.util.time; + +import java.util.concurrent.TimeUnit; + +import com.google.common.base.Preconditions; +import com.google.common.base.Predicate; +import com.google.common.base.Stopwatch; + +public class DurationPredicates { + + /** + * @return A {@link Predicate} that checks if a {@link Duration} supplied to + * {@link Predicate#apply(Object)} is positive. + */ + public static Predicate<Duration> positive() { + return new Positive(); + } + + protected static class Positive implements Predicate<Duration> { + @Override + public boolean apply(Duration input) { + return input != null && input.isPositive(); + } + } + + /** + * @return A {@link Predicate} that checks if a {@link Duration} supplied to + * {@link Predicate#apply(Object)} is negative. + */ + public static Predicate<Duration> negative() { + return new Negative(); + } + + protected static class Negative implements Predicate<Duration> { + @Override + public boolean apply(Duration input) { + return input != null && input.isNegative(); + } + } + + /** + * @param duration + * The {@link Duration} that will be the basis for comparison in + * the returned {@link Predicate}. + * @return A {@link Predicate} that checks if a {@link Duration} supplied to + * {@link Predicate#apply(Object)} is longer than the + * {@link Duration} that was supplied to this method. + */ + public static Predicate<Duration> longerThan(final Duration duration) { + return new LongerThan(duration); + } + + protected static class LongerThan implements Predicate<Duration> { + private final Duration value; + + protected LongerThan(Duration value) { + Preconditions.checkNotNull(value); + this.value = value; + } + + @Override + public boolean apply(Duration input) { + return input != null && input.isLongerThan(value); + } + } + + /** + * @param duration + * The {@link Duration} that will be the basis for comparison in + * the returned {@link Predicate}. + * @return A {@link Predicate} that checks if a {@link Duration} supplied to + * {@link Predicate#apply(Object)} is shorter than the + * {@link Duration} that was supplied to this method. + */ + public static Predicate<Duration> shorterThan(final Duration duration) { + return new ShorterThan(duration); + } + + protected static class ShorterThan implements Predicate<Duration> { + private final Duration value; + + protected ShorterThan(Duration value) { + Preconditions.checkNotNull(value); + this.value = value; + } + + @Override + public boolean apply(Duration input) { + return input != null && input.isShorterThan(value); + } + } + + /** + * @param duration + * The {@link Duration} that will be the basis for comparison in + * the returned {@link Predicate}. + * @return A {@link Predicate} that checks if a {@link Stopwatch} supplied to + * {@link Predicate#apply(Object)} is longer than the + * {@link Duration} that was supplied to this method. + */ + public static Predicate<Stopwatch> longerThanDuration(final Duration duration) { + return new LongerThanDuration(duration); + } + + protected static class LongerThanDuration implements Predicate<Stopwatch> { + private final Duration value; + + protected LongerThanDuration(Duration value) { + Preconditions.checkNotNull(value); + this.value = value; + } + + @Override + public boolean apply(Stopwatch input) { + return input != null && Duration.millis(input.elapsed(TimeUnit.MILLISECONDS)).isLongerThan(value); + } + } + + /** + * @param duration + * The {@link Duration} that will be the basis for comparison in + * the returned {@link Predicate}. + * @return A {@link Predicate} that checks if a {@link Stopwatch} supplied to + * {@link Predicate#apply(Object)} is shorter than the + * {@link Duration} that was supplied to this method. + */ + public static Predicate<Stopwatch> shorterThanDuration(final Duration duration) { + return new ShorterThanDuration(duration); + } + + protected static class ShorterThanDuration implements Predicate<Stopwatch> { + private final Duration value; + + protected ShorterThanDuration(Duration value) { + Preconditions.checkNotNull(value); + this.value = value; + } + + @Override + public boolean apply(Stopwatch input) { + return input != null && Duration.millis(input.elapsed(TimeUnit.MILLISECONDS)).isShorterThan(value); + } + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/070c6f54/utils/common/src/test/java/org/apache/brooklyn/util/time/DurationPredicatesTest.java ---------------------------------------------------------------------- diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/time/DurationPredicatesTest.java b/utils/common/src/test/java/org/apache/brooklyn/util/time/DurationPredicatesTest.java new file mode 100644 index 0000000..39d4184 --- /dev/null +++ b/utils/common/src/test/java/org/apache/brooklyn/util/time/DurationPredicatesTest.java @@ -0,0 +1,150 @@ +/* + * 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.util.time; + +import static org.testng.Assert.assertEquals; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import com.google.common.base.Stopwatch; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; + +public class DurationPredicatesTest { + + private static final List<Duration> TEST_DURATIONS_POSITIVE = Lists.newArrayList(null, + Duration.minutes(1), Duration.minutes(2), Duration.minutes(5), Duration.minutes(10)); + + private static final List<Duration> TEST_DURATIONS_NEGATIVE = Lists.newArrayList(null, + Duration.minutes(-1), Duration.minutes(-2), Duration.minutes(-5), Duration.minutes(-10)); + + private static final List<Stopwatch> TEST_STOPWATCHES = new ArrayList<>(TEST_DURATIONS_POSITIVE.size()); + + @BeforeClass + public static void setUp() throws Exception { + for (Duration duration : TEST_DURATIONS_POSITIVE) { + TEST_STOPWATCHES.add(createStopwatchWithElapsedTime(duration)); + } + } + + @Test + public void testPositive() { + Iterable<Duration> result = Iterables.filter(TEST_DURATIONS_POSITIVE, DurationPredicates.positive()); + assertEquals(Iterables.size(result), Iterables.size(TEST_DURATIONS_POSITIVE) - 1); + + result = Iterables.filter(TEST_DURATIONS_NEGATIVE, DurationPredicates.positive()); + assertEquals(Iterables.size(result), 0); + } + + @Test + public void testNegative() { + Iterable<Duration> result = Iterables.filter(TEST_DURATIONS_POSITIVE, DurationPredicates.negative()); + assertEquals(Iterables.size(result), 0); + + result = Iterables.filter(TEST_DURATIONS_NEGATIVE, DurationPredicates.negative()); + assertEquals(Iterables.size(result), Iterables.size(TEST_DURATIONS_NEGATIVE) - 1); + } + + @Test + public void testLongerThan() { + Duration testDuration = Duration.minutes(3); + + Iterable<Duration> result = Iterables.filter(TEST_DURATIONS_POSITIVE, + DurationPredicates.longerThan(testDuration)); + assertEquals(Iterables.size(result), 2); + + result = Iterables.filter(TEST_DURATIONS_NEGATIVE, DurationPredicates.longerThan(testDuration)); + assertEquals(Iterables.size(result), 0); + + testDuration = Duration.minutes(-3); + + result = Iterables.filter(TEST_DURATIONS_POSITIVE, DurationPredicates.longerThan(testDuration)); + assertEquals(Iterables.size(result), 4); + + result = Iterables.filter(TEST_DURATIONS_NEGATIVE, DurationPredicates.longerThan(testDuration)); + assertEquals(Iterables.size(result), 2); + } + + @Test + public void testShorterThan() { + Duration testDuration = Duration.minutes(3); + + Iterable<Duration> result = Iterables.filter(TEST_DURATIONS_POSITIVE, + DurationPredicates.shorterThan(testDuration)); + assertEquals(Iterables.size(result), 2); + + result = Iterables.filter(TEST_DURATIONS_NEGATIVE, DurationPredicates.shorterThan(testDuration)); + assertEquals(Iterables.size(result), 4); + + testDuration = Duration.minutes(-3); + + result = Iterables.filter(TEST_DURATIONS_POSITIVE, DurationPredicates.shorterThan(testDuration)); + assertEquals(Iterables.size(result), 0); + + result = Iterables.filter(TEST_DURATIONS_NEGATIVE, DurationPredicates.shorterThan(testDuration)); + assertEquals(Iterables.size(result), 2); + } + + @Test + public void testLongerThanDuration() { + Duration testDuration = Duration.minutes(3); + + Iterable<Stopwatch> result = Iterables.filter(TEST_STOPWATCHES, + DurationPredicates.longerThanDuration(testDuration)); + assertEquals(Iterables.size(result), 2); + + testDuration = Duration.minutes(-3); + + result = Iterables.filter(TEST_STOPWATCHES, DurationPredicates.longerThanDuration(testDuration)); + assertEquals(Iterables.size(result), 4); + } + + @Test + public void testShorterThanDuration() { + Duration testDuration = Duration.minutes(3); + + Iterable<Stopwatch> result = Iterables.filter(TEST_STOPWATCHES, + DurationPredicates.shorterThanDuration(testDuration)); + assertEquals(Iterables.size(result), 2); + + testDuration = Duration.minutes(-3); + + result = Iterables.filter(TEST_STOPWATCHES, DurationPredicates.shorterThanDuration(testDuration)); + assertEquals(Iterables.size(result), 0); + } + + private static Stopwatch createStopwatchWithElapsedTime(Duration duration) throws Exception { + if (duration == null) { + return null; + } + + Stopwatch stopwatch = Stopwatch.createUnstarted(); + + Field field = stopwatch.getClass().getDeclaredField("elapsedNanos"); + field.setAccessible(true); + field.set(stopwatch, duration.nanos()); + + return stopwatch; + } +}