-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
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,90 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2018 Yegor Bugayenko | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.cactoos.iterator; | ||
|
||
import java.util.Iterator; | ||
import java.util.concurrent.locks.ReadWriteLock; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
||
/** | ||
* Synchronized {@link Iterator} implementation using a {@link ReadWriteLock} | ||
* either provided to the constructor or an internally created | ||
* {@link ReentrantReadWriteLock}. | ||
* The {@link ReadWriteLock} is used to synchronize read calls to | ||
* {@link SyncIterator#hasNext()} against write calls to | ||
* {@link SyncIterator#next()} and write calls to any other read or write | ||
* calls. | ||
* @author Sven Diedrichsen (sven.diedrichsen@gmail.com) | ||
* @version $Id$ | ||
* @param <T> The type of the iterator. | ||
* @since 1.0 | ||
*/ | ||
public final class SyncIterator<T> implements Iterator<T> { | ||
/** | ||
* The original iterator. | ||
*/ | ||
private final Iterator<T> iterator; | ||
/** | ||
* The lock to use. | ||
*/ | ||
private final ReadWriteLock lock; | ||
|
||
/** | ||
* Ctor. | ||
* @param iterator The iterator to synchronize access to. | ||
*/ | ||
public SyncIterator(final Iterator<T> iterator) { | ||
this(iterator, new ReentrantReadWriteLock()); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param iterator The iterator to synchronize access to. | ||
* @param lock The lock to use for synchronization. | ||
*/ | ||
public SyncIterator(final Iterator<T> iterator, final ReadWriteLock lock) { | ||
this.iterator = iterator; | ||
this.lock = lock; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
this.lock.readLock().lock(); | ||
try { | ||
return this.iterator.hasNext(); | ||
} finally { | ||
this.lock.readLock().unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public T next() { | ||
this.lock.writeLock().lock(); | ||
try { | ||
return this.iterator.next(); | ||
} finally { | ||
this.lock.writeLock().unlock(); | ||
} | ||
} | ||
} |
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,76 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2018 Yegor Bugayenko | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.cactoos.iterator; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import org.cactoos.list.ListOf; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
|
||
// @todo #482:30min add multi-threaded tests which test that the lock syncs the | ||
// access to the next method against next and hasNext calls and calls to the | ||
// hasNext method against next calls. | ||
/** | ||
* Test for {@link SyncIterator}. | ||
* | ||
* @author Sven Diedrichsen (sven.diedrichsen@gmail.com) | ||
* @version $Id$ | ||
* @since 1.0 | ||
* @checkstyle JavadocMethodCheck (500 lines) | ||
* @checkstyle MagicNumberCheck (500 lines) | ||
* @checkstyle TodoCommentCheck (500 lines) | ||
*/ | ||
@SuppressWarnings("PMD.AvoidDuplicateLiterals") | ||
public final class SyncIteratorTest { | ||
|
||
@Test | ||
public void syncIteratorReturnsCorrectValuesWithExternalLock() { | ||
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); | ||
MatcherAssert.assertThat( | ||
"Unexpected value found.", | ||
new ListOf<>( | ||
new SyncIterator<>( | ||
Arrays.asList("a", "b").iterator(), lock | ||
) | ||
).toArray(), | ||
Matchers.equalTo(new Object[]{"a", "b"}) | ||
); | ||
} | ||
|
||
@Test | ||
public void syncIteratorReturnsCorrectValuesWithInternalLock() { | ||
MatcherAssert.assertThat( | ||
"Unexpected value found.", | ||
new ListOf<>( | ||
new SyncIterator<>( | ||
Arrays.asList("a", "b").iterator() | ||
) | ||
).toArray(), | ||
Matchers.equalTo(new Object[]{"a", "b"}) | ||
); | ||
} | ||
|
||
} |
bfccf2f
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.
Puzzle
482-5994e3af
discovered insrc/test/java/org/cactoos/iterator/SyncIteratorTest.java
and submitted as #585. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, butwe discovered it only now.