From 81f0f42f2ddb087a1748f2e2141a127ebf38c858 Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Sun, 11 Oct 2015 13:48:40 +0900 Subject: CustomListAdapter: synchronized ブロックの代わりにメソッドに synchronized キーワードを付けた MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../view/adapter/CustomListAdapter.java | 9 +---- .../view/adapter/OrderedCustomListAdapter.java | 40 +++++++------------ .../view/adapter/StatusListAdapter.java | 18 ++++----- .../view/adapter/UnorderedCustomListAdapter.java | 46 ++++++++-------------- 4 files changed, 42 insertions(+), 71 deletions(-) (limited to 'app/src/main/java/net/lacolaco/smileessence/view/adapter') diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.java b/app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.java index ba888142..ab89189e 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.java @@ -13,7 +13,6 @@ public abstract class CustomListAdapter extends BaseAdapte // ------------------------------ FIELDS ------------------------------ - protected final Object LOCK = new Object(); private boolean isNotifiable = true; private List frozenList = new ArrayList<>(); private Activity activity; @@ -27,15 +26,11 @@ public abstract class CustomListAdapter extends BaseAdapte // --------------------- GETTER / SETTER METHODS --------------------- public final boolean isNotifiable() { - synchronized (LOCK) { - return isNotifiable; - } + return isNotifiable; } public final void setNotifiable(boolean notifiable) { - synchronized (LOCK) { - isNotifiable = notifiable; - } + isNotifiable = notifiable; } // --------------------- Interface BaseAdapter --------------------- diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/OrderedCustomListAdapter.java b/app/src/main/java/net/lacolaco/smileessence/view/adapter/OrderedCustomListAdapter.java index 5e46e088..24ccc997 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/adapter/OrderedCustomListAdapter.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/adapter/OrderedCustomListAdapter.java @@ -56,40 +56,30 @@ public class OrderedCustomListAdapter extends C // -------------------------- OTHER METHODS -------------------------- - public void addItem(T item) { - synchronized (LOCK) { - treeMap.put(item.getId(), item); - } + public synchronized void addItem(T item) { + treeMap.put(item.getId(), item); } - public void addItems(List items) { - synchronized (LOCK) { - for (T item : items) { - treeMap.put(item.getId(), item); - } + public synchronized void addItems(List items) { + for (T item : items) { + treeMap.put(item.getId(), item); } } - public void clear() { - synchronized (LOCK) { - treeMap.clear(); - } + public synchronized void clear() { + treeMap.clear(); } - public T removeItem(T item) { - synchronized (LOCK) { - return treeMap.remove(item.getId()); - } + public synchronized T removeItem(T item) { + return treeMap.remove(item.getId()); } - public int removeItemById(long id) { - synchronized (LOCK) { - T item = treeMap.remove(id); - if (item == null) { - return 0; - } else { - return 1; - } + public synchronized int removeItemById(long id) { + T item = treeMap.remove(id); + if (item == null) { + return 0; + } else { + return 1; } } } diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/StatusListAdapter.java b/app/src/main/java/net/lacolaco/smileessence/view/adapter/StatusListAdapter.java index b9f7929d..5fda8d56 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/adapter/StatusListAdapter.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/adapter/StatusListAdapter.java @@ -57,17 +57,15 @@ public class StatusListAdapter extends OrderedCustomListAdapter // -------------------------- OTHER METHODS -------------------------- @Override - public int removeItemById(long statusID) { - synchronized (LOCK) { - int count = 0; - count += super.removeItemById(statusID); - Tweet t = Tweet.fetch(statusID); - if (t != null) { - for (long retweetId : t.getRetweets().values()) { - count += super.removeItemById(retweetId); - } + public synchronized int removeItemById(long statusID) { + int count = 0; + count += super.removeItemById(statusID); + Tweet t = Tweet.fetch(statusID); + if (t != null) { + for (long retweetId : t.getRetweets().values()) { + count += super.removeItemById(retweetId); } - return count; } + return count; } } diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/UnorderedCustomListAdapter.java b/app/src/main/java/net/lacolaco/smileessence/view/adapter/UnorderedCustomListAdapter.java index 8e72cc80..2afc0316 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/adapter/UnorderedCustomListAdapter.java +++ b/app/src/main/java/net/lacolaco/smileessence/view/adapter/UnorderedCustomListAdapter.java @@ -50,48 +50,36 @@ public class UnorderedCustomListAdapter extends CustomList // -------------------------- OTHER METHODS -------------------------- - public void addItemToTop(T item) { - synchronized (LOCK) { - if (list.contains(item)) { - list.add(0, item); - } + public synchronized void addItemToTop(T item) { + if (!list.contains(item)) { + list.add(0, item); } } - public void addItemsToTop(List items) { - synchronized (LOCK) { - ListIterator iterator = items.listIterator(items.size()); - while (iterator.hasPrevious()) { - addItemToTop(iterator.previous()); - } + public synchronized void addItemsToTop(List items) { + ListIterator iterator = items.listIterator(items.size()); + while (iterator.hasPrevious()) { + addItemToTop(iterator.previous()); } } - public void addItemToBottom(T item) { - synchronized (LOCK) { - if (list.contains(item)) { - list.add(item); - } + public synchronized void addItemToBottom(T item) { + if (!list.contains(item)) { + list.add(item); } } - public void addItemsToBottom(List items) { - synchronized (LOCK) { - for (T item : items) { - addItemToBottom(item); - } + public synchronized void addItemsToBottom(List items) { + for (T item : items) { + addItemToBottom(item); } } - public void clear() { - synchronized (LOCK) { - list.clear(); - } + public synchronized void clear() { + list.clear(); } - public boolean removeItem(T item) { - synchronized (LOCK) { - return list.remove(item); - } + public synchronized boolean removeItem(T item) { + return list.remove(item); } } -- cgit v1.2.3