diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/LockedTable.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/ReadWriteLockedTable.java similarity index 76% rename from src/main/java/xyz/zhouxy/plusone/commons/collection/LockedTable.java rename to src/main/java/xyz/zhouxy/plusone/commons/collection/ReadWriteLockedTable.java index bc5a4b1..240ce52 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/LockedTable.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/ReadWriteLockedTable.java @@ -6,6 +6,8 @@ import com.google.common.collect.Table; import xyz.zhouxy.plusone.commons.annotation.ReaderMethod; import xyz.zhouxy.plusone.commons.annotation.WriterMethod; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; import javax.annotation.concurrent.ThreadSafe; import java.util.Collection; import java.util.Map; @@ -31,43 +33,44 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; * @author ZhouXY * @see Table * @see ImmutableTable + * @see ReentrantReadWriteLock * @since 0.1.0-SNAPSHOT */ @Beta @ThreadSafe -public class LockedTable implements Table { +public class ReadWriteLockedTable implements Table { private final Table table; private final ReentrantReadWriteLock.ReadLock readLock; private final ReentrantReadWriteLock.WriteLock writeLock; - private LockedTable(Table table, boolean fair) { + private ReadWriteLockedTable(Table table, boolean fair) { this.table = Objects.requireNonNull(table); ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(fair); this.readLock = rwl.readLock(); this.writeLock = rwl.writeLock(); } - public static LockedTable of(Table table) { - if (table instanceof LockedTable) { - return (LockedTable) table; + public static ReadWriteLockedTable of(Table table) { + if (table instanceof ReadWriteLockedTable) { + return (ReadWriteLockedTable) table; } else { - return new LockedTable<>(table, false); + return new ReadWriteLockedTable<>(table, false); } } - public static LockedTable of(Table table, boolean fair) { - if (table instanceof LockedTable) { - return (LockedTable) table; + public static ReadWriteLockedTable of(Table table, boolean fair) { + if (table instanceof ReadWriteLockedTable) { + return (ReadWriteLockedTable) table; } else { - return new LockedTable<>(table, fair); + return new ReadWriteLockedTable<>(table, fair); } } @Override @ReaderMethod - public boolean contains(Object rowKey, Object columnKey) { + public boolean contains(@CheckForNull @Nonnull Object rowKey, @CheckForNull @Nonnull Object columnKey) { readLock.lock(); try { return this.table.contains(rowKey, columnKey); @@ -78,7 +81,7 @@ public class LockedTable implements Table { @Override @ReaderMethod - public boolean containsRow(Object rowKey) { + public boolean containsRow(@CheckForNull @Nonnull Object rowKey) { readLock.lock(); try { return this.table.containsRow(rowKey); @@ -89,7 +92,7 @@ public class LockedTable implements Table { @Override @ReaderMethod - public boolean containsColumn(Object columnKey) { + public boolean containsColumn(@CheckForNull @Nonnull Object columnKey) { readLock.lock(); try { return this.table.containsColumn(columnKey); @@ -100,7 +103,7 @@ public class LockedTable implements Table { @Override @ReaderMethod - public boolean containsValue(Object value) { + public boolean containsValue(@CheckForNull @Nonnull Object value) { readLock.lock(); try { return this.table.containsValue(value); @@ -111,7 +114,7 @@ public class LockedTable implements Table { @Override @ReaderMethod - public V get(Object rowKey, Object columnKey) { + public V get(@CheckForNull @Nonnull Object rowKey, @CheckForNull @Nonnull Object columnKey) { readLock.lock(); try { return this.table.get(rowKey, columnKey); @@ -155,7 +158,9 @@ public class LockedTable implements Table { @Override @WriterMethod - public V put(R rowKey, C columnKey, V value) { + public V put(@CheckForNull @Nonnull R rowKey, + @CheckForNull @Nonnull C columnKey, + @CheckForNull @Nonnull V value) { writeLock.lock(); try { return this.table.put(rowKey, columnKey, value); @@ -166,7 +171,7 @@ public class LockedTable implements Table { @Override @WriterMethod - public void putAll(Table table) { + public void putAll(@Nonnull Table table) { writeLock.lock(); try { this.table.putAll(table); @@ -177,7 +182,7 @@ public class LockedTable implements Table { @Override @WriterMethod - public V remove(Object rowKey, Object columnKey) { + public V remove(@CheckForNull @Nonnull Object rowKey, @CheckForNull @Nonnull Object columnKey) { writeLock.lock(); try { return this.table.remove(rowKey, columnKey); @@ -188,7 +193,7 @@ public class LockedTable implements Table { @Override @ReaderMethod - public Map row(R rowKey) { + public Map row(@CheckForNull @Nonnull R rowKey) { readLock.lock(); try { return this.table.row(rowKey); @@ -199,7 +204,7 @@ public class LockedTable implements Table { @Override @ReaderMethod - public Map column(C columnKey) { + public Map column(@CheckForNull @Nonnull C columnKey) { readLock.lock(); try { return this.table.column(columnKey);