-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[COLLECTIONS-795] Add a new Iterator to allowing zipping over two iterators of different types #238
Closed
Closed
Changes from 1 commit
Commits
Show all changes
326 commits
Select commit
Hold shift + click to select a range
afa8fe8
Use java.lang.Objects#equals; eliminate a couple of nulls #307
garydgregory 8aa1cd4
Replace JUnit 3/4 @Ignore with JUnit 5 @Disabled
garydgregory 2a6308e
[COLLECTIONS-811] Integrate Guava Testlib tests for Apache Commons Co…
kinow 61d87b9
Bump actions/cache from 3.0.3 to 3.0.4
garydgregory d814fc1
Bump maven-pmd-plugin from 3.16.0 to 3.17.0
dependabot[bot] cf3fb3d
Remove obsolete Maven profile
garydgregory a22a2b9
Fix and update site build (except jdepend/site broken compatibility)
garydgregory 4e36ad9
Simplify bloom filters (#258)
Claudenw 86e5f7c
Move counting long predicate (#314)
Claudenw 0096325
Bump commons.pmd-impl.version from 6.46.0 to 6.47.0
dependabot[bot] e306c67
Add missing namespace and XSD to SpotBugs filter file.
garydgregory b7bcfcf
Add missing namespace and XSD to SpotBugs filter file.
garydgregory a2b0fb7
COLLECTIONS-823: Modified ArrayCountingBloomFilter.forEachBitMap to b…
Claudenw 194c16c
moved ArrayTracker and BitMapTracker to their own files.
Claudenw 50c9a89
Added new lines at end of files
Claudenw b723a52
put IndexFilter in separate file.
Claudenw c9c0692
added missing license
Claudenw 6b060af
fixed checkstyle issues
Claudenw 3c2f9c9
removed bad import
Claudenw f44bab8
Made IndexFilter.create() return an IntPredicate
Claudenw 0a21aab
removed unused imports
Claudenw 2824b3d
Use for-each loop
garydgregory f5256e4
Bump actions/cache 3.0.4 to 3.0.5
garydgregory e7b92dc
replaced merge with old mergeInPlace
Claudenw 056c6f4
removed files added in error
Claudenw 7b4ea05
Bump commons.junit.version from 5.8.2 to 5.9.0 (#324)
dependabot[bot] 88147b1
Bump commons.junit.version from 5.8.2 to 5.9.0 #324
garydgregory e452ee9
Update GitHub builds to use Temurin
garydgregory f4d9058
Bump actions/cache from 3.0.5 to 3.0.6
garydgregory eddbd00
Bump actions/cache from 3.0.5 to 3.0.6
garydgregory 4bc40e5
Bump commons.pmd-impl.version from 6.47.0 to 6.48.0
dependabot[bot] 2b03765
Collections-824: Optimize SimpleHasher.forEachIndex and SimpleHasher …
Claudenw 55528c5
Collections 827: Add tests using or, and and xor with different lengt…
Claudenw 731c42f
Bump actions/cache from 3.0.6 to 3.0.7
garydgregory 05ec896
Collections-818: convert to characteristics flag (#329)
Claudenw f767cfd
Bump actions/cache from 3.0.7 to 3.0.8
garydgregory 403aeaf
COLLECTIONS-831: Add BloomFilter clear() method
Claudenw f4ae48f
Test code cleanup
aherbert 134303e
Removed obsolete file
aherbert 4c410fb
[COLLECTIONS-833] Intermittent failure in EmptyPropertiesTest
garydgregory 3c7a225
Bump maven-pmd-plugin from 3.17.0 to 3.18.0
garydgregory 333a2e9
Bump maven-checkstyle-plugin 3.1.0 to 3.2.0.
garydgregory 03f9f6d
Update README to use the default Maven goal
garydgregory 426a289
Use diamond operator
aherbert 0f701b7
Use private inner class
aherbert 9eb8e43
Add CodeQL badge
garydgregory 0118753
Fix typo independant -> independent
garydgregory 6720909
Javadoc @see tags do not need to use a FQCN for classes in java.lang
garydgregory 1538214
Javadoc @link tags do not need to use a FQCN for classes in java.lang
garydgregory a14e1e5
Formatting
aherbert 6e06ecd
Update code coverage badge and link
garydgregory d8938a7
Refer to master branch only and drop reference to trunk branch name
garydgregory db8c7d6
Use GAV coordinates Maven Central coordinates to make sure to pick up
garydgregory 54ef3fa
Rename type parameter
garydgregory 44cb1f5
Bump commons.pmd-impl.version from 6.48.0 to 6.49.0 (#333)
dependabot[bot] 5ea10a0
Bump commons.pmd-impl.version from 6.48.0 to 6.49.0 #333
garydgregory 46e7ea9
Bump actions/checkout from 3 to 3.0.2.
garydgregory 964695a
Bump maven-pmd-plugin from 3.18.0 to 3.19.0
dependabot[bot] ac30e1b
Collections-763: Remove BloomFilter constructors that create initial …
Claudenw 4d214b5
Fix typo
aherbert 98ce7ff
Simplify random indices tests
aherbert a294c35
Update IndexProducerTest to test the behaviour
aherbert 28078f7
Add same scorecards-analysis.yml as Apache Log4j (except for branches)
garydgregory f88867c
Add ossf/scorecard-action badge to readme
garydgregory c0a5ba3
Use GitHub cache for CodeQL
garydgregory 34c11c3
Bump commons.junit.version from 5.9.0 to 5.9.1 (#338)
dependabot[bot] 076f8ee
Bump commons.junit.version from 5.9.0 to 5.9.1 #338
garydgregory f88644c
Bump commons-parent from 53 to 54 (#339)
dependabot[bot] 33aa9d1
Bump commons-parent from 53 to 54 #339
garydgregory 663cf79
Fix flaky test failure in SynchronizedBagTest#testCollectionToArray2
Partha-SUST16 40fc4d1
Update test of unordered arrays
aherbert 08c3e11
Track changes
aherbert 0fe27a2
Drop Clirr property since commons-parent has replaced Clirr with
garydgregory 0d20bfa
Pick up Apache RAT 0.15 from commons-parent
garydgregory 1a21f3e
Bump japicmp-maven-plugin from 0.15.7 to 0.16.0
garydgregory d1e2412
Doc: Bump commons-parent from 53 to 54
garydgregory 1dc2d9c
Replace Travis-CI badge with GitHub Actions CI
garydgregory 24b8b2c
Bump actions/cache from 3.0.8 to 3.0.9 (#342)
dependabot[bot] a6355c2
Bump actions/cache from 3.0.8 to 3.0.9 #342
garydgregory 2ea29ab
Bump actions/setup-java from 3 to 3.5.1
garydgregory 673da2f
Fix flaky test failure in 'AbstractCollectionTest.testCollectionToArr…
Partha-SUST16 df2bfae
GitHub Actions build: no top level permission defined
garydgregory b1b8c65
Set to level permissions to 'read'
garydgregory 604c9bd
Bump actions/cache from 3.0.9 to 3.0.10
dependabot[bot] 6dbf920
Bump commons.pmd-impl.version from 6.49.0 to 6.50.0
dependabot[bot] 99bea64
Bump actions/checkout from 3.0.2 to 3.1.0
dependabot[bot] 59c73fd
Don't persist credentials unnecessarily
sebbASF 337e931
Bump actions/cache from 3.0.10 to 3.0.11 (#348)
dependabot[bot] 2e60e13
[COLLECTIONS-835] Fix links in javadoc and documentations
marcwrobel d524cf0
[COLLECTIONS-835] Add change log and credit
kinow 5283d38
[COLLECTIONS-814] CollectionUtils.removeAll() not throwing proper Nul…
angusdev d103b64
[COLLECTIONS-814] Add change log
kinow 48ebe7f
Bump actions/setup-java from 3.5.1 to 3.6.0
dependabot[bot] 2ab32ce
Bump easymock from 4.3 to 5.0.0 (#352)
dependabot[bot] 26168e5
Bump easymock from 4.3 to 5.0.0 #352
garydgregory 392c8cc
Bump Scorecards from 1 to 2
garydgregory 9278b83
Bump easymock from 5.0.0 to 5.0.1
dependabot[bot] 5bdd36e
Bump pmd from 6.50.0 to 6.51.0
garydgregory bdc488c
Bump actions/upload-artifact from 3.1.0 to 3.1.1 (#354)
dependabot[bot] cbb3877
Remove junit version managed in commons parent via junit BOM
aherbert 5ba3c3a
Remove junit-vintage-engine dependency
aherbert 0221d15
Make Hasher test classes package-private
aherbert f5b3dd7
COLLECTIONS-836: fix flaky test DualHashBidiMapTest (#353)
anantdahiya8 9b93933
Track changes
aherbert 3034107
Remove Comparable from the Shape class
aherbert 39bd0d9
Collections-834: Clarify Bloom filter BitCountProducer operation (#335)
Claudenw 36b56e7
Bloom filter code clean-up
aherbert 7434b20
Fixed typos originally reported in pull #323 (#349)
Claudenw bf9fc25
(chore) Fix minor typos (#323)
marcwrobel c07500f
Fix minor typos #323
garydgregory 687480d
Add @SuppressWarnings and tweak formatting
garydgregory eefba11
Add missing @Override
garydgregory 5186ccb
Simpler lambdas
garydgregory fbb34d3
Use Java 5 for each loop
garydgregory bf230c3
Use lambdas
garydgregory 490b6af
Call to super is redundant
garydgregory fef5a26
These test classes can be static
garydgregory 970a09f
PMD: Use direct access
garydgregory 955352e
Simplify ignorance of caught exception
garydgregory e360f3b
PMD: Use direct access
garydgregory 1845fe8
PMD: No need to nest
garydgregory a023de1
PMD: AbstractReferenceMap.SoftRef implements hashCode() but not equals()
garydgregory 659b840
PMD: AbstractReferenceMap.WeakRef implements hashCode() but not equals()
garydgregory 5b12651
Javadoc
garydgregory ebae8a3
Remove unnecessary parens
garydgregory 0496277
Enable PMD check
garydgregory 7170bf8
Format tweak
garydgregory f9333ca
Javadocs and comments
garydgregory 7c848e3
Use method reference
aherbert 6024a71
Whitespace formatting
aherbert 2b8301e
Collections-837: Remove HasherCollection
Claudenw 31de544
Fix flaky test in CollectionUtilsTest (#360)
yannizhou05 067fedd
Add GHitHub robots file
garydgregory 0a7395c
Bump pmd from 6.51.0 to 6.52.0
garydgregory 3cb83a8
Sort imports
garydgregory 981de22
Reuse constants
garydgregory 297ae00
Inherit PMD plugin configuration of PMD from parent POM
garydgregory 19ed3d4
Use Java style array decelerations (#362)
arturobernalg 9c16067
Update changes.xml
garydgregory 11577d0
Collections-817: Update estimateN, estimateIntersection and
Claudenw 0cdae4d
Javadoc comment typo
aherbert efc1065
Remove empty line
aherbert 1c87346
Remove public keyword from package level test members
aherbert 8bfe546
Collections-817: Update estimateN, estimateIntersection and
aherbert 32c6677
Bump actions/setup-java from 3.6.0 to 3.7.0 #126
garydgregory 9ca64a9
Undo Bump actions/setup-java from 3.6.0 to 3.7.0
garydgregory 8bc78fb
Bump actions/setup-java from 3.6.0 to 3.8.0 (#364)
dependabot[bot] 090aa99
Bump actions/checkout from 3.1.0 to 3.2.0 (#365)
dependabot[bot] dd80f9d
Bump ossf/scorecard-action from 2.0.6 to 2.1.0 (#366)
dependabot[bot] 065a09f
Bump actions/setup-java from 3.8.0 to 3.9.0
dependabot[bot] ea89e40
Bump commons-parent from 54 to 55 (#368)
dependabot[bot] 1527ca8
Bump commons-parent from 54 to 55 #368
garydgregory 0b3529c
Bump ossf/scorecard-action from 2.1.0 to 2.1.2 (#369)
dependabot[bot] a9f5c7f
Bump actions/cache from 3.0.11 to 3.2.1 (#370)
dependabot[bot] ad16709
COLLECTIONS-806: Remove use of JUnit4 junit.framework.Test class (#371)
pas725 3ae2668
Remove use of JUnit4 junit.framework.Test class #371
garydgregory 70f14fc
Use Arrays.copyOf()
garydgregory d673b06
Bump actions/cache from 3.2.1 to 3.2.2 (#372)
dependabot[bot] c31bf9c
Update NOTICE to 2023
garydgregory dbab6e2
Bump commons-parent from 55 to 56 (#373)
dependabot[bot] 126185e
Bump commons-parent from 55 to 56 #373
garydgregory 1340b16
Bump easymock from 5.0.1 to 5.1.0 (#375)
dependabot[bot] b25f1a4
Bump easymock from 5.0.1 to 5.1.0 #375
garydgregory e136aa3
Bump actions/checkout from 3.2.0 to 3.3.0 (#376)
dependabot[bot] 22a6b59
Pick up JUnit version from parent POM
garydgregory da1083e
Pick up JaCoCo version from parent POM
garydgregory d80a5a7
Pick up maven-pmd-plugin version from parent POM
garydgregory 14234ba
Pick up japicmp-maven-plugin version from parent POM
garydgregory c1cae17
Pick up pmd version from parent POM
garydgregory 78863e3
Bump actions/cache from 3.2.2 to 3.2.3 (#377)
dependabot[bot] 791cf80
Bump actions/upload-artifact from 3.1.1 to 3.1.2 (#378)
dependabot[bot] 10b1292
Pick up maven-checkstyle-plugin version from parent
garydgregory 7996552
Javadoc fixes
garydgregory fe239b9
Bump maven-checkstyle-plugin from 3.2.0 to 3.2.1
dependabot[bot] e82c795
Pick up maven-checkstyle-plugin version from parent
garydgregory 64d8c65
POM project name should contain "Apache" prefix
garydgregory 8363328
Fix typos
garydgregory 19775dc
Fix spelling
garydgregory ed39e80
Add homepage and standard notifications
sebbASF 5cbf81a
Pick up Checkstyle version from parent POM
garydgregory 4e70a00
codecov-commenter => notifications
sebbASF 19edcb5
Bump actions/cache from 3.2.3 to 3.2.5
dependabot[bot] 94f0514
Bump actions/setup-java from 3.9.0 to 3.10.0
dependabot[bot] 3e8bc92
Remove unused
garydgregory 59f4fa3
Bump actions/cache from 3.2.5 to 3.2.6 (#383)
dependabot[bot] cc1f04b
Bump actions/cache from 3.2.6 to 3.3.0
dependabot[bot] 7797a45
Use XSD
garydgregory f2c8b42
Javadoc
garydgregory ea1c58e
Bump actions/checkout from 3.3.0 to 3.4.0
dependabot[bot] 3fce696
Bump actions/cache from 3.3.0 to 3.3.1 (#386)
dependabot[bot] 548c445
Bump actions/checkout from 3.4.0 to 3.5.0
dependabot[bot] 7af91ee
Javadoc: Remove extra "whitespace"
garydgregory 68e3ff1
Javadoc/Comments: Remove extra "whitespace"
garydgregory 5acb65c
Bump actions/setup-java from 3.10.0 to 3.11.0 (#389)
dependabot[bot] 00239f3
Bump ossf/scorecard-action from 2.1.2 to 2.1.3 (#388)
dependabot[bot] 2b59ddb
Bump actions/checkout from 3.5.0 to 3.5.2
dependabot[bot] 4cc5c37
Format: "catch(" -> "catch ("
garydgregory 6538189
[COLLECTIONS-839] migrate all tests to use junit5 assert and BulkTest…
samabcde d42c87e
[COLLECTIONS-839] Migrate Map tests to JUnit 5 assertions #391
garydgregory b2bf4e3
Organize imports
garydgregory 7874b21
Bump commons-parent from 56 to 57 (#393)
dependabot[bot] ebbd286
Bump commons-parent from 56 to 57 #393
garydgregory 94cdbec
Bump tests from commons-io:commons-io 2.11.0 to 2.12.0
garydgregory c00e682
Javadoc typos
garydgregory 638693c
Javadoc typos
garydgregory c20b777
In-line comment and messages typos
garydgregory 5db2642
Grammar
garydgregory 05549e0
Bump commons-parent from 57 to 58
garydgregory b912b23
Bump guava-testlib from 31.1-jre to 32.0.0-jre (#394)
dependabot[bot] b45ad94
Bump guava-testlib from 31.1-jre to 32.0.0-jre #394.
garydgregory 83c8ee2
Re-categorize some entries
garydgregory 08efcd4
Bump commons-io from 2.12.0 to 2.13.0
garydgregory 8de9296
Bump guava-testlib from 32.0.0-jre to 32.0.1-jre (#395)
dependabot[bot] 67cb7f7
Bump guava-testlib from 32.0.0-jre to 32.0.1-jre #395
garydgregory 633708b
Javadoc
aherbert ba1e44e
Move EnhancedDoubleHasher.mod() to a public BitMap API (#396)
Claudenw 9ac6788
Move mod tests to BitMapTest
aherbert bf5cefd
Update BitMap.mod javadoc
aherbert 69f9a27
Bump actions/checkout from 3.5.2 to 3.5.3
dependabot[bot] c051326
Changes required to open testing to new implementations. (#398)
Claudenw f425667
[COLLECTIONS-841] Open up bloom filter tests - test changes to suppor…
Claudenw 2b7f1f0
Bump commons-codec:commons-codec: from 1.15 to 1.16.0
garydgregory 88b5e65
[StepSecurity] ci: Harden GitHub Actions (#401)
step-security-bot f3a18bd
[StepSecurity] ci: Harden GitHub Actions #401
garydgregory c6aee80
Bump ossf/scorecard-action from 2.1.3 to 2.2.0 (#403)
dependabot[bot] d777afd
Bump guava-testlib from 32.0.1-jre to 32.1.0-jre (#404)
dependabot[bot] 9fec5f4
Bump guava-testlib from 32.0.1-jre to 32.1.0-jre #404
garydgregory 08fa869
Throw a RuntimeException subclass instead of RuntimeException
garydgregory 37a1dc2
Throw a RuntimeException subclass instead of an Error
garydgregory 97dfed3
Bump guava-testlib from 32.1.0-jre to 32.1.1-jre (#405)
dependabot[bot] fac9baa
Bump guava-testlib from 32.1.0-jre to 32.1.1-jre #405
garydgregory b459495
Add missing entry
garydgregory 170a776
Add org.apache.commons.collections4.properties.OrderedProperties and
garydgregory 46a364d
Support entrySet() and forEach()
garydgregory b88df81
Disable testToString for now
garydgregory 4104913
Must override toString() for Java 17
garydgregory 21e2208
Remove unused import
garydgregory 782d3bb
Make AbstractPatriciaTrie public (#407)
vad0 c91927a
Make AbstractPatriciaTrie public #407
garydgregory cd33766
Bump commons-parent from 58 to 59
garydgregory bedb515
Bump actions/setup-java from 3.11.0 to 3.12.0 (#408)
dependabot[bot] 6d765ac
Bump commons-lang3 from 3.12.0 to 3.13.0
garydgregory f12c3af
Bump commons-lang3 from 3.12.0 to 3.13.0
garydgregory b6e7ac2
fixes misplaced symbols in javadoc (mainly in Hasher, CountingLongPre…
mawiesne 23ce3d3
Fix punctuation and minor Javadoc issues #409
garydgregory a30ac76
(doc) Fix grammatical error in Javadoc
syoon2 c01d3dd
Bump com.google.guava:guava-testlib from 32.1.1-jre to 32.1.2-jre (#411)
dependabot[bot] e7b796d
Bump guava-testlib from 31.1-jre to 32.1.2-jre #394, #395, #404…
garydgregory 45af3cc
Fix JUNIT-5 update on master
anantdamle dd42aa7
Merge remote-tracking branch 'origin/collections_795' into collection…
anantdamle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
176 changes: 176 additions & 0 deletions
176
src/main/java/org/apache/commons/collections4/iterators/ZippedTupleIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
* 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.commons.collections4.iterators; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import org.apache.commons.collections4.iterators.ZippedTupleIterator.ZippedTuple; | ||
|
||
/** | ||
* Provides a iteration over the elements contained in a pair of Iterators. | ||
* | ||
* <p> | ||
* Given two {@link Iterator} instances {@code A} and {@code B}, the {@link #next} method on this | ||
* iterator provide a Pair of {@code A.next()} and {@code B.next()} until one of the iterators is | ||
* exhausted. | ||
* </p> | ||
* Example usage: | ||
* <pre>{@code | ||
* List<Integer> studentIds = ... | ||
* List<String> studentNames = ... | ||
* | ||
* ZippedTupleIterator<ZippedTuple<Integer, String>> zippedIterator = | ||
* ZippedTupleIterator.ofIterables(studentIds, studentNames); | ||
* | ||
* while (zippedIterator.hasNext()) { | ||
* ZippedTuple<Integer, String> item = zippedIterator.next(); | ||
* ... | ||
* } | ||
* }</pre> | ||
* | ||
* @param <L> the left elements' type | ||
* @param <R> the right elements' type | ||
*/ | ||
public class ZippedTupleIterator<L, R> implements Iterator<ZippedTuple<L, R>> { | ||
|
||
/** | ||
* The left {@link Iterator}s to evaluate. | ||
*/ | ||
private final Iterator<L> leftIterator; | ||
|
||
/** | ||
* The right {@link Iterator}s to evaluate. | ||
*/ | ||
private final Iterator<R> rightIterator; | ||
|
||
// Constructor | ||
// ---------------------------------------------------------------------- | ||
|
||
/** | ||
* Constructs a new {@code ZipPairIterator} that will provide iteration over the two given | ||
* iterators. | ||
* | ||
* @param leftIterator the iterator for the left side element. | ||
* @param rightIterator the iterator for the right side element. | ||
* @throws NullPointerException if either iterator is null | ||
*/ | ||
public ZippedTupleIterator(Iterator<L> leftIterator, Iterator<R> rightIterator) { | ||
this.leftIterator = requireNonNull(leftIterator); | ||
this.rightIterator = requireNonNull(rightIterator); | ||
} | ||
|
||
/** | ||
* Convenience static factory to construct the ZipPairIterator | ||
* | ||
* @param leftIterator the iterator for the left side element. | ||
* @param rightIterator the iterator for the right side element. | ||
* @return the iterator to iterate over the provided iterators. | ||
* @throws NullPointerException if either iterator is null | ||
*/ | ||
public static <L, R> ZippedTupleIterator<L, R> of(Iterator<L> leftIterator, | ||
Iterator<R> rightIterator) { | ||
return new ZippedTupleIterator<>(leftIterator, rightIterator); | ||
} | ||
|
||
/** | ||
* Convenience static factory to construct the ZipPairIterator from any {@link Iterable} sources. | ||
* | ||
* @param leftIterable the iterable for the left side element. | ||
* @param rightIterable the iterable for the right side element. | ||
* @return the iterator to iterate over the iterators derived from the provided iterables. | ||
* @throws NullPointerException if either iterables is null | ||
*/ | ||
public static <L, R> ZippedTupleIterator<L, R> ofIterables(Iterable<L> leftIterable, | ||
Iterable<R> rightIterable) { | ||
return of(requireNonNull(leftIterable).iterator(), requireNonNull(rightIterable).iterator()); | ||
} | ||
|
||
// Iterator Methods | ||
// ------------------------------------------------------------------- | ||
|
||
/** | ||
* An immutable tuple class to represent elements from both the iterators. | ||
* | ||
* @param <L> the left elements' type | ||
* @param <R> the right elements' type | ||
*/ | ||
public static final class ZippedTuple<L, R> { | ||
|
||
private final L leftItem; | ||
|
||
private final R rightItem; | ||
|
||
private ZippedTuple(L leftItem, R rightItem) { | ||
this.leftItem = leftItem; | ||
this.rightItem = rightItem; | ||
} | ||
|
||
/** | ||
* Convenience static factory method to construct the tuple pair. | ||
* | ||
* @param left the left element | ||
* @param right the right element | ||
* @return the Immutable tuple pair of two elements. | ||
*/ | ||
private static <L, R> ZippedTuple<L, R> of(L left, R right) { | ||
return new ZippedTuple<>(left, right); | ||
} | ||
|
||
public L getLeftItem() { | ||
return leftItem; | ||
} | ||
|
||
public R getRightItem() { | ||
return rightItem; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("{%s, %s}", leftItem, rightItem); | ||
} | ||
} | ||
|
||
// Iterator Methods | ||
// ------------------------------------------------------------------- | ||
|
||
/** | ||
* Returns {@code true} if both the child iterators have remaining elements. | ||
* | ||
* @return true if both the child iterators have remaining elements | ||
*/ | ||
@Override | ||
public boolean hasNext() { | ||
return leftIterator.hasNext() && rightIterator.hasNext(); | ||
} | ||
|
||
/** | ||
* Returns the next elements from both the child iterators. | ||
* | ||
* @return the next elements from both the iterators. | ||
* @throws NoSuchElementException if any one child iterator is exhausted. | ||
*/ | ||
@Override | ||
public ZippedTuple<L, R> next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
|
||
return ZippedTuple.of(leftIterator.next(), rightIterator.next()); | ||
} | ||
} |
150 changes: 150 additions & 0 deletions
150
src/test/java/org/apache/commons/collections4/iterators/ZippedTupleIteratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.commons.collections4.iterators; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
import org.apache.commons.collections4.IteratorUtils; | ||
import org.apache.commons.collections4.iterators.ZippedTupleIterator.ZippedTuple; | ||
|
||
|
||
/** Unit test suite for {@link ZippingIterator}. */ | ||
public final class ZippedTupleIteratorTest | ||
extends AbstractIteratorTest<ZippedTuple<String, Integer>> { | ||
|
||
//------------------------------------------------------------ Conventional | ||
|
||
public ZippedTupleIteratorTest(String testName) { | ||
super(testName); | ||
} | ||
|
||
//------------------------------------------------------------ Lifecycle | ||
|
||
private ArrayList<String> smallStringsList = null; | ||
private ArrayList<String> largeStringsList = null; | ||
private ArrayList<Integer> smallIntsList = null; | ||
private ArrayList<Integer> largeIntsList = null; | ||
|
||
// Unequal sized lists | ||
private static final int SMALL_LIST_SIZE = 20; | ||
private static final int LARGE_LIST_SIZE = 40; | ||
|
||
@Override | ||
protected void setUp() throws Exception { | ||
super.setUp(); | ||
|
||
smallStringsList = new ArrayList<>(); | ||
largeStringsList = new ArrayList<>(); | ||
smallIntsList = new ArrayList<>(); | ||
largeIntsList = new ArrayList<>(); | ||
|
||
Random random = new Random(); | ||
|
||
for (int i = 0; i < SMALL_LIST_SIZE; i++) { | ||
smallIntsList.add(random.nextInt()); | ||
smallStringsList.add(UUID.randomUUID().toString()); | ||
} | ||
|
||
for (int i = 0; i < LARGE_LIST_SIZE; i++) { | ||
largeIntsList.add(random.nextInt()); | ||
largeStringsList.add(UUID.randomUUID().toString()); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean supportsRemove() { | ||
return false; | ||
} | ||
|
||
//---------------------------------------------------- TestIterator Methods | ||
|
||
@Override | ||
public Iterator<ZippedTuple<String, Integer>> makeEmptyIterator() { | ||
return ZippedTupleIterator.of(IteratorUtils.emptyIterator(), IteratorUtils.emptyIterator()); | ||
} | ||
|
||
@Override | ||
public Iterator<ZippedTuple<String, Integer>> makeObject() { | ||
return ZippedTupleIterator.of(smallStringsList.iterator(), smallIntsList.iterator()); | ||
} | ||
|
||
public void testLeftIteratorLargerThanRight() { | ||
Iterator<ZippedTuple<String, Integer>> zipPairIterator = | ||
ZippedTupleIterator.ofIterables(largeStringsList, smallIntsList); | ||
|
||
|
||
for (int i = 0; i < SMALL_LIST_SIZE; i++) { | ||
assertTrue(zipPairIterator.hasNext()); | ||
ZippedTuple<String, Integer> zippedItem = zipPairIterator.next(); | ||
|
||
assertEquals(largeStringsList.get(i) , zippedItem.getLeftItem()); | ||
assertEquals(smallIntsList.get(i) , zippedItem.getRightItem()); | ||
} | ||
|
||
assertFalse(zipPairIterator.hasNext()); | ||
} | ||
|
||
public void testRightIteratorLargerThanLeft() { | ||
Iterator<ZippedTuple<String, Integer>> zipPairIterator = | ||
ZippedTupleIterator.ofIterables(smallStringsList, largeIntsList); | ||
|
||
|
||
for (int i = 0; i < SMALL_LIST_SIZE; i++) { | ||
assertTrue(zipPairIterator.hasNext()); | ||
ZippedTuple<String, Integer> zippedItem = zipPairIterator.next(); | ||
|
||
assertEquals(smallStringsList.get(i) , zippedItem.getLeftItem()); | ||
assertEquals(largeIntsList.get(i) , zippedItem.getRightItem()); | ||
} | ||
|
||
assertFalse(zipPairIterator.hasNext()); | ||
} | ||
|
||
public void testEmptyLeftIterator() { | ||
Iterator<ZippedTuple<String, Integer>> zipPairIterator = | ||
ZippedTupleIterator.of(IteratorUtils.emptyIterator(), largeIntsList.iterator()); | ||
|
||
assertFalse(zipPairIterator.hasNext()); | ||
} | ||
|
||
public void testEmptyRightIterator() { | ||
Iterator<ZippedTuple<String, Integer>> zipPairIterator = | ||
ZippedTupleIterator.of(largeStringsList.iterator(), IteratorUtils.emptyIterator()); | ||
|
||
assertFalse(zipPairIterator.hasNext()); | ||
} | ||
|
||
|
||
public void testValidTupleString() { | ||
Iterator<ZippedTuple<String, Integer>> zipPairIterator = | ||
ZippedTupleIterator.ofIterables(smallStringsList, largeIntsList); | ||
|
||
|
||
for (int i = 0; i < SMALL_LIST_SIZE; i++) { | ||
assertTrue(zipPairIterator.hasNext()); | ||
ZippedTuple<String, Integer> zippedItem = zipPairIterator.next(); | ||
|
||
assertEquals( | ||
String.format("{%s, %s}", zippedItem.getLeftItem(), zippedItem.getRightItem()), | ||
zippedItem.toString()); | ||
} | ||
|
||
assertFalse(zipPairIterator.hasNext()); | ||
} | ||
} | ||
anantdamle marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this prefixed with "zip" when it has nothing to do with zip files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is related to zipper. Like Python stdlib's zip function. Scala and Clojure also have a similar function I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe but the prefix here is "zipped" not "zipper", terrible name IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not good naming things, so can't really suggest a better one. The code example in the Python docs uses
zipped
too for the variable that contains the tuples returned withzip
(that were zipped), so that should sound OK for users coming from Python (or JS using Lodash's zip/unzip which I'm using in my current project). But if that's confusing for other users, we can try to think in a better name I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use
PairedIterator
andPairedIterable
names. Hope is acceptable to all.