All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.validator.internal.engine.messageinterpolation.parser.TokenIterator Maven / Gradle / Ivy

There is a newer version: 8.0.1.Final
Show newest version
/*
 * Hibernate Validator, declare and validate application constraints
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or .
 */
package org.hibernate.validator.internal.engine.messageinterpolation.parser;

import java.util.ArrayList;
import java.util.List;

/**
 * Allows to iterate over a list of message tokens and replace parameters.
 *
 * @author Hardy Ferentschik
 */
public class TokenIterator {
	private final List tokenList;

	private int currentPosition;
	private Token currentToken;
	private boolean allInterpolationTermsProcessed;
	private boolean currentTokenAvailable;

	public TokenIterator(List tokens) {
		this.tokenList = new ArrayList( tokens );
	}

	/**
	 * Called to advance the next interpolation term of the message descriptor. This message can be called multiple times.
	 * Once it returns {@code false} all interpolation terms have been processed and {@link #getInterpolatedMessage()}
	 * can be called.
	 *
	 * @return Returns {@code true} in case there are more message parameters, {@code false} otherwise.
	 *
	 * @throws MessageDescriptorFormatException in case the message descriptor is invalid
	 */
	public boolean hasMoreInterpolationTerms() throws MessageDescriptorFormatException {
		while ( currentPosition < tokenList.size() ) {
			currentToken = tokenList.get( currentPosition );
			currentPosition++;
			if ( currentToken.isParameter() ) {
				currentTokenAvailable = true;
				return true;
			}
		}
		allInterpolationTermsProcessed = true;
		return false;
	}

	/**
	 * @return Returns the next interpolation term
	 */
	public String nextInterpolationTerm() {
		if ( !currentTokenAvailable ) {
			throw new IllegalStateException(
					"Trying to call #nextInterpolationTerm without calling #hasMoreInterpolationTerms"
			);
		}
		currentTokenAvailable = false;
		return currentToken.getTokenValue();
	}

	/**
	 * Replaces the current interpolation term with the given string.
	 *
	 * @param replacement The string to replace the current term with.
	 */
	public void replaceCurrentInterpolationTerm(String replacement) {
		Token token = new Token( replacement );
		token.terminate();
		tokenList.set( currentPosition - 1, token );
	}

	public String getInterpolatedMessage() {
		if ( !allInterpolationTermsProcessed ) {
			throw new IllegalStateException( "Not all interpolation terms have been processed yet." );
		}
		StringBuilder messageBuilder = new StringBuilder();
		for ( Token token : tokenList ) {
			messageBuilder.append( token.getTokenValue() );
		}

		return messageBuilder.toString();
	}
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy