Skip to content

Commit

Permalink
ZOOKEEPER-2630: Use interface type instead of implementation type whe…
Browse files Browse the repository at this point in the history
Use interface type instead of implementation type when appropriate.

There are a couple of places in code base where we declare a field / variable as implementation type (i.e. HashMap, HashSet) instead of interface type (i.e. Map, Set), while in other places we do the opposite by declaring as interface type. A quick check indicates that most if not all of these places could be updated so we have a consistent style over the code base (prefer using interface type), which is also a good coding style to stick per best practice.

Checked and fixed Set, Map and List interface usages.

Author: Tamas Penzes <[email protected]>

Reviewers: Abe Fine <[email protected]>, Michael Han <[email protected]>

Closes apache#354 from tamaashu/ZOOKEEPER-2630
  • Loading branch information
tamaashu authored and hanm committed Sep 11, 2017
1 parent faab8d9 commit 1165794
Show file tree
Hide file tree
Showing 54 changed files with 159 additions and 121 deletions.
2 changes: 1 addition & 1 deletion docs/zookeeperReconfig.html
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ <h4>Non-incremental mode</h4>
example, you may specify a Hierarchical Quorum System even if the
current ensemble uses a Majority Quorum System.</p>
<p>Bulk mode - example using the Java API:</p>
<pre class="code">ArrayList&lt;String&gt; newMembers = new ArrayList&lt;String&gt;();
<pre class="code">List&lt;String&gt; newMembers = new ArrayList&lt;String&gt;();
newMembers.add("server.1=1111:1234:1235;1236");
newMembers.add("server.2=1112:1237:1238;1239");
newMembers.add("server.3=1114:1240:1241:observer;1242");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* This is a generic Main class that is completely driven by the
Expand All @@ -48,8 +50,8 @@ static class Cmd {
String clazz;
String desc;
}
static HashMap<String, Cmd> cmds = new HashMap<String, Cmd>();
static ArrayList<String> order = new ArrayList<String>();
static Map<String, Cmd> cmds = new HashMap<String, Cmd>();
static List<String> order = new ArrayList<String>();

