11/*******************************************************************************
2- * Copyright (c) 2014-2017 Pivotal, Inc.
2+ * Copyright (c) 2014, 2025 Pivotal, Inc.
33 * All rights reserved. This program and the accompanying materials
44 * are made available under the terms of the Eclipse Public License v1.0
55 * which accompanies this distribution, and is available at
1111package org .springframework .ide .vscode .commons .util ;
1212
1313import java .util .Collection ;
14+ import java .util .Set ;
1415import java .util .TreeSet ;
1516import java .util .concurrent .Callable ;
1617import java .util .function .Supplier ;
18+ import java .util .stream .Collectors ;
1719
1820import com .google .common .collect .ImmutableSet ;
1921
@@ -29,6 +31,7 @@ public class EnumValueParser implements ValueParser {
2931 private Supplier <PartialCollection <String >> values ;
3032 private final boolean longRunning ;
3133
34+ private transient Set <String > _canonicalValues ;
3235
3336 public EnumValueParser (String typeName , String ... values ) {
3437 this (typeName , ImmutableSet .copyOf (values ));
@@ -69,20 +72,15 @@ public Object parse(String str) throws Exception {
6972 throw errorOnBlank (createBlankTextErrorMessage ());
7073 }
7174
72- PartialCollection <String > values = this .values .get ();
73-
74- // If values is not fully known then just assume the str is acceptable.
75- if (values == null || !values .isComplete () || hasMatchingValue (str , values .getElements ())) {
75+ Set <String > canonicalValues = getCanonicalValues ();
76+
77+ if (canonicalValues == null || canonicalValues .contains (getCanonicalName (str ))) {
7678 return str ;
7779 } else {
78- throw errorOnParse (createErrorMessage (str , values .getElements ()));
80+ throw errorOnParse (createErrorMessage (str , this . values . get () .getElements ()));
7981 }
8082 }
8183
82- protected boolean hasMatchingValue (String str , Collection <String > values ) {
83- return values .contains (str );
84- }
85-
8684 protected String createBlankTextErrorMessage () {
8785 return "'" + typeName + "'" + " cannot be blank." ;
8886 }
@@ -102,4 +100,21 @@ protected Exception errorOnBlank(String message) {
102100 public boolean longRunning () {
103101 return this .longRunning ;
104102 }
103+
104+ private Set <String > getCanonicalValues () {
105+ PartialCollection <String > partialCollection = this .values .get ();
106+ if (partialCollection != null ) {
107+ _canonicalValues = partialCollection .getElements ().stream ().map (EnumValueParser ::getCanonicalName ).collect (Collectors .toSet ());
108+ }
109+ return _canonicalValues ;
110+ }
111+
112+ static String getCanonicalName (String name ) {
113+ StringBuilder canonicalName = new StringBuilder (name .length ());
114+ name .chars ()
115+ .filter (Character ::isLetterOrDigit )
116+ .map (Character ::toLowerCase )
117+ .forEach ((c ) -> canonicalName .append ((char ) c ));
118+ return canonicalName .toString ();
119+ }
105120}
0 commit comments