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

org.devocative.wickomp.formatter.OBooleanFormatter Maven / Gradle / Ivy

There is a newer version: 2.11
Show newest version
package org.devocative.wickomp.formatter;

import org.apache.wicket.model.ResourceModel;

public class OBooleanFormatter implements OFormatter {
	private static final long serialVersionUID = -5789921480586802442L;

	private static String stTrue, stFalse;

	@Override
	public String format(Object value) {
		if (stTrue == null) {
			stTrue = new ResourceModel("label.true", "True").getObject();
			stFalse = new ResourceModel("label.false", "False").getObject();
		}

		if (value instanceof Number) {
			Number no = (Number) value;
			return fromNumber(no);
		}

		if (value instanceof Boolean) {
			Boolean b = (Boolean) value;
			return fromBoolean(b);
		}

		if (value instanceof String) {
			try {
				int intValue = Integer.parseInt(value.toString());
				return fromNumber(intValue);
			} catch (NumberFormatException ignored) {
			}

			return fromBoolean(Boolean.valueOf(value.toString()));
		}
		return null;
	}

	private String fromBoolean(Boolean b) {
		return b ? stTrue : stFalse;
	}

	private String fromNumber(Number no) {
		return no.intValue() == 0 ? stFalse : stTrue;
	}

	private static final OBooleanFormatter BOOLEAN = new OBooleanFormatter();

	public static OBooleanFormatter bool() {
		return BOOLEAN;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy