修改类名。

dev
ZhouXY108 2024-03-02 16:36:52 +08:00
parent c472050d00
commit e5c2ba99c3
1 changed files with 25 additions and 20 deletions

View File

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