Java; Using Flag Classes to Replace boolean values.
Let me supply some background information before talking about the Flag classes. Part of my project involves using Java classes to access XML. I don't want to use the currently available unmarshalling tools because I only need part of the XML data inside my Java application. Therefore, I use XPATHs to select the data as needed. In order to ensure that I can use the same class to selectively unmarshall from XML and provide the ability to run test cases, I use the following technique:
private String senderName = null; public String getSenderName() { if (this.senderName == null) { this.senderName = XmlHelper.getTextAtXpath(getCurrentElement(), "./senderName"); } return this.senderName; } public void setSenderName(final String _senderName) { this.senderName = _senderName; }
This technique works fine for strings because they are nullable. However, boolean values are harder to handle. So I create a Flag class:
public class MsgIsParent{ private boolean value; public static final MsgIsParent TRUE = new MsgIsParent(true); public static final MsgIsParent FALSE = new MsgIsParent(false); private MsgIsParent(final boolean _value) { this.value = _value; } public static MsgIsParent factory(final boolean _value) { if (_value) { return TRUE; } else { return FALSE; } } }
With this small helper available, I can follow the same technique that I used for Strings:
private MsgIsParent msgIsParent = null; public MsgIsParent getMsgIsParent() { if (this.msgIsParent == null) { this.msgIsParent = MsgIsParent.factory(getRootElement().getNodeName().equals(getMessageType())); } return this.msgIsParent; } public void setMsgIsParent(final MsgIsParent _msgIsParent) { this.msgIsParent = _msgIsParent; }