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

com.ovh.ws.jsonizer.api.base.ToStringHelper Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2012, OVH. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */
package com.ovh.ws.jsonizer.api.base;

public class ToStringHelper {
	private final StringBuilder builder;
	private boolean needsSeparator;

	public ToStringHelper(String className) {
		builder = new StringBuilder(32).append(className).append('{');
	}

	public ToStringHelper add(String name, Object value) {
		maybeAppendSeparator().append(name).append('=').append(value);
		return this;
	}

	public ToStringHelper addValue(Object value) {
		maybeAppendSeparator().append(value);
		return this;
	}

	@Override
	public String toString() {
		try {
			return builder.append('}').toString();
		} finally {
			builder.setLength(builder.length() - 1);
		}
	}

	private StringBuilder maybeAppendSeparator() {
		if (needsSeparator) {
			return builder.append(", ");
		} else {
			needsSeparator = true;
			return builder;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy