pgyori commented on code in PR #8670:
URL: https://github.com/apache/nifi/pull/8670#discussion_r1617159432


##########
nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/ClosedOpenInterval.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.nifi.web.util;
+
+/**
+ * This implementation includes the lower boundary bud does not include the 
higher boundary.
+ */
+final class ClosedOpenInterval implements Interval {
+    private final int lowerBoundary;
+    private final int higherBoundary;
+
+    /**
+     * @param lowerBoundary Inclusive index of lower boundary
+     * @param higherBoundary Exclusive index of higher boundary. In case of 0, 
the higher boundary is unspecified and the interval is open.
+     */
+    ClosedOpenInterval(final int lowerBoundary, final int higherBoundary) {
+        if (lowerBoundary < 0) {
+            throw new IllegalArgumentException("Lower boundary cannot be 
negative");
+        }
+
+        if (higherBoundary < 0) {
+            throw new IllegalArgumentException("Higher boundary cannot be 
negative");
+        }
+
+        if (higherBoundary <= lowerBoundary && higherBoundary != 0) {
+            throw new IllegalArgumentException(
+                "Higher boundary cannot be lower than or equal to lower 
boundary except when unspecified. Higher boundary is considered unspecified 
when the value is set to 0"
+            );
+        }
+
+        this.lowerBoundary = lowerBoundary;
+        this.higherBoundary = higherBoundary;
+    }
+
+    @Override
+    public RelativePosition getRelativePositionOf(final int 
otherIntervalLowerBoundary, final int otherIntervalHigherBoundary) {
+        if (otherIntervalLowerBoundary < 0) {
+            throw new IllegalArgumentException("Lower boundary cannot be 
negative");
+        }
+
+        if (otherIntervalHigherBoundary <= 0) {
+            // Note: as a design decision the implementation currently does 
not support comparison with unspecified higher boundary
+            throw new IllegalArgumentException("Higher boundary must be 
positive");
+        }
+
+        if (otherIntervalLowerBoundary > otherIntervalHigherBoundary) {

Review Comment:
   `otherIntervalLowerBoundary >= otherIntervalHigherBoundary` needs to be used 
in order to check for 0-length intervals where lower==higher (currently this 
case is not covered).



##########
nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/Interval.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.nifi.web.util;
+
+public interface Interval {
+
+    enum RelativePosition {
+        /**
+         * The compared interval ends before the actual, there is no 
intersection.
+         */
+        BEFORE,
+
+        /**
+         * The compared interval exceeds the actual both at the low and high 
ends.
+         */
+        EXCEEDS,
+
+        /**
+         * The compared interval's tail (but not the whole interval) 
intersects the actual interval (part of it or the whole actual interval).
+         */
+        TAIL_INTERSECTS,
+
+        /**
+         * The compared interval is within the actual interval. It can match 
with the actual or contained by that.
+         */
+        WITHIN,
+
+        /**
+         *The compared interval's head (but not the whole interval) intersects 
the actual interval  (part of it or the whole actual interval).
+         */
+        HEAD_INTERSECTS,
+
+        /**
+         * The compared interval starts after the actual, there is no 
intersection.
+         */
+        AFTER,
+    }
+
+    /**
+     * Relative position of the "other" interval compared to this.
+     *
+     * @param otherIntervalLowerBoundary Lower boundary of the compared 
interval.
+     * @param otherIntervalHigherBoundary Higher boundary of the compared 
interval.
+     *
+     * @return Returns the relative position of the "other" interval compared 
to this interval. For example: if the result
+     *         is BEFORE, read it as: the other interval ends BEFORE the 
actual (and there is no intersection between them).
+     */
+    RelativePosition getRelativePositionOf(final int 
otherIntervalLowerBoundary, final int otherIntervalHigherBoundary);

Review Comment:
   I'll leave this thought here for consideration:
   I believe it would make sense for this method to accept another Interval 
object as parameter instead of boundary integers. That way in the subclass we 
do not have to implement again the checks for the reations between the lower 
and higher boundary. The only extra check in the sublcass' method would be to 
not allow the other interval to have undefined higher boundary.



##########
nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/ClosedOpenInterval.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.nifi.web.util;
+
+/**
+ * This implementation includes the lower boundary bud does not include the 
higher boundary.

Review Comment:
   Typo in the comment: "bud" = "but".



##########
nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/util/Interval.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.nifi.web.util;
+
+public interface Interval {
+
+    enum RelativePosition {
+        /**
+         * The compared interval ends before the actual, there is no 
intersection.
+         */
+        BEFORE,
+
+        /**
+         * The compared interval exceeds the actual both at the low and high 
ends.
+         */
+        EXCEEDS,
+
+        /**
+         * The compared interval's tail (but not the whole interval) 
intersects the actual interval (part of it or the whole actual interval).
+         */
+        TAIL_INTERSECTS,
+
+        /**
+         * The compared interval is within the actual interval. It can match 
with the actual or contained by that.
+         */
+        WITHIN,
+
+        /**
+         *The compared interval's head (but not the whole interval) intersects 
the actual interval  (part of it or the whole actual interval).
+         */
+        HEAD_INTERSECTS,
+
+        /**
+         * The compared interval starts after the actual, there is no 
intersection.
+         */
+        AFTER,
+    }
+
+    /**
+     * Relative position of the "other" interval compared to this.
+     *
+     * @param otherIntervalLowerBoundary Lower boundary of the compared 
interval.
+     * @param otherIntervalHigherBoundary Higher boundary of the compared 
interval.
+     *
+     * @return Returns the relative position of the "other" interval compared 
to this interval. For example: if the result
+     *         is BEFORE, read it as: the other interval ends BEFORE the 
actual (and there is no intersection between them).
+     */
+    RelativePosition getRelativePositionOf(final int 
otherIntervalLowerBoundary, final int otherIntervalHigherBoundary);
+
+    /**
+     * @return Returns an interval instance with closed low and open high 
boundary.
+     */
+    static Interval getClosedOpenInterval(final int lowerBoundary, final int 
higherBoundary) {

Review Comment:
   I do not agree with having a factory method in the generic interface: if 
further classes were to be added that implement the interface, then the 
interface would need to be modified each time to contain factory methods for 
each implementing type if we wanted to maintain consistency. I think this 
method could be removed and instead of calling 
`Interval.getClosedOpenInterval(offset, higherBoundary)` in PaginationHelper, 
`new ClosedOpenInterval(offset, higherBoundary)` constructor could be used.



##########
nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/web/util/ClosedOpenIntervalTest.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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.nifi.web.util;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.util.stream.Stream;
+
+class ClosedOpenIntervalTest {
+
+    @Test
+    public void testNegativeLowerBoundary() {
+        Assertions.assertThrows(IllegalArgumentException.class, () -> new 
ClosedOpenInterval(-1, 3));
+    }
+
+    @Test
+    public void testNegativeHigherBoundary() {
+        Assertions.assertThrows(IllegalArgumentException.class, () -> new 
ClosedOpenInterval(0, -1));
+    }
+
+    @Test
+    public void testReorderedBoundaries() {

Review Comment:
   I believe a more appropriate name would be `testSwitchedBoundaries`, just 
like the word "switched" is used in `testCompareWhenOtherBoundariesAreSwitched`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to