/**
* @param args the first parameter of args will be used as an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
package org.apache.zookeeper.graph;

import java.util.ArrayList;
import java.util.List;
import org.apache.zookeeper.graph.filterops.*;

public abstract class FilterOp {
protected ArrayList<FilterOp> subOps;
protected ArrayList<Arg> args;
protected List<FilterOp> subOps;
protected List<Arg> args;

public enum ArgType {
STRING, NUMBER, SYMBOL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Set;

public class JsonGenerator {
private JSONObject root;
private HashSet<Integer> servers;
private Set<Integer> servers;

private class Message {
private int from;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public abstract class LogEntry implements Serializable {
private HashMap attributes;
private Map attributes;

public enum Type { UNKNOWN, LOG4J, TXN };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.PrintStream;

import java.util.HashSet;
import java.util.Set;

public class MeasureThroughput {
private static final int MS_PER_SEC = 1000;
Expand All @@ -45,7 +46,7 @@ public static void main(String[] args) throws IOException {
long currentsec = 0;
long currentmin = 0;
long currenthour = 0;
HashSet<Long> zxids_ms = new HashSet<Long>();
Set<Long> zxids_ms = new HashSet<Long>();
long zxid_sec = 0;
long zxid_min = 0;
long zxid_hour = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Set;

import org.apache.zookeeper.graph.*;
import org.slf4j.Logger;
Expand Down Expand Up @@ -80,7 +81,7 @@ public String handleRequest(JsonRequest request) throws Exception {

long current = 0;
long currentms = 0;
HashSet<Long> zxids_ms = new HashSet<Long>();
Set<Long> zxids_ms = new HashSet<Long>();
long zxidcount = 0;

JSONArray events = new JSONArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
*/
public class ZooInspectorConnectionPropertiesDialog extends JDialog {

private final HashMap<String, JComponent> components;
private final Map<String, JComponent> components;

/**
* @param lastConnectionProps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ZooInspectorPanel(final ZooInspectorManager zooInspectorManager, final Ic
this.zooInspectorManager = zooInspectorManager;
this.iconResource = iconResource;
toolbar = new Toolbar(iconResource);
final ArrayList<ZooInspectorNodeViewer> nodeViewers = new ArrayList<ZooInspectorNodeViewer>();
final List<ZooInspectorNodeViewer> nodeViewers = new ArrayList<ZooInspectorNodeViewer>();
try {
List<String> defaultNodeViewersClassNames = this.zooInspectorManager
.getDefaultNodeViewerConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ System.out.println(configStr);]]></programlisting>
example, you may specify a Hierarchical Quorum System even if the
current ensemble uses a Majority Quorum System.</para>
<para>Bulk mode - example using the Java API:</para>
<programlisting><![CDATA[ArrayList<String> newMembers = new ArrayList<String>();
<programlisting><![CDATA[List<String> newMembers = new ArrayList<String>();
newMembers.add("server.1=1111:1234:1235;1236");
newMembers.add("server.2=1112:1237:1238;1239");
newMembers.add("server.3=1114:1240:1241:observer;1242");
Expand Down
8 changes: 4 additions & 4 deletions src/java/main/org/apache/jute/compiler/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@

package org.apache.jute.compiler;

import java.util.ArrayList;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
* C++ Code generator front-end for Hadoop record I/O.
*/
class CGenerator {
private String mName;
private ArrayList<JFile> mInclFiles;
private ArrayList<JRecord> mRecList;
private List<JFile> mInclFiles;
private List<JRecord> mRecList;
private final File outputDirectory;

/** Creates a new instance of CppGenerator
Expand All @@ -40,7 +40,7 @@ class CGenerator {
* @param rlist List of records defined within this file
* @param outputDirectory
*/
CGenerator(String name, ArrayList<JFile> ilist, ArrayList<JRecord> rlist,
CGenerator(String name, List<JFile> ilist, List<JRecord> rlist,
File outputDirectory)
{
this.outputDirectory = outputDirectory;
Expand Down
6 changes: 3 additions & 3 deletions src/java/main/org/apache/jute/compiler/CSharpGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSharpGenerator {
private ArrayList<JRecord> mRecList;
private List<JRecord> mRecList;
private final File outputDirectory;

/** Creates a new instance of CSharpGenerator
Expand All @@ -33,7 +33,7 @@ public class CSharpGenerator {
* @param rlist List of records defined within this file
* @param outputDirectory
*/
CSharpGenerator(String name, ArrayList<JFile> ilist, ArrayList<JRecord> rlist,
CSharpGenerator(String name, List<JFile> ilist, List<JRecord> rlist,
File outputDirectory)
{
this.outputDirectory = outputDirectory;
Expand Down
8 changes: 4 additions & 4 deletions src/java/main/org/apache/jute/compiler/CppGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@

package org.apache.jute.compiler;

import java.util.ArrayList;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
* C++ Code generator front-end for Hadoop record I/O.
*/
class CppGenerator {
private String mName;
private ArrayList<JFile> mInclFiles;
private ArrayList<JRecord> mRecList;
private List<JFile> mInclFiles;
private List<JRecord> mRecList;
private final File outputDirectory;

/** Creates a new instance of CppGenerator
Expand All @@ -40,7 +40,7 @@ class CppGenerator {
* @param rlist List of records defined within this file
* @param outputDirectory
*/
CppGenerator(String name, ArrayList<JFile> ilist, ArrayList<JRecord> rlist,
CppGenerator(String name, List<JFile> ilist, List<JRecord> rlist,
File outputDirectory)
{
this.outputDirectory = outputDirectory;
Expand Down
5 changes: 3 additions & 2 deletions src/java/main/org/apache/jute/compiler/JFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Container for the Hadoop Record DDL.
Expand All @@ -31,8 +32,8 @@
public class JFile {

private String mName;
private ArrayList<JFile> mInclFiles;
private ArrayList<JRecord> mRecords;
private List<JFile> mInclFiles;
private List<JRecord> mRecords;

/** Creates a new instance of JFile
*
Expand Down
8 changes: 5 additions & 3 deletions src/java/main/org/apache/jute/compiler/JRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
*
Expand All @@ -33,7 +35,7 @@ public class JRecord extends JCompType {
private String mFQName;
private String mName;
private String mModule;
private ArrayList<JField> mFields;
private List<JField> mFields;

/**
* Creates a new instance of JRecord
Expand Down Expand Up @@ -83,7 +85,7 @@ public String getCsharpNameSpace() {
return namespace.toString();
}

public ArrayList<JField> getFields() {
public List<JField> getFields() {
return mFields;
}

Expand Down Expand Up @@ -139,7 +141,7 @@ public String genCsharpWriteWrapper(String fname, String tag) {
return " a_.WriteRecord("+fname+",\""+tag+"\");\n";
}

static HashMap<String, String> vectorStructs = new HashMap<String, String>();
static Map<String, String> vectorStructs = new HashMap<String, String>();
public void genCCode(FileWriter h, FileWriter c) throws IOException {
for (JField f : mFields) {
if (f.getType() instanceof JVector) {
Expand Down
8 changes: 4 additions & 4 deletions src/java/main/org/apache/jute/compiler/JavaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* Java Code generator front-end for Hadoop record I/O.
*/
class JavaGenerator {
private ArrayList<JRecord> mRecList;
private List<JRecord> mRecList;
private final File outputDirectory;

/** Creates a new instance of JavaGenerator
Expand All @@ -37,8 +37,8 @@ class JavaGenerator {
* @param records List of records defined within this file
* @param outputDirectory
*/
JavaGenerator(String name, ArrayList<JFile> incl,
ArrayList<JRecord> records, File outputDirectory)
JavaGenerator(String name, List<JFile> incl,
List<JRecord> records, File outputDirectory)
{
mRecList = records;
this.outputDirectory = outputDirectory;
Expand Down
4 changes: 2 additions & 2 deletions src/java/main/org/apache/zookeeper/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public String toString() {
}
}

private static void put(ArrayList<Entry> l, String k, String v) {
private static void put(List<Entry> l, String k, String v) {
l.add(new Entry(k,v));
}

public static List<Entry> list() {
ArrayList<Entry> l = new ArrayList<Entry>();
List<Entry> l = new ArrayList<Entry>();
put(l, "zookeeper.version", Version.getFullVersion());

try {
Expand Down
3 changes: 2 additions & 1 deletion src/java/main/org/apache/zookeeper/common/PathTrie.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -53,7 +54,7 @@ public class PathTrie {

static class TrieNode {
boolean property = false;
final HashMap<String, TrieNode> children;
final Map<String, TrieNode> children;
TrieNode parent = null;
/**
* create a trienode with parent
Expand Down
10 changes: 5 additions & 5 deletions src/java/main/org/apache/zookeeper/server/DataTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public Set<String> getEphemerals(long sessionId) {
if (retv == null) {
return new HashSet<String>();
}
HashSet<String> cloned = null;
Set<String> cloned = null;
synchronized (retv) {
cloned = (HashSet<String>) retv.clone();
}
Expand Down Expand Up @@ -549,7 +549,7 @@ public void deleteNode(String path, long zxid)
} else if (ephemeralType == EphemeralType.TTL) {
ttls.remove(path);
} else if (eowner != 0) {
HashSet<String> nodes = ephemerals.get(eowner);
Set<String> nodes = ephemerals.get(eowner);
if (nodes != null) {
synchronized (nodes) {
nodes.remove(path);
Expand Down Expand Up @@ -1002,7 +1002,7 @@ void killSession(long session, long zxid) {
// so there is no need for synchronization. The list is not
// changed here. Only create and delete change the list which
// are again called from FinalRequestProcessor in sequence.
HashSet<String> list = ephemerals.remove(session);
Set<String> list = ephemerals.remove(session);
if (list != null) {
for (String path : list) {
try {
Expand Down Expand Up @@ -1286,7 +1286,7 @@ public void dumpEphemerals(PrintWriter pwriter) {
for (Entry<Long, HashSet<String>> entry : ephemerals.entrySet()) {
pwriter.print("0x" + Long.toHexString(entry.getKey()));
pwriter.println(":");
HashSet<String> tmp = entry.getValue();
Set<String> tmp = entry.getValue();
if (tmp != null) {
synchronized (tmp) {
for (String path : tmp) {
Expand All @@ -1303,7 +1303,7 @@ public void dumpEphemerals(PrintWriter pwriter) {
* @return map of session ID to sets of ephemeral znodes
*/
public Map<Long, Set<String>> getEphemerals() {
HashMap<Long, Set<String>> ephemeralsCopy = new HashMap<Long, Set<String>>();
Map<Long, Set<String>> ephemeralsCopy = new HashMap<Long, Set<String>>();
for (Entry<Long, HashSet<String>> e : ephemerals.entrySet()) {
synchronized (e.getValue()) {
ephemeralsCopy.put(e.getKey(), new HashSet<String>(e.getValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class NettyServerCnxnFactory extends ServerCnxnFactory {
ServerBootstrap bootstrap;
Channel parentChannel;
ChannelGroup allChannels = new DefaultChannelGroup("zkServerCnxns");
HashMap<InetAddress, Set<NettyServerCnxn>> ipMap =
Map<InetAddress, Set<NettyServerCnxn>> ipMap =
new HashMap<InetAddress, Set<NettyServerCnxn>>( );
InetSocketAddress localAddress;
int maxClientCnxns = 60;
Expand Down Expand Up @@ -547,7 +547,7 @@ public void resetAllConnectionStats() {

@Override
public Iterable<Map<String, Object>> getAllConnectionInfo(boolean brief) {
HashSet<Map<String,Object>> info = new HashSet<Map<String,Object>>();
Set<Map<String,Object>> info = new HashSet<Map<String,Object>>();
// No need to synchronize since cnxns is backed by a ConcurrentHashMap
for (ServerCnxn c : cnxns) {
info.add(c.getConnectionInfo(brief));
Expand Down
Loading

0 comments on commit 1165794

Please sign in to comment.