From f4c26d9cc7e084547e32913a825a67cf5cd2c09e Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Wed, 16 Nov 2016 16:19:47 +0000 Subject: [PATCH] Remove unnecessary unmodifiable collection wrappers. --- .../android/util/AndroidUtils.java | 3 +- .../plugins/droidtooth/DroidtoothPlugin.java | 8 +++-- .../briarproject/blogs/BlogManagerImpl.java | 4 +-- .../clients/ClientHelperImpl.java | 5 ++- .../briarproject/clients/MessageTreeImpl.java | 2 +- .../db/DatabaseComponentImpl.java | 4 +-- .../briarproject/forum/ForumManagerImpl.java | 2 +- .../plugins/ConnectionRegistryImpl.java | 7 ++++- .../plugins/PluginManagerImpl.java | 8 ++--- .../TransportPropertyManagerImpl.java | 5 ++- .../sharing/SharingManagerImpl.java | 10 +++--- .../briarproject/sync/PacketReaderImpl.java | 16 +++++++--- .../lifecycle/WindowsShutdownManagerImpl.java | 31 ++++++++++--------- .../plugins/file/RemovableDrivePlugin.java | 3 +- .../file/UnixRemovableDriveFinder.java | 6 ++-- .../file/WindowsRemovableDriveFinder.java | 10 +++--- 16 files changed, 68 insertions(+), 56 deletions(-) diff --git a/briar-android/src/org/briarproject/android/util/AndroidUtils.java b/briar-android/src/org/briarproject/android/util/AndroidUtils.java index 5fed2f5a2e..6a0e6b9da3 100644 --- a/briar-android/src/org/briarproject/android/util/AndroidUtils.java +++ b/briar-android/src/org/briarproject/android/util/AndroidUtils.java @@ -31,7 +31,6 @@ import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.List; import static android.content.Context.MODE_PRIVATE; @@ -63,7 +62,7 @@ public class AndroidUtils { abis.add(Build.CPU_ABI); if (Build.CPU_ABI2 != null) abis.add(Build.CPU_ABI2); } - return Collections.unmodifiableList(abis); + return abis; } public static void setError(TextInputLayout til, String error, diff --git a/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java b/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java index d1ba7d8582..f60e52bebb 100644 --- a/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java +++ b/briar-android/src/org/briarproject/plugins/droidtooth/DroidtoothPlugin.java @@ -40,6 +40,7 @@ import java.util.Map.Entry; import java.util.UUID; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; @@ -578,7 +579,7 @@ class DroidtoothPlugin implements DuplexPlugin { private static class DiscoveryReceiver extends BroadcastReceiver { private final CountDownLatch finished = new CountDownLatch(1); - private final List<String> addresses = new ArrayList<>(); + private final List<String> addresses = new CopyOnWriteArrayList<>(); @Override public void onReceive(Context ctx, Intent intent) { @@ -598,8 +599,9 @@ class DroidtoothPlugin implements DuplexPlugin { private List<String> waitForAddresses() throws InterruptedException { finished.await(); - Collections.shuffle(addresses); - return Collections.unmodifiableList(addresses); + List<String> shuffled = new ArrayList<>(addresses); + Collections.shuffle(shuffled); + return shuffled; } } diff --git a/briar-core/src/org/briarproject/blogs/BlogManagerImpl.java b/briar-core/src/org/briarproject/blogs/BlogManagerImpl.java index b593dfce74..ecc5991fa3 100644 --- a/briar-core/src/org/briarproject/blogs/BlogManagerImpl.java +++ b/briar-core/src/org/briarproject/blogs/BlogManagerImpl.java @@ -396,7 +396,7 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager, blogs.add(b); } } - return Collections.unmodifiableList(blogs); + return blogs; } @Override @@ -419,7 +419,7 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager, } finally { db.endTransaction(txn); } - return Collections.unmodifiableList(blogs); + return blogs; } catch (FormatException e) { throw new DbException(e); } diff --git a/briar-core/src/org/briarproject/clients/ClientHelperImpl.java b/briar-core/src/org/briarproject/clients/ClientHelperImpl.java index e78e1d36f1..7356ca1d1c 100644 --- a/briar-core/src/org/briarproject/clients/ClientHelperImpl.java +++ b/briar-core/src/org/briarproject/clients/ClientHelperImpl.java @@ -29,7 +29,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.security.GeneralSecurityException; -import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -208,7 +207,7 @@ class ClientHelperImpl implements ClientHelper { new HashMap<MessageId, BdfDictionary>(raw.size()); for (Entry<MessageId, Metadata> e : raw.entrySet()) parsed.put(e.getKey(), metadataParser.parse(e.getValue())); - return Collections.unmodifiableMap(parsed); + return parsed; } @Override @@ -236,7 +235,7 @@ class ClientHelperImpl implements ClientHelper { new HashMap<MessageId, BdfDictionary>(raw.size()); for (Entry<MessageId, Metadata> e : raw.entrySet()) parsed.put(e.getKey(), metadataParser.parse(e.getValue())); - return Collections.unmodifiableMap(parsed); + return parsed; } @Override diff --git a/briar-core/src/org/briarproject/clients/MessageTreeImpl.java b/briar-core/src/org/briarproject/clients/MessageTreeImpl.java index b1cd81a571..0ef7c230ab 100644 --- a/briar-core/src/org/briarproject/clients/MessageTreeImpl.java +++ b/briar-core/src/org/briarproject/clients/MessageTreeImpl.java @@ -103,7 +103,7 @@ public class MessageTreeImpl<T extends MessageTree.MessageNode> for (T root : roots) { traverse(orderedList, root, 0); } - return Collections.unmodifiableList(orderedList); + return orderedList; } } diff --git a/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java b/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java index 392c0b90b1..bac6f84ad0 100644 --- a/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java +++ b/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java @@ -329,7 +329,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent { if (ids.isEmpty()) return null; db.lowerRequestedFlag(txn, c, ids); transaction.attach(new MessagesSentEvent(c, ids)); - return Collections.unmodifiableList(messages); + return messages; } @Nullable @@ -379,7 +379,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent { if (ids.isEmpty()) return null; db.lowerRequestedFlag(txn, c, ids); transaction.attach(new MessagesSentEvent(c, ids)); - return Collections.unmodifiableList(messages); + return messages; } @Override diff --git a/briar-core/src/org/briarproject/forum/ForumManagerImpl.java b/briar-core/src/org/briarproject/forum/ForumManagerImpl.java index ff88d9fdbe..b4d7d84cba 100644 --- a/briar-core/src/org/briarproject/forum/ForumManagerImpl.java +++ b/briar-core/src/org/briarproject/forum/ForumManagerImpl.java @@ -199,7 +199,7 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager { } List<Forum> forums = new ArrayList<Forum>(); for (Group g : groups) forums.add(parseForum(g)); - return Collections.unmodifiableList(forums); + return forums; } catch (FormatException e) { throw new DbException(e); } diff --git a/briar-core/src/org/briarproject/plugins/ConnectionRegistryImpl.java b/briar-core/src/org/briarproject/plugins/ConnectionRegistryImpl.java index c7f726a985..7b687f9355 100644 --- a/briar-core/src/org/briarproject/plugins/ConnectionRegistryImpl.java +++ b/briar-core/src/org/briarproject/plugins/ConnectionRegistryImpl.java @@ -42,6 +42,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry { contactCounts = new HashMap<ContactId, Integer>(); } + @Override public void registerConnection(ContactId c, TransportId t, boolean incoming) { if (LOG.isLoggable(INFO)) { @@ -76,6 +77,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry { } } + @Override public void unregisterConnection(ContactId c, TransportId t, boolean incoming) { if (LOG.isLoggable(INFO)) { @@ -112,6 +114,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry { } } + @Override public Collection<ContactId> getConnectedContacts(TransportId t) { lock.lock(); try { @@ -120,12 +123,13 @@ class ConnectionRegistryImpl implements ConnectionRegistry { List<ContactId> ids = new ArrayList<ContactId>(m.keySet()); if (LOG.isLoggable(INFO)) LOG.info(ids.size() + " contacts connected"); - return Collections.unmodifiableList(ids); + return ids; } finally { lock.unlock(); } } + @Override public boolean isConnected(ContactId c, TransportId t) { lock.lock(); try { @@ -136,6 +140,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry { } } + @Override public boolean isConnected(ContactId c) { lock.lock(); try { diff --git a/briar-core/src/org/briarproject/plugins/PluginManagerImpl.java b/briar-core/src/org/briarproject/plugins/PluginManagerImpl.java index f9aa0ec179..010e75da58 100644 --- a/briar-core/src/org/briarproject/plugins/PluginManagerImpl.java +++ b/briar-core/src/org/briarproject/plugins/PluginManagerImpl.java @@ -156,12 +156,12 @@ class PluginManagerImpl implements PluginManager, Service { @Override public Collection<SimplexPlugin> getSimplexPlugins() { - return Collections.unmodifiableList(simplexPlugins); + return new ArrayList<SimplexPlugin>(simplexPlugins); } @Override public Collection<DuplexPlugin> getDuplexPlugins() { - return Collections.unmodifiableList(duplexPlugins); + return new ArrayList<DuplexPlugin>(duplexPlugins); } @Override @@ -169,7 +169,7 @@ class PluginManagerImpl implements PluginManager, Service { List<DuplexPlugin> supported = new ArrayList<DuplexPlugin>(); for (DuplexPlugin d : duplexPlugins) if (d.supportsInvitations()) supported.add(d); - return Collections.unmodifiableList(supported); + return supported; } @Override @@ -177,7 +177,7 @@ class PluginManagerImpl implements PluginManager, Service { List<DuplexPlugin> supported = new ArrayList<DuplexPlugin>(); for (DuplexPlugin d : duplexPlugins) if (d.supportsKeyAgreement()) supported.add(d); - return Collections.unmodifiableList(supported); + return supported; } private class PluginStarter implements Runnable { diff --git a/briar-core/src/org/briarproject/properties/TransportPropertyManagerImpl.java b/briar-core/src/org/briarproject/properties/TransportPropertyManagerImpl.java index 405d252b92..62b4be0c03 100644 --- a/briar-core/src/org/briarproject/properties/TransportPropertyManagerImpl.java +++ b/briar-core/src/org/briarproject/properties/TransportPropertyManagerImpl.java @@ -22,7 +22,6 @@ import org.briarproject.api.sync.Message; import org.briarproject.api.sync.MessageId; import org.briarproject.api.system.Clock; -import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -101,7 +100,7 @@ class TransportPropertyManagerImpl implements TransportPropertyManager, } finally { db.endTransaction(txn); } - return Collections.unmodifiableMap(local); + return local; } @Override @@ -176,7 +175,7 @@ class TransportPropertyManagerImpl implements TransportPropertyManager, } finally { db.endTransaction(txn); } - return Collections.unmodifiableMap(remote); + return remote; } catch (FormatException e) { throw new DbException(e); } diff --git a/briar-core/src/org/briarproject/sharing/SharingManagerImpl.java b/briar-core/src/org/briarproject/sharing/SharingManagerImpl.java index 2768d4a7ed..fbb0276f2a 100644 --- a/briar-core/src/org/briarproject/sharing/SharingManagerImpl.java +++ b/briar-core/src/org/briarproject/sharing/SharingManagerImpl.java @@ -46,7 +46,6 @@ import java.security.SecureRandom; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -443,7 +442,8 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS @Override public Collection<SharingInvitationItem> getInvitations() throws DbException { - List<SharingInvitationItem> invitations = new ArrayList<SharingInvitationItem>(); + List<SharingInvitationItem> invitations = + new ArrayList<SharingInvitationItem>(); Transaction txn = db.startTransaction(true); try { Set<S> shareables = new HashSet<S>(); @@ -474,7 +474,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS invitations.add(invitation); } db.commitTransaction(txn); - return Collections.unmodifiableCollection(invitations); + return invitations; } catch (FormatException e) { throw new DbException(e); } finally { @@ -523,7 +523,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS } finally { db.endTransaction(txn); } - return Collections.unmodifiableList(subscribers); + return subscribers; } private List<Contact> getSharedBy(Transaction txn, GroupId g) @@ -556,7 +556,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IS } finally { db.endTransaction(txn); } - return Collections.unmodifiableList(shared); + return shared; } catch (FormatException e) { throw new DbException(e); } diff --git a/briar-core/src/org/briarproject/sync/PacketReaderImpl.java b/briar-core/src/org/briarproject/sync/PacketReaderImpl.java index 4eea8831e8..abe24fa6b4 100644 --- a/briar-core/src/org/briarproject/sync/PacketReaderImpl.java +++ b/briar-core/src/org/briarproject/sync/PacketReaderImpl.java @@ -15,7 +15,6 @@ import org.briarproject.util.ByteUtils; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import static org.briarproject.api.sync.PacketTypes.ACK; @@ -74,19 +73,22 @@ class PacketReaderImpl implements PacketReader { state = State.BUFFER_FULL; } + @Override public boolean eof() throws IOException { if (state == State.BUFFER_EMPTY) readPacket(); if (state == State.BUFFER_EMPTY) throw new IllegalStateException(); return state == State.EOF; } + @Override public boolean hasAck() throws IOException { return !eof() && header[1] == ACK; } + @Override public Ack readAck() throws IOException { if (!hasAck()) throw new FormatException(); - return new Ack(Collections.unmodifiableList(readMessageIds())); + return new Ack(readMessageIds()); } private List<MessageId> readMessageIds() throws IOException { @@ -102,10 +104,12 @@ class PacketReaderImpl implements PacketReader { return ids; } + @Override public boolean hasMessage() throws IOException { return !eof() && header[1] == MESSAGE; } + @Override public Message readMessage() throws IOException { if (!hasMessage()) throw new FormatException(); if (payloadLength <= MESSAGE_HEADER_LENGTH) throw new FormatException(); @@ -125,21 +129,25 @@ class PacketReaderImpl implements PacketReader { return new Message(messageId, groupId, timestamp, raw); } + @Override public boolean hasOffer() throws IOException { return !eof() && header[1] == OFFER; } + @Override public Offer readOffer() throws IOException { if (!hasOffer()) throw new FormatException(); - return new Offer(Collections.unmodifiableList(readMessageIds())); + return new Offer(readMessageIds()); } + @Override public boolean hasRequest() throws IOException { return !eof() && header[1] == REQUEST; } + @Override public Request readRequest() throws IOException { if (!hasRequest()) throw new FormatException(); - return new Request(Collections.unmodifiableList(readMessageIds())); + return new Request(readMessageIds()); } } diff --git a/briar-desktop/src/org/briarproject/lifecycle/WindowsShutdownManagerImpl.java b/briar-desktop/src/org/briarproject/lifecycle/WindowsShutdownManagerImpl.java index 602d27f75e..764f25f600 100644 --- a/briar-desktop/src/org/briarproject/lifecycle/WindowsShutdownManagerImpl.java +++ b/briar-desktop/src/org/briarproject/lifecycle/WindowsShutdownManagerImpl.java @@ -1,19 +1,5 @@ package org.briarproject.lifecycle; -import static com.sun.jna.Library.OPTION_FUNCTION_MAPPER; -import static com.sun.jna.Library.OPTION_TYPE_MAPPER; -import static java.util.logging.Level.WARNING; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; -import java.util.logging.Logger; - -import org.briarproject.util.OsUtils; - import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.platform.win32.WinDef.HINSTANCE; @@ -28,6 +14,20 @@ import com.sun.jna.win32.StdCallLibrary.StdCallCallback; import com.sun.jna.win32.W32APIFunctionMapper; import com.sun.jna.win32.W32APITypeMapper; +import org.briarproject.util.OsUtils; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import java.util.logging.Logger; + +import static com.sun.jna.Library.OPTION_FUNCTION_MAPPER; +import static com.sun.jna.Library.OPTION_TYPE_MAPPER; +import static java.util.logging.Level.WARNING; + class WindowsShutdownManagerImpl extends ShutdownManagerImpl { private static final Logger LOG = @@ -44,7 +44,7 @@ class WindowsShutdownManagerImpl extends ShutdownManagerImpl { WindowsShutdownManagerImpl() { // Use the Unicode versions of Win32 API calls - Map<String, Object> m = new HashMap<String, Object>(); + Map<String, Object> m = new HashMap<>(); m.put(OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE); m.put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE); options = Collections.unmodifiableMap(m); @@ -112,6 +112,7 @@ class WindowsShutdownManagerImpl extends ShutdownManagerImpl { User32.class, options); // Create a callback to handle the WM_QUERYENDSESSION message WindowProc proc = new WindowProc() { + @Override public LRESULT callback(HWND hwnd, int msg, WPARAM wp, LPARAM lp) { if (msg == WM_QUERYENDSESSION) { diff --git a/briar-desktop/src/org/briarproject/plugins/file/RemovableDrivePlugin.java b/briar-desktop/src/org/briarproject/plugins/file/RemovableDrivePlugin.java index 4a463eb3eb..37728666d0 100644 --- a/briar-desktop/src/org/briarproject/plugins/file/RemovableDrivePlugin.java +++ b/briar-desktop/src/org/briarproject/plugins/file/RemovableDrivePlugin.java @@ -9,7 +9,6 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.concurrent.Executor; import java.util.logging.Logger; @@ -114,7 +113,7 @@ implements RemovableDriveMonitor.Callback { } catch (IOException e) { if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); } - return Collections.unmodifiableList(matches); + return matches; } @Override diff --git a/briar-desktop/src/org/briarproject/plugins/file/UnixRemovableDriveFinder.java b/briar-desktop/src/org/briarproject/plugins/file/UnixRemovableDriveFinder.java index d5c2d76fe4..0931f6f443 100644 --- a/briar-desktop/src/org/briarproject/plugins/file/UnixRemovableDriveFinder.java +++ b/briar-desktop/src/org/briarproject/plugins/file/UnixRemovableDriveFinder.java @@ -3,7 +3,6 @@ package org.briarproject.plugins.file; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Scanner; @@ -13,8 +12,9 @@ abstract class UnixRemovableDriveFinder implements RemovableDriveFinder { protected abstract String parseMountPoint(String line); protected abstract boolean isRemovableDriveMountPoint(String path); + @Override public List<File> findRemovableDrives() throws IOException { - List<File> drives = new ArrayList<File>(); + List<File> drives = new ArrayList<>(); Process p = new ProcessBuilder(getMountCommand()).start(); Scanner s = new Scanner(p.getInputStream(), "UTF-8"); try { @@ -35,6 +35,6 @@ abstract class UnixRemovableDriveFinder implements RemovableDriveFinder { } finally { s.close(); } - return Collections.unmodifiableList(drives); + return drives; } } diff --git a/briar-desktop/src/org/briarproject/plugins/file/WindowsRemovableDriveFinder.java b/briar-desktop/src/org/briarproject/plugins/file/WindowsRemovableDriveFinder.java index cfbb1f5a59..c67783ca8d 100644 --- a/briar-desktop/src/org/briarproject/plugins/file/WindowsRemovableDriveFinder.java +++ b/briar-desktop/src/org/briarproject/plugins/file/WindowsRemovableDriveFinder.java @@ -1,23 +1,23 @@ package org.briarproject.plugins.file; +import com.sun.jna.platform.win32.Kernel32; + import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; -import com.sun.jna.platform.win32.Kernel32; - class WindowsRemovableDriveFinder implements RemovableDriveFinder { // http://msdn.microsoft.com/en-us/library/windows/desktop/aa364939.aspx private static final int DRIVE_REMOVABLE = 2; + @Override public Collection<File> findRemovableDrives() throws IOException { File[] roots = File.listRoots(); if (roots == null) throw new IOException(); - List<File> drives = new ArrayList<File>(); + List<File> drives = new ArrayList<>(); for (File root : roots) { try { int type = Kernel32.INSTANCE.GetDriveType(root.getPath()); @@ -26,6 +26,6 @@ class WindowsRemovableDriveFinder implements RemovableDriveFinder { throw new IOException(e); } } - return Collections.unmodifiableList(drives); + return drives; } } -- GitLab