Skip to content

Commit

Permalink
OAK-11413 : extracted out Deque methods from CollectionUtils (#2007)
Browse files Browse the repository at this point in the history
* OAK-11413 : extracted out Deque methods from CollectionUtils

* OAK-11413 : added missing license header

---------

Co-authored-by: Rishabh Kumar <[email protected]>
  • Loading branch information
rishabhdaim and Rishabh Kumar authored Jan 22, 2025
1 parent 41fbc8e commit 593d4c1
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
*/
package org.apache.jackrabbit.oak.commons.collections;

import java.util.ArrayDeque;
import java.util.Objects;

import org.jetbrains.annotations.NotNull;

/**
* Utility methods for collections conversions.
*/
Expand All @@ -36,21 +31,6 @@ private CollectionUtils() {
// no instances for you
}

/**
* Convert an iterable to a {@link java.util.ArrayDeque}.
* The returning array deque is mutable and supports all optional operations.
*
* @param iterable the iterable to convert
* @param <T> the type of the elements
* @return the arrayDeque
*/
public static <T> ArrayDeque<T> toArrayDeque(@NotNull Iterable<? extends T> iterable) {
Objects.requireNonNull(iterable);
ArrayDeque<T> arrayDeque = new ArrayDeque<>();
iterable.forEach(arrayDeque::add);
return arrayDeque;
}

/**
* Ensure the capacity of a map or set given the expected number of elements.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.jackrabbit.oak.commons.collections;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayDeque;
import java.util.Objects;

/**
* Utility methods for {@link java.util.Deque} conversions.
*/
public class DequeUtils {

private DequeUtils() {
// no instances for you
}

/**
* Convert an iterable to a {@link java.util.ArrayDeque}.
* The returning array deque is mutable and supports all optional operations.
*
* @param iterable the iterable to convert
* @param <T> the type of the elements
* @return the arrayDeque
*/
public static <T> ArrayDeque<T> toArrayDeque(@NotNull Iterable<? extends T> iterable) {
Objects.requireNonNull(iterable);
ArrayDeque<T> arrayDeque = new ArrayDeque<>();
iterable.forEach(arrayDeque::add);
return arrayDeque;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,10 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.fail;

public class CollectionUtilsTest {

@Test
public void toArrayDequeWithNonEmptyIterable() {
List<String> list = Arrays.asList("one", "two", "three");
ArrayDeque<String> result = CollectionUtils.toArrayDeque(list);

Assert.assertNotNull(result);
Assert.assertEquals(3, result.size());
Assert.assertEquals("one", result.peekFirst());
Assert.assertEquals("three", result.peekLast());
}

@Test
public void toArrayDequeWithEmptyIterable() {
List<String> emptyList = Collections.emptyList();
ArrayDeque<String> result = CollectionUtils.toArrayDeque(emptyList);

Assert.assertNotNull(result);
Assert.assertTrue(result.isEmpty());
}
@Test
public void ensureCapacity() {
int capacity = CollectionUtils.ensureCapacity(8);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.jackrabbit.oak.commons.collections;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* Unit tests for the {@link DequeUtils} class.
* <p>
* This class contains test cases to verify the functionality of the methods
* in the {@link DequeUtils} class.
*/
public class DequeUtilsTest {

@Test
public void toArrayDequeWithNonEmptyIterable() {
List<String> list = Arrays.asList("one", "two", "three");
ArrayDeque<String> result = DequeUtils.toArrayDeque(list);

Assert.assertNotNull(result);
Assert.assertEquals(3, result.size());
Assert.assertEquals("one", result.peekFirst());
Assert.assertEquals("three", result.peekLast());
}

@Test
public void toArrayDequeWithEmptyIterable() {
List<String> emptyList = Collections.emptyList();
ArrayDeque<String> result = DequeUtils.toArrayDeque(emptyList);

Assert.assertNotNull(result);
Assert.assertTrue(result.isEmpty());
}

@Test
public void testToArrayDequeWithNullIterable() {
Assert.assertThrows(NullPointerException.class, () -> DequeUtils.toArrayDeque(null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import org.apache.jackrabbit.guava.common.collect.Ordering;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.commons.PathUtils;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.collections.DequeUtils;
import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
import org.apache.jackrabbit.oak.commons.json.JsopReader;
import org.apache.jackrabbit.oak.commons.json.JsopTokenizer;
Expand Down Expand Up @@ -1434,7 +1434,7 @@ Iterator<NodeDocument> getAllPreviousDocs() {
//on property that all prevDoc id would starts <depth+2>:p/path/to/node
return new AbstractIterator<NodeDocument>(){
private Queue<Map.Entry<Revision, Range>> previousRanges =
CollectionUtils.toArrayDeque(getPreviousRanges().entrySet());
DequeUtils.toArrayDeque(getPreviousRanges().entrySet());
@Override
protected NodeDocument computeNext() {
if(!previousRanges.isEmpty()){
Expand Down

0 comments on commit 593d4c1

Please sign in to comment.