{"record":{"id":"06beca357174c3bb","repo":"stanfordnlp/CoreNLP","slug":"unrecognized-numeric-type-in-wrapped-counter","errorCode":null,"errorMessage":"Unrecognized numeric type in wrapped counter","messagePattern":"Unrecognized numeric type in wrapped counter","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/Counters.java","lineNumber":2706,"sourceCode":"                    return entry.getValue().doubleValue();\n                  }\n\n                  public Double setValue(Double value) {\n                    final double lastValue = entry.getValue().doubleValue();\n                    double rv;\n\n                    if (type == Double.class) {\n                      rv = ErasureUtils.<Entry<E, Double>> uncheckedCast(entry).setValue(value);\n                    } else if (type == Integer.class) {\n                      rv = ErasureUtils.<Entry<E, Integer>> uncheckedCast(entry).setValue(value.intValue());\n                    } else if (type == Float.class) {\n                      rv = ErasureUtils.<Entry<E, Float>> uncheckedCast(entry).setValue(value.floatValue());\n                    } else if (type == Long.class) {\n                      rv = ErasureUtils.<Entry<E, Long>> uncheckedCast(entry).setValue(value.longValue());\n                    } else if (type == Short.class) {\n                      rv = ErasureUtils.<Entry<E, Short>> uncheckedCast(entry).setValue(value.shortValue());\n                    } else {\n                      throw new RuntimeException(\"Unrecognized numeric type in wrapped counter\");\n                    }\n\n                    // need to call getValue().doubleValue() to make sure\n                    // we keep the same precision as the underlying map\n                    total += entry.getValue().doubleValue() - lastValue;\n\n                    return rv;\n                  }\n                };\n              }\n\n              public void remove() {\n                total -= lastEntry.getValue().doubleValue();\n                it.remove();\n              }\n            };\n          }\n","sourceCodeStart":2688,"sourceCodeEnd":2724,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/Counters.java#L2688-L2724","documentation":"Counters.fromMap wraps a Map's entry set in a Counter view whose mutators cast values to the concrete numeric type captured when the view was created. If setCount is called with a type that the wrapper does not recognize (not one of the handled Integer/Double/Float/Long/Short classes), it throws this RuntimeException.","triggerScenarios":"Calling setCount on a Counter produced by Counters.fromMap(map, typeClass) where typeClass is a numeric type outside the supported set (e.g. BigDecimal, BigInteger, Byte), or a mismatched class due to unchecked generics.","commonSituations":"Passing BigInteger/BigDecimal maps from math or crypto code; changing the map's value type after refactoring without updating the explicit class argument; raw-type usage that defeats generic checking.","solutions":["Use a supported numeric type for the map values (Integer, Double, Float, Long, Short)","Copy the map to a plain ClassicCounter<Double> instead of using the fromMap view","Check the exact class passed as the second argument to fromMap — it must match map.values()' runtime type"],"exampleFix":"// before\nCounter<String> c = Counters.fromMap(bigDecimalMap, BigDecimal.class); // throws on setCount\n// after\nMap<String, Double> doubles = new HashMap<>();\nbigDecimalMap.forEach((k, v) -> doubles.put(k, v.doubleValue()));\nCounter<String> c = Counters.fromMap(doubles, Double.class);","handlingStrategy":"type-guard","validationCode":"Class<?> type = map.values().iterator().next().getClass();\nSet<Class<?>> supported = Set.of(Integer.class, Double.class, Float.class, Long.class, Short.class);\nif (!supported.contains(type)) {\n  throw new IllegalArgumentException(\"Unsupported numeric type: \" + type);\n}","typeGuard":"static boolean isSupportedNumeric(Object v) {\n  return v instanceof Integer || v instanceof Double || v instanceof Float\n      || v instanceof Long || v instanceof Short;\n}","tryCatchPattern":"try {\n  counterView.setCount(key, value);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"Unrecognized numeric type\")) {\n    throw new IllegalStateException(\"Use a supported numeric type (Integer/Double/Float/Long/Short)\", e);\n  }\n  throw e;\n}","preventionTips":["Stick to Integer/Double/Float/Long/Short as map value types for fromMap","Always pass the explicit class matching the map's runtime value type","Convert exotic numerics (BigDecimal/BigInteger/Byte) to double before wrapping"],"tags":["java","counter","type-mismatch","numeric"],"backgroundTag":"type-mismatch","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}