Skip to content

Commit

Permalink
Java language level syntactic sugaring
Browse files Browse the repository at this point in the history
Non-functional change - make use of new Java language features available up to Java 17 LTS

- Use String.repeat()
- Replace statements with enhanced switch
- Use pattern variable
- Convert to enhanced for-loop
- Prefer StringBuilder over StringBuffer
- Remove unnecessary boxing/unboxing
- Use Objects.equals() rather than equals() expression
- Replace explicit types by diamond operator
- Use lambdas for anonymous types
- Use Comparator combinator
- Introduce method references for lambdas
- Change statement to expression lambdas
- Check for null using method call
  • Loading branch information
spannm committed Jan 26, 2024
1 parent ab5d4e2 commit 954b11a
Show file tree
Hide file tree
Showing 41 changed files with 324 additions and 447 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Set<Map.Entry<K, V>> entrySet() {
@SuppressWarnings("unchecked")
public V get(Object _key) {
for (Object[] entry : entries) {
if (_key == entry[0] || _key != null && _key.equals(entry[0])) {
if (Objects.equals(_key, entry[0])) {
return (V) entry[1];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,9 @@ public boolean equals(Object _obj) {
if (this == _obj) {
return true;
}
if (!(_obj instanceof DBusMatchRule)) {
if (!(_obj instanceof DBusMatchRule other)) {
return false;
}
DBusMatchRule other = (DBusMatchRule) _obj;
return Objects.equals(iface, other.iface) && Objects.equals(member, other.member)
&& Objects.equals(object, other.object) && Objects.equals(source, other.source)
&& Objects.equals(type, other.type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ public class MethodTuple {

public MethodTuple(String _name, String _sig) {
name = _name;
if (null != _sig) {
sig = _sig;
} else {
sig = "";
}
sig = Objects.requireNonNullElse(_sig, "");
logger.trace("new MethodTuple({}, {})", name, sig);
}

Expand All @@ -31,10 +27,9 @@ public boolean equals(Object _obj) {
if (this == _obj) {
return true;
}
if (!(_obj instanceof MethodTuple)) {
if (!(_obj instanceof MethodTuple other)) {
return false;
}
MethodTuple other = (MethodTuple) _obj;
return Objects.equals(name, other.name) && Objects.equals(sig, other.sig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Object invoke(Object _proxy, Method _method, Object[] _args) throws Throw
} else if (_method.getName().equals("equals")) {
try {
if (1 == _args.length) {
return Boolean.valueOf(_args[0] != null && remote.equals(((RemoteInvocationHandler) Proxy.getInvocationHandler(_args[0])).remote));
return _args[0] != null && remote.equals(((RemoteInvocationHandler) Proxy.getInvocationHandler(_args[0])).remote);
}
} catch (IllegalArgumentException _exIa) {
return Boolean.FALSE;
Expand Down Expand Up @@ -211,16 +211,16 @@ public static Object executeRemoteMethod(final RemoteObject _ro, final Method _m
}

switch (_syncmethod) {
case CALL_TYPE_ASYNC:
case CALL_TYPE_ASYNC -> {
_conn.sendMessage(call);
return new DBusAsyncReply<>(call, _m, _conn);
case CALL_TYPE_CALLBACK:
}
case CALL_TYPE_CALLBACK -> {
_conn.queueCallback(call, _m, _callback);
_conn.sendMessage(call);
return null;
case CALL_TYPE_SYNC:
_conn.sendMessage(call);
break;
}
case CALL_TYPE_SYNC -> _conn.sendMessage(call);
}

// get reply
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ public RemoteObject(String _busname, String _objectpath, Class<? extends DBusInt

@Override
public boolean equals(Object _o) {
if (!(_o instanceof RemoteObject)) {
if (!(_o instanceof RemoteObject them)) {
return false;
}
RemoteObject them = (RemoteObject) _o;

if (!them.objectpath.equals(this.objectpath)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.function.IntFunction;

/**
* Helper util to create {@link Struct} subclasses when receiving it from DBus.
Expand Down Expand Up @@ -100,7 +101,7 @@ public static <T extends Struct> void convertToStructCollection(Collection<Objec

Class<?>[] constructorArgClasses = Arrays.stream(_structType.getDeclaredFields())
.filter(f -> f.isAnnotationPresent(Position.class))
.sorted((f1, f2) -> Integer.compare(f1.getAnnotation(Position.class).value(), f2.getAnnotation(Position.class).value()))
.sorted(Comparator.comparingInt(f -> f.getAnnotation(Position.class).value()))
.map(Field::getType)
.toArray(Class[]::new);

Expand Down Expand Up @@ -137,7 +138,7 @@ public static <T extends Struct> T createStructFromVariant(Variant<?> _variant,
}

if (_variant.getType() instanceof DBusStructType && _variant.getValue() instanceof Object[]) {
Class<?>[] argTypes = Arrays.stream((Object[]) _variant.getValue()).map(Object::getClass).toArray(size -> new Class<?>[size]);
Class<?>[] argTypes = Arrays.stream((Object[]) _variant.getValue()).map(Object::getClass).toArray((IntFunction<Class<?>[]>) Class[]::new);
return createStruct(argTypes, _variant.getValue(), _structClass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,9 @@ public boolean equals(Object _obj) {
if (this == _obj) {
return true;
}
if (!(_obj instanceof Pair)) {
if (!(_obj instanceof Pair<?, ?> other)) {
return false;
}
Pair<?, ?> other = (Pair<?, ?>) _obj;
return Objects.equals(first, other.first) && Objects.equals(second, other.second);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ private static Class<?>[] createTypesArray(Object... _parameters) {
return null;
}
return Arrays.stream(_parameters)
.filter(p -> p != null) // do no try to convert null values to concrete class
.filter(Objects::nonNull) // do no try to convert null values to concrete class
.map(p -> {
if (List.class.isAssignableFrom(p.getClass())) { // turn possible List subclasses (e.g. ArrayList) to interface class List
return List.class;
Expand Down

0 comments on commit 954b11a

Please sign in to comment.