diff --git a/rewrite-hcl/src/main/java/org/openrewrite/hcl/tree/Space.java b/rewrite-hcl/src/main/java/org/openrewrite/hcl/tree/Space.java index 2ff0f3ce46d..bfc823cbcc6 100644 --- a/rewrite-hcl/src/main/java/org/openrewrite/hcl/tree/Space.java +++ b/rewrite-hcl/src/main/java/org/openrewrite/hcl/tree/Space.java @@ -61,8 +61,10 @@ public static Space build(@Nullable String whitespace, List comments) { if (comments.isEmpty()) { if (whitespace == null || whitespace.isEmpty()) { return Space.EMPTY; + } else if (whitespace.length() <= 100) { + //noinspection StringOperationCanBeSimplified + return flyweights.computeIfAbsent(new String(whitespace), k -> new Space(whitespace, comments)); } - return flyweights.computeIfAbsent(whitespace, k -> new Space(whitespace, comments)); } return new Space(whitespace, comments); } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/Space.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/Space.java index 7f48548710c..8c39aab2d09 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/Space.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/Space.java @@ -61,8 +61,10 @@ public static Space build(@Nullable String whitespace, List comments) { if (comments.isEmpty()) { if (whitespace == null || whitespace.isEmpty()) { return Space.EMPTY; + } else if (whitespace.length() <= 100) { + //noinspection StringOperationCanBeSimplified + return flyweights.computeIfAbsent(new String(whitespace), k -> new Space(whitespace, comments)); } - return flyweights.computeIfAbsent(whitespace, k -> new Space(whitespace, comments)); } return new Space(whitespace, comments); } diff --git a/rewrite-json/src/main/java/org/openrewrite/json/tree/Space.java b/rewrite-json/src/main/java/org/openrewrite/json/tree/Space.java index add9e5dcdb3..bb1a1347aaa 100644 --- a/rewrite-json/src/main/java/org/openrewrite/json/tree/Space.java +++ b/rewrite-json/src/main/java/org/openrewrite/json/tree/Space.java @@ -60,8 +60,10 @@ public static Space build(@Nullable String whitespace, List comments) { if (comments.isEmpty()) { if (whitespace == null || whitespace.isEmpty()) { return Space.EMPTY; + } else if (whitespace.length() <= 100) { + //noinspection StringOperationCanBeSimplified + return flyweights.computeIfAbsent(new String(whitespace), k -> new Space(whitespace, comments)); } - return flyweights.computeIfAbsent(whitespace, k -> new Space(whitespace, comments)); } return new Space(whitespace, comments); } diff --git a/rewrite-protobuf/src/main/java/org/openrewrite/protobuf/tree/Space.java b/rewrite-protobuf/src/main/java/org/openrewrite/protobuf/tree/Space.java index 4b1d5445e09..138722b5e09 100644 --- a/rewrite-protobuf/src/main/java/org/openrewrite/protobuf/tree/Space.java +++ b/rewrite-protobuf/src/main/java/org/openrewrite/protobuf/tree/Space.java @@ -60,8 +60,10 @@ public static Space build(@Nullable String whitespace, List comments) { if (comments.isEmpty()) { if (whitespace == null || whitespace.isEmpty()) { return Space.EMPTY; + } else if (whitespace.length() <= 100) { + //noinspection StringOperationCanBeSimplified + return flyweights.computeIfAbsent(new String(whitespace), k -> new Space(whitespace, comments)); } - return flyweights.computeIfAbsent(whitespace, k -> new Space(whitespace, comments)); } return new Space(whitespace, comments); } diff --git a/tools/language-parser-builder/src/main/java/org/openrewrite/toml/tree/Space.java b/tools/language-parser-builder/src/main/java/org/openrewrite/toml/tree/Space.java index f5826e40b8e..ea577d76ba9 100644 --- a/tools/language-parser-builder/src/main/java/org/openrewrite/toml/tree/Space.java +++ b/tools/language-parser-builder/src/main/java/org/openrewrite/toml/tree/Space.java @@ -48,8 +48,13 @@ private Space(@Nullable String whitespace) { @JsonCreator public static Space build(@Nullable String whitespace) { - if (whitespace == null || whitespace.isEmpty()) { - return Space.EMPTY; + if (comments.isEmpty()) { + if (whitespace == null || whitespace.isEmpty()) { + return Space.EMPTY; + } else if (whitespace.length() <= 100) { + //noinspection StringOperationCanBeSimplified + return flyweights.computeIfAbsent(new String(whitespace), k -> new Space(whitespace, comments)); + } } return flyweights.computeIfAbsent(whitespace, k -> new Space(whitespace)); }