...
A class that implements functions specific to a particular class (e.g. its event handlers or services that it exports) should be implemented as an inner private class within the class. Such classes have names beginning with Internal-
, e,g InternalClusterEventListener
.
Objects, Methods, Fields and
...
Variables
Data types
Wherever possible, use a rich data type over a primitive (e.g. MacAddress
versus a long
). This reduces ambiguity.
...
- Avoid
synchronized(this)
and synchronized methods wherever possible. Opt for thread-safe objects such as
ConcurrentMap
, and if usingsynchronized
, apply it to the structure that must be locked:Code Block language java protected final Map<DeviceId, LinkDiscovery> discoverers = new HashMap<>(); @Override public void event(MastershipEvent event) { ... synchronized (discoverers) { if (!discoverers.containsKey(deviceId)) { discoverers.put(deviceId, new LinkDiscovery(device, packetSevice, masterService, providerService, useBDDP)); } } }
Additionally, some structures such as Hazelcast's
IMap
have per-key locks.
equals() and hashCode()
Any objects that are compared, or stored in any hash based data structure (e.g. HashSet, HashMap), should implement these methods. For objects that implement them, comparisons should use their equals()
method, and not ==
. An exception to this rule are Enums.
Variables and Parameters
There is no need to decorate variables and parameters with the final
keyword. This is an out-dated convention from Java 1.1 era. The only time this is required is when the parameter is used as part of an anonymous class closure defines within the method. Otherwise, let the Java compiler/optimizer to its work.
Naming
JSON fields should be in camel case:
Code Block { "aliasIp": "10.1.2.3" }
and not
"alias_ip"
.
...