private Object findValidOption(final List<?> options, final Object selected,
			final boolean handleInvalid) {

		// No selection made
		if (selected == null) {
			// No Selection required, or no options to select
			if (isAllowNoSelection() || options == null || options.isEmpty()) {
				return null;
			} else { // Selection is required
				// Check if NULL is an option
				if (SelectListUtil.containsOption(options, null)) {
					return null;
				}

				// Must have a selected option
				throw new IllegalStateException(
						"A valid option must be selected for lists that require a selected option.");
			}
		}

		// Check we have a valid option and if we find it via Option/Code or Legacy matching, return the matching option
		if (options != null) {
			Object option = SelectListUtil.getOptionWithMatching(options, selected);
			if (option != null) {
				return option;
			}
		}

		// Editable with User Entered Text
		if (isEditable() && selected instanceof String) {
			return selected;
		}

		// Handle invalid option
		if (handleInvalid) {
			Object valid = doHandleInvalidOption(selected);
			setSelected(valid);
			return valid;
		} else {
			throw new IllegalStateException(
					"The selected option \"" + selected + "\" is not an available option.");
		}

	}
private Object findValidOption ( final List < ? > options , final Object selected , final boolean handleInvalid ) { if ( selected == null ) { if ( isAllowNoSelection ( ) || options == null || options . isEmpty ( ) ) { return null ; } else { if ( SelectListUtil . containsOption ( options , null ) ) { return null ; } throw new IllegalStateException ( "A valid option must be selected for lists that require a selected option." ) ; } } if ( options != null ) { Object option = SelectListUtil . getOptionWithMatching ( options , selected ) ; if ( option != null ) { return option ; } } if ( isEditable ( ) && selected instanceof String ) { return selected ; } if ( handleInvalid ) { Object valid = doHandleInvalidOption ( selected ) ; setSelected ( valid ) ; return valid ; } else { throw new IllegalStateException ( "The selected option \"" + selected + "\" is not an available option." ) ; } }
Find the valid option for the selected data, allowing for option/code and legacy matching.

@param options the list of options
@param selected the option to search for
@param handleInvalid true if allow handle invalid option
@return the valid option