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

org.hibernate.dialect.Replacer Maven / Gradle / Ivy

There is a newer version: 6.6.2.Final
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.dialect;

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

import org.hibernate.internal.util.StringHelper;

/**
 * @author Gavin King
 */
public class Replacer {
	private final String[] chunks;
	private final String quote;
	private final String delimiter;
	private final List replacements = new ArrayList<>();

	static class Replacement {
		String placeholder;
		String replacement;

		Replacement(String placeholder, String replacement) {
			this.placeholder = placeholder;
			this.replacement = replacement;
		}

//		boolean matches(StringBuilder string, int position) {
//			return string.indexOf( placeholder, position ) >= 0;
//		}
//
		int apply(StringBuilder string, int position) {
			if ( position + placeholder.length() > string.length() ) {
				return -1;
			}
			for (int index = 0; index < placeholder.length(); index++ ) {
				if ( string.charAt( position + index ) != placeholder.charAt( index ) ) {
					return -1;
				}
			}
			string.replace( position, position + placeholder.length(), replacement );
			return replacement.length();
		}
	}

	public Replacer(String format, String quote, String delimiter) {
		this.delimiter = delimiter;
		this.chunks = StringHelper.splitFull( quote, format );
		this.quote = quote;
	}

	public Replacer replace(String placeholder, String replacement) {
		for ( Replacement old : replacements ) {
			if ( old.placeholder.equals( placeholder ) ) {
				old.replacement = replacement;
				return this;
			}
		}
		replacements.add( new Replacement( placeholder, replacement ) );
		return this;
	}

	public String result() {
		for ( int i=0; i= 0 ) {
						position += result - 1;
						break;
					}
				}
			}
			chunks[i] = chunk.toString();
		}
		for ( int i=1; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy