{"record":{"id":"83eba6b71811f35b","repo":"TheAlgorithms/Java","slug":"inputunit-must-be-different-from-outputunit","errorCode":null,"errorMessage":"inputUnit must be different from outputUnit.","messagePattern":"inputUnit must be different from outputUnit\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/UnitsConverter.java","lineNumber":133,"sourceCode":"     */\n    public UnitsConverter(final Map<Pair<String, String>, AffineConverter> basicConversions) {\n        conversions = computeAllConversions(basicConversions);\n        units = extractUnits(conversions);\n    }\n\n    /**\n     * Converts a value from one unit to another.\n     *\n     * @param inputUnit the unit of the input value.\n     * @param outputUnit the unit to convert the value into.\n     * @param value the value to convert.\n     * @return the converted value in the target unit.\n     * @throws IllegalArgumentException if inputUnit equals outputUnit.\n     * @throws NoSuchElementException if no conversion exists between the units.\n     */\n    public double convert(final String inputUnit, final String outputUnit, final double value) {\n        if (inputUnit.equals(outputUnit)) {\n            throw new IllegalArgumentException(\"inputUnit must be different from outputUnit.\");\n        }\n        final var conversionKey = Pair.of(inputUnit, outputUnit);\n        return conversions.computeIfAbsent(conversionKey, k -> { throw new NoSuchElementException(\"No converter for: \" + k); }).convert(value);\n    }\n\n    /**\n     * Retrieves the set of all units supported by this converter.\n     *\n     * @return a set of available units.\n     */\n    public Set<String> availableUnits() {\n        return units;\n    }\n}\n","sourceCodeStart":115,"sourceCodeEnd":148,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/UnitsConverter.java#L115-L148","documentation":"Thrown by UnitsConverter.convert when inputUnit.equals(outputUnit) — the library refuses no-op conversions and requires distinct units. This check fires before any lookup, so even units not in the converter set will trip this guard if both strings are equal.","triggerScenarios":"Calling converter.convert(\"Celsius\", \"Celsius\", 100), or passing the same variable for both arguments (e.g., convert(unit, unit, val)).","commonSituations":"Building generic pipelines where source and target unit come from the same config field or are user-selected independently; refactoring that accidentally passes the same variable twice.","solutions":["Short-circuit in the caller: if inputUnit.equals(outputUnit), return the value unchanged instead of calling convert.","Validate that the two unit parameters are distinct before invoking convert.","If the same-unit case is valid in your domain, handle it explicitly before the call."],"exampleFix":"// before\nreturn converter.convert(unit, unit, value);\n// after\nif (inputUnit.equals(outputUnit)) {\n    return value;\n}\nreturn converter.convert(inputUnit, outputUnit, value);","handlingStrategy":"validation","validationCode":"static double safeConvert(UnitsConverter c, String in, String out, double v) {\n    if (in == null || out == null) throw new IllegalArgumentException(\"units must not be null\");\n    if (in.equals(out)) return v; // no-op short-circuit\n    return c.convert(in, out, v);\n}","typeGuard":"static boolean isDistinctNonNull(String a, String b) {\n    return a != null && b != null && !a.equals(b);\n}","tryCatchPattern":"try {\n    return converter.convert(inputUnit, outputUnit, value);\n} catch (IllegalArgumentException e) {\n    if (inputUnit.equals(outputUnit)) return value; // legitimate no-op\n    throw e;\n}","preventionTips":["Short-circuit same-unit conversions in the caller before invoking convert.","When source and target units come from independent selectors, validate distinctness on selection change.","Document that same-unit calls are rejected by design."],"tags":["java","unit-conversion","input-validation","illegalargument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}