Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 29, 2018
2 parents befb49b + 5230973 commit bfccf2f
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/main/java/org/cactoos/iterator/SyncIterator.java
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();
}
}
}
76 changes: 76 additions & 0 deletions src/test/java/org/cactoos/iterator/SyncIteratorTest.java
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"})
);
}

}

1 comment on commit bfccf2f

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on bfccf2f Jan 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 482-5994e3af discovered in src/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, but
we discovered it only now.

Please sign in to comment.