sklaha commented on code in PR #249: URL: https://github.com/apache/cassandra-sidecar/pull/249#discussion_r2323595027
########## adapters/adapters-base/src/main/java/org/apache/cassandra/sidecar/adapters/base/utils/DataTypeUtils.java: ########## @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.sidecar.adapters.base.utils; + +/** + * Utility class for data type conversions. + */ +public class DataTypeUtils +{ + + /** + * Private constructor to prevent instantiation + */ + private DataTypeUtils() + { + // utility class + } + + /** + * Converts a metric value to a long representation. + * This utility method handles common metric value types returned from JMX operations, + * converting them to a consistent long type for numerical operations. + * + * @param value the metric value to convert, must be an Integer or Long + * @return the value as a long + * @throws IllegalArgumentException if the value type is not supported (not Integer or Long) + */ + public static long getValueAsLong(Object value) + { + if (value instanceof Integer) + { + return ((Integer) value).longValue(); + } + else if (value instanceof Long) + { + return (Long) value; + } + else + { + throw new IllegalArgumentException("Unsupported value type: " + value.getClass()); + } + } + + /** + * Safely casts an object to the specified type with descriptive error handling. + * This method performs a runtime type check before casting to prevent ClassCastException + * and provides meaningful error messages when the cast fails. + * + * @param value the object to be cast + * @param expectedType the expected type to cast to + * @param contextDescription descriptive context for error messages (e.g., "keyspace name", "table data") + * @param <T> the target type + * @return the cast object of type T + * @throws IllegalStateException if the value is not an instance of the expected type, + * with a descriptive message indicating what was expected vs what was received + */ + public static <T> T safeCast(Object value, Class<T> expectedType, String contextDescription) + { + if (!expectedType.isInstance(value)) + { + throw new ClassCastException("Expected " + expectedType.getSimpleName() + " for " + contextDescription + " but got: " + + (value == null ? "null" : value.getClass().getSimpleName())); + } + return expectedType.cast(value); + } + + /** + * Safely parses a string to a long with descriptive error handling. + * This method handles null values and provides meaningful error messages when parsing fails. + * + * @param value the string value to be parsed + * @param contextDescription descriptive context for error messages (e.g., "completed bytes", "total bytes") + * @return the parsed long value + * @throws IllegalStateException if the value cannot be parsed as a long, + * with a descriptive message indicating what failed to parse + */ + public static long safeParseLong(String value, String contextDescription) + { + if (value == null) + { + throw new NumberFormatException("Cannot parse null value for " + contextDescription); + } + + try + { + return Long.parseLong(value); + } + catch (NumberFormatException ex) + { + throw new NumberFormatException("Failed to parse long value '" + value + "' for " + contextDescription + ": " + ex.getMessage()); + } + } + + /** + * Converts megabytes to bytes. + * This utility method performs the standard conversion from megabytes to bytes + * using the binary conversion factor (1 MB = 1024 * 1024 bytes). + * + * @param megabytes the value in megabytes to convert + * @return the equivalent value in bytes + */ + public static long megabytesToBytes(long megabytes) + { + return megabytes * 1024 * 1024; Review Comment: Updated. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

