Upgrade pattern matches

This commit is contained in:
Anton Tananaev
2026-05-16 15:57:06 -07:00
parent 054c151fc4
commit c1f81846eb
9 changed files with 147 additions and 158 deletions

View File

@@ -64,11 +64,11 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> {
public static <T extends ChannelHandler> T getHandler(ChannelPipeline pipeline, Class<T> clazz) {
for (Map.Entry<String, ChannelHandler> handlerEntry : pipeline) {
ChannelHandler handler = handlerEntry.getValue();
if (handler instanceof WrapperInboundHandler wrapperHandler) {
handler = wrapperHandler.getWrappedHandler();
} else if (handler instanceof WrapperOutboundHandler wrapperHandler) {
handler = wrapperHandler.getWrappedHandler();
}
handler = switch (handler) {
case WrapperInboundHandler wrapperHandler -> wrapperHandler.getWrappedHandler();
case WrapperOutboundHandler wrapperHandler -> wrapperHandler.getWrappedHandler();
default -> handler;
};
if (clazz.isAssignableFrom(handler.getClass())) {
return (T) handler;
}
@@ -106,11 +106,11 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> {
if (handler instanceof BaseProtocolDecoder || handler instanceof BaseProtocolEncoder) {
injectMembers(handler);
} else {
if (handler instanceof ChannelInboundHandler channelHandler) {
handler = new WrapperInboundHandler(channelHandler);
} else if (handler instanceof ChannelOutboundHandler channelHandler) {
handler = new WrapperOutboundHandler(channelHandler);
}
handler = switch (handler) {
case ChannelInboundHandler channelHandler -> new WrapperInboundHandler(channelHandler);
case ChannelOutboundHandler channelHandler -> new WrapperOutboundHandler(channelHandler);
default -> handler;
};
}
pipeline.addLast(handler);
});

View File

@@ -162,15 +162,14 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder {
statisticsManager.registerMessageReceived();
}
Set<Long> deviceIds = new HashSet<>();
if (decodedMessage != null) {
if (decodedMessage instanceof Position position) {
deviceIds.add(position.getDeviceId());
} else if (decodedMessage instanceof Collection) {
Collection<Position> positions = (Collection) decodedMessage;
for (Position position : positions) {
deviceIds.add(position.getDeviceId());
switch (decodedMessage) {
case Position position -> deviceIds.add(position.getDeviceId());
case Collection<?> positions -> {
for (Object position : positions) {
deviceIds.add(((Position) position).getDeviceId());
}
}
case null, default -> { }
}
if (deviceIds.isEmpty()) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);

View File

@@ -93,33 +93,24 @@ public class Calendar extends ExtendedModel {
}
private static Temporal convertToMatchingTemporal(Instant instant, Temporal sample) {
if (sample instanceof LocalDate) {
return instant.atZone(ZoneOffset.UTC).toLocalDate();
} else if (sample instanceof LocalDateTime) {
return instant.atZone(ZoneOffset.UTC).toLocalDateTime();
} else if (sample instanceof ZonedDateTime zonedDateTime) {
return instant.atZone(zonedDateTime.getZone());
} else if (sample instanceof OffsetDateTime offsetDateTime) {
return instant.atOffset(offsetDateTime.getOffset());
} else {
return instant;
}
return switch (sample) {
case LocalDate ignored -> instant.atZone(ZoneOffset.UTC).toLocalDate();
case LocalDateTime ignored -> instant.atZone(ZoneOffset.UTC).toLocalDateTime();
case ZonedDateTime zonedDateTime -> instant.atZone(zonedDateTime.getZone());
case OffsetDateTime offsetDateTime -> instant.atOffset(offsetDateTime.getOffset());
default -> instant;
};
}
private static Instant temporalToInstant(Temporal temporal) {
if (temporal instanceof ZonedDateTime zonedDateTime) {
return zonedDateTime.toInstant();
} else if (temporal instanceof OffsetDateTime offsetDateTime) {
return offsetDateTime.toInstant();
} else if (temporal instanceof LocalDateTime localDateTime) {
return localDateTime.toInstant(ZoneOffset.UTC);
} else if (temporal instanceof LocalDate localDate) {
return localDate.atStartOfDay(ZoneOffset.UTC).toInstant();
} else if (temporal instanceof Instant instantValue) {
return instantValue;
} else {
throw new IllegalArgumentException("Unsupported Temporal type");
}
return switch (temporal) {
case ZonedDateTime zonedDateTime -> zonedDateTime.toInstant();
case OffsetDateTime offsetDateTime -> offsetDateTime.toInstant();
case LocalDateTime localDateTime -> localDateTime.toInstant(ZoneOffset.UTC);
case LocalDate localDate -> localDate.atStartOfDay(ZoneOffset.UTC).toInstant();
case Instant instantValue -> instantValue;
default -> throw new IllegalArgumentException("Unsupported Temporal type");
};
}
}

View File

@@ -288,17 +288,19 @@ public class FlespiProtocolDecoder extends BaseHttpProtocolDecoder {
}
private void decodeUnknownParam(String name, JsonValue value, Position position) {
if (value instanceof JsonNumber jsonNumber) {
if (jsonNumber.isIntegral()) {
position.set(name, jsonNumber.longValue());
} else {
switch (value) {
case JsonNumber jsonNumber -> {
if (jsonNumber.isIntegral()) {
position.set(name, jsonNumber.longValue());
} else {
position.set(name, jsonNumber.doubleValue());
}
position.set(name, jsonNumber.doubleValue());
}
position.set(name, jsonNumber.doubleValue());
} else if (value instanceof JsonString jsonString) {
position.set(name, jsonString.getString());
} else if (value == JsonValue.TRUE || value == JsonValue.FALSE) {
position.set(name, value == JsonValue.TRUE);
case JsonString jsonString -> position.set(name, jsonString.getString());
case JsonValue v when v == JsonValue.TRUE || v == JsonValue.FALSE ->
position.set(name, v == JsonValue.TRUE);
default -> { }
}
}

View File

@@ -77,12 +77,12 @@ public class OutsafeProtocolDecoder extends BaseHttpProtocolDecoder {
}
private void decodeUnknownParam(String name, JsonValue value, Position position) {
if (value instanceof JsonNumber jsonNumber) {
position.set(name, jsonNumber.doubleValue());
} else if (value instanceof JsonString jsonString) {
position.set(name, jsonString.getString());
} else if (value == JsonValue.TRUE || value == JsonValue.FALSE) {
position.set(name, value == JsonValue.TRUE);
switch (value) {
case JsonNumber jsonNumber -> position.set(name, jsonNumber.doubleValue());
case JsonString jsonString -> position.set(name, jsonString.getString());
case JsonValue v when v == JsonValue.TRUE || v == JsonValue.FALSE ->
position.set(name, v == JsonValue.TRUE);
default -> { }
}
}

View File

@@ -241,28 +241,32 @@ public class CacheManager implements BroadcastInterface {
return;
}
if (after instanceof GroupedModel afterGrouped) {
long beforeGroupId = ((GroupedModel) before).getGroupId();
long afterGroupId = afterGrouped.getGroupId();
if (beforeGroupId != afterGroupId) {
if (beforeGroupId > 0) {
invalidatePermission(clazz, id, Group.class, beforeGroupId, false);
}
if (afterGroupId > 0) {
invalidatePermission(clazz, id, Group.class, afterGroupId, true);
switch (after) {
case GroupedModel afterGrouped -> {
long beforeGroupId = ((GroupedModel) before).getGroupId();
long afterGroupId = afterGrouped.getGroupId();
if (beforeGroupId != afterGroupId) {
if (beforeGroupId > 0) {
invalidatePermission(clazz, id, Group.class, beforeGroupId, false);
}
if (afterGroupId > 0) {
invalidatePermission(clazz, id, Group.class, afterGroupId, true);
}
}
}
} else if (after instanceof Schedulable afterSchedulable) {
long beforeCalendarId = ((Schedulable) before).getCalendarId();
long afterCalendarId = afterSchedulable.getCalendarId();
if (beforeCalendarId != afterCalendarId) {
if (beforeCalendarId > 0) {
invalidatePermission(clazz, id, Calendar.class, beforeCalendarId, false);
}
if (afterCalendarId > 0) {
invalidatePermission(clazz, id, Calendar.class, afterCalendarId, true);
case Schedulable afterSchedulable -> {
long beforeCalendarId = ((Schedulable) before).getCalendarId();
long afterCalendarId = afterSchedulable.getCalendarId();
if (beforeCalendarId != afterCalendarId) {
if (beforeCalendarId > 0) {
invalidatePermission(clazz, id, Calendar.class, beforeCalendarId, false);
}
if (afterCalendarId > 0) {
invalidatePermission(clazz, id, Calendar.class, afterCalendarId, true);
}
}
}
default -> { }
}
graph.updateObject(after);

View File

@@ -253,33 +253,39 @@ public class DatabaseStorage extends Storage {
private List<Object> getConditionVariables(Condition genericCondition) {
List<Object> results = new ArrayList<>();
if (genericCondition instanceof Condition.Compare condition) {
results.add(condition.getValue());
} else if (genericCondition instanceof Condition.Between condition) {
results.add(condition.getFromValue());
results.add(condition.getToValue());
} else if (genericCondition instanceof Condition.Binary condition) {
results.addAll(getConditionVariables(condition.getFirst()));
results.addAll(getConditionVariables(condition.getSecond()));
} else if (genericCondition instanceof Condition.Contains condition) {
String value = "%" + condition.getValue().toLowerCase(Locale.ROOT) + "%";
results.addAll(Collections.nCopies(condition.getColumns().size(), value));
} else if (genericCondition instanceof Condition.Permission condition) {
long conditionId = condition.getOwnerId() > 0 ? condition.getOwnerId() : condition.getPropertyId();
results.add(conditionId);
if (condition.getIncludeGroups()) {
results.add(conditionId);
switch (genericCondition) {
case Condition.Compare condition -> results.add(condition.getValue());
case Condition.Between condition -> {
results.add(condition.getFromValue());
results.add(condition.getToValue());
}
} else if (genericCondition instanceof Condition.LatestPositions condition) {
if (condition.getDeviceId() > 0) {
results.add(condition.getDeviceId());
results.add(condition.getDeviceId());
} else {
long period = config.getLong(Keys.DATABASE_POSITION_PERIOD);
if (period > 0) {
results.add(new Date(System.currentTimeMillis() - period * 1000));
case Condition.Binary condition -> {
results.addAll(getConditionVariables(condition.getFirst()));
results.addAll(getConditionVariables(condition.getSecond()));
}
case Condition.Contains condition -> {
String value = "%" + condition.getValue().toLowerCase(Locale.ROOT) + "%";
results.addAll(Collections.nCopies(condition.getColumns().size(), value));
}
case Condition.Permission condition -> {
long conditionId = condition.getOwnerId() > 0 ? condition.getOwnerId() : condition.getPropertyId();
results.add(conditionId);
if (condition.getIncludeGroups()) {
results.add(conditionId);
}
}
case Condition.LatestPositions condition -> {
if (condition.getDeviceId() > 0) {
results.add(condition.getDeviceId());
results.add(condition.getDeviceId());
} else {
long period = config.getLong(Keys.DATABASE_POSITION_PERIOD);
if (period > 0) {
results.add(new Date(System.currentTimeMillis() - period * 1000));
}
}
}
default -> { }
}
return results;
}

View File

@@ -65,55 +65,46 @@ public class MemoryStorage extends Storage {
if (genericCondition == null) {
return true;
}
if (genericCondition instanceof Condition.Compare condition) {
Object value = retrieveValue(object, condition.getColumn());
int result = ((Comparable) value).compareTo(condition.getValue());
return switch (condition.getOperator()) {
case "<" -> result < 0;
case "<=" -> result <= 0;
case ">" -> result > 0;
case ">=" -> result >= 0;
case "=" -> result == 0;
default -> throw new RuntimeException("Unsupported comparison condition");
};
} else if (genericCondition instanceof Condition.Between condition) {
Object fromValue = retrieveValue(object, condition.getColumn());
int fromResult = ((Comparable) fromValue).compareTo(condition.getFromValue());
Object toValue = retrieveValue(object, condition.getColumn());
int toResult = ((Comparable) toValue).compareTo(condition.getToValue());
return fromResult >= 0 && toResult <= 0;
} else if (genericCondition instanceof Condition.Binary condition) {
if (condition.getOperator().equals("AND")) {
return checkCondition(condition.getFirst(), object) && checkCondition(condition.getSecond(), object);
} else if (condition.getOperator().equals("OR")) {
return checkCondition(condition.getFirst(), object) || checkCondition(condition.getSecond(), object);
return switch (genericCondition) {
case Condition.Compare condition -> {
Object value = retrieveValue(object, condition.getColumn());
int result = ((Comparable) value).compareTo(condition.getValue());
yield switch (condition.getOperator()) {
case "<" -> result < 0;
case "<=" -> result <= 0;
case ">" -> result > 0;
case ">=" -> result >= 0;
case "=" -> result == 0;
default -> throw new RuntimeException("Unsupported comparison condition");
};
}
} else if (genericCondition instanceof Condition.Permission condition) {
long id = (Long) retrieveValue(object, "id");
return getPermissionsSet(condition.getOwnerClass(), condition.getPropertyClass()).stream()
.anyMatch(pair -> {
if (condition.getOwnerId() > 0) {
return pair.first() == condition.getOwnerId() && pair.second() == id;
} else {
return pair.first() == id && pair.second() == condition.getPropertyId();
}
});
} else if (genericCondition instanceof Condition.LatestPositions) {
return false;
}
return false;
case Condition.Between condition -> {
Object fromValue = retrieveValue(object, condition.getColumn());
int fromResult = ((Comparable) fromValue).compareTo(condition.getFromValue());
Object toValue = retrieveValue(object, condition.getColumn());
int toResult = ((Comparable) toValue).compareTo(condition.getToValue());
yield fromResult >= 0 && toResult <= 0;
}
case Condition.Binary condition -> switch (condition.getOperator()) {
case "AND" -> checkCondition(condition.getFirst(), object)
&& checkCondition(condition.getSecond(), object);
case "OR" -> checkCondition(condition.getFirst(), object)
|| checkCondition(condition.getSecond(), object);
default -> false;
};
case Condition.Permission condition -> {
long id = (Long) retrieveValue(object, "id");
yield getPermissionsSet(condition.getOwnerClass(), condition.getPropertyClass()).stream()
.anyMatch(pair -> {
if (condition.getOwnerId() > 0) {
return pair.first() == condition.getOwnerId() && pair.second() == id;
} else {
return pair.first() == id && pair.second() == condition.getPropertyId();
}
});
}
default -> false;
};
}
private Object retrieveValue(Object object, String key) {

View File

@@ -139,18 +139,14 @@ public final class QueryBuilder implements AutoCloseable {
}
public void setValue(int index, Object value) throws SQLException {
if (value instanceof Boolean booleanValue) {
setBoolean(index, booleanValue);
} else if (value instanceof Integer integerValue) {
setInteger(index, integerValue);
} else if (value instanceof Long longValue) {
setLong(index, longValue);
} else if (value instanceof Double doubleValue) {
setDouble(index, doubleValue);
} else if (value instanceof String stringValue) {
setString(index, stringValue);
} else if (value instanceof Date dateValue) {
setDate(index, dateValue);
switch (value) {
case Boolean booleanValue -> setBoolean(index, booleanValue);
case Integer integerValue -> setInteger(index, integerValue);
case Long longValue -> setLong(index, longValue);
case Double doubleValue -> setDouble(index, doubleValue);
case String stringValue -> setString(index, stringValue);
case Date dateValue -> setDate(index, dateValue);
default -> { }
}
}