<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="https://www.taitl.com/an/skins/common/feed.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>Andrey - New pages [en]</title>
		<link>https://www.taitl.com/andrey/Special:Newpages</link>
		<description>From Andrey</description>
		<language>en</language>
		<generator>MediaWiki 1.5.6</generator>
		<lastBuildDate>Sun, 03 May 2026 12:21:45 GMT</lastBuildDate>
		<item>
			<title>My coding style</title>
			<link>https://www.taitl.com/andrey/My_coding_style</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;When coding, I try to make it as readable as possible - and encourage others to do the same. Here is an utility class I wrote to for use in command line applications. It is surprising how often command line apps directly validate their argument arrays, while it is possible to delegate at least some logic to some kind of rule-driven parser. Here's the helping hand:&lt;br /&gt;
&lt;br /&gt;
[[CommandLineParser.java]]&lt;br /&gt;
&lt;br /&gt;
[[CommandLineParserTest.java]]&lt;/div&gt;</description>
			<pubDate>Sat, 22 Oct 2011 22:35:41 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:My_coding_style</comments>		</item>
		<item>
			<title>CommandLineParserTest.java</title>
			<link>https://www.taitl.com/andrey/CommandLineParserTest.java</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
package com.taitl.world.commandline;&lt;br /&gt;
&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
import org.junit.After;&lt;br /&gt;
import org.junit.Before;&lt;br /&gt;
import org.junit.Test;&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright 2011 Taitl Design. All rights reserved.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Tests for CommandLineParser class.&lt;br /&gt;
 * &lt;br /&gt;
 * @author Andrey Potekhin&lt;br /&gt;
 * &lt;br /&gt;
 */&lt;br /&gt;
public class CommandLineParserTest extends TestCase&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
	CommandLineParser commandLineParser = null;&lt;br /&gt;
	CommandLineParser newCommandLineParser = null;&lt;br /&gt;
&lt;br /&gt;
	// Error strings&lt;br /&gt;
	static final String MISSING_EXCEPTION = &amp;quot;Exception not thrown where it should&amp;quot;;&lt;br /&gt;
	static final String ERRONEOUS_EXCEPTION = &amp;quot;Exception thrown where it shouldn't&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	// Trivial command lines&lt;br /&gt;
	static final String cmdlineNULL = null;&lt;br /&gt;
	static final String cmdlineEmpty = &amp;quot;&amp;quot;;&lt;br /&gt;
	static final String cmdlineUsage = &amp;quot;--prev --usage --next&amp;quot;;&lt;br /&gt;
	static final String cmdlineVersion = &amp;quot;--prev --version --next&amp;quot;;&lt;br /&gt;
	static final String cmdlineHelp = &amp;quot;--prev --help --next&amp;quot;;&lt;br /&gt;
	static final String cmdlineHelpWithArgument = &amp;quot;--prev --help general --next&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	// Values of switches&lt;br /&gt;
	static final String cmdlineNoValue = &amp;quot;--novalue --next&amp;quot;;&lt;br /&gt;
	static final String cmdlineOneValue = &amp;quot;--onevalue val --next&amp;quot;;&lt;br /&gt;
	static final String cmdlineOneValueOnEnd = &amp;quot;--prev --onevalue val&amp;quot;;&lt;br /&gt;
	static final String cmdlineTwoValues = &amp;quot;--twovalues val1 val2 --next&amp;quot;;&lt;br /&gt;
	static final String cmdlineTwoValuesOnEnd = &amp;quot;--prev --twovalues val1 val2&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	static final String cmdlineThreeValues = &amp;quot;--threevalues val1 val2 val3 --next&amp;quot;;&lt;br /&gt;
	static final String cmdlineThreeValuesOnEnd = &amp;quot;--prev --threevalues val1 val2 val3&amp;quot;;&lt;br /&gt;
	static final String cmdlineOneToThree = &amp;quot;--prev --onetothree val1 val2 --next&amp;quot;;&lt;br /&gt;
	static final String cmdlineOneToThreeTooFew = &amp;quot;--prev --onetothree --next&amp;quot;;&lt;br /&gt;
	static final String cmdlineOneToThreeTooMany = &amp;quot;--prev --onetothree val1 val2 val3 val4 --next&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	// Multiple values of a switch&lt;br /&gt;
	static final String cmdlineMultiValues = &amp;quot;--prev --multi val1 val2 val3 val4 val5 --next&amp;quot;;&lt;br /&gt;
	static final String cmdlineMultiValuesOnEnd = &amp;quot;--prev --multi val1 val2 val3 val4 val5&amp;quot;;&lt;br /&gt;
	static final String cmdlineMany = &amp;quot;--prev --many val1 val2 \&amp;quot;val3\&amp;quot; val4 val5 val6 --next&amp;quot;;&lt;br /&gt;
	static final String cmdlineManyTooFew = &amp;quot;--prev --many val1 \&amp;quot;val2\&amp;quot; --next&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	// Default switch&lt;br /&gt;
	static final String cmdlineImplicitSwitch = &amp;quot;--prev file.txt --next&amp;quot;;&lt;br /&gt;
	static final String cmdlineImplicitSwitchTooManyValues = &amp;quot;--prev file.txt file2.txt --next&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	// Quoted arguments&lt;br /&gt;
	static final String cmdlineQuote = &amp;quot;--prev --onevalue \&amp;quot;val1 --next val2 val3\&amp;quot; --next&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	// Switchless arguments&lt;br /&gt;
	static final String cmdlineSwitchlessArguments1 = &amp;quot;--prev file1.txt --next file2.txt file3.txt file4.txt&amp;quot;;&lt;br /&gt;
	static final String cmdlineSwitchlessArguments2 = &amp;quot;file1.txt --prev file2.txt file3.txt --next file4.txt&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	static final String possibleSwitches = &amp;quot;--usage(0) --version(0) --help(0-1) --prev(0) --next(0)&amp;quot;&lt;br /&gt;
			+ &amp;quot; --novalue(0) --onevalue(1) --twovalues(2) --threevalues(3) --onetothree(1-3)&amp;quot;&lt;br /&gt;
			+ &amp;quot; --multi(*) --many(3-*)&amp;quot;;&lt;br /&gt;
	static final String possibleThreeSwitches = &amp;quot;--prev(0) --next(0) --onevalue(1)&amp;quot;;&lt;br /&gt;
	static final String incorrectPossibleSwitches1 = &amp;quot;--usage&amp;quot;;&lt;br /&gt;
	static final String incorrectPossibleSwitches2 = &amp;quot;--usage(?)&amp;quot;;&lt;br /&gt;
	static final String incorrectPossibleSwitches3 = &amp;quot;--usage(3-2)&amp;quot;;&lt;br /&gt;
	static final String incorrectPossibleSwitches4 = &amp;quot;--usage[0]&amp;quot;;&lt;br /&gt;
	static final String implicitSwitch = &amp;quot;--file(1)&amp;quot;;&lt;br /&gt;
	static final String implicitSwitchAlternative = &amp;quot;--file&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	/** Sets up test class object. */&lt;br /&gt;
	@Override&lt;br /&gt;
	@Before&lt;br /&gt;
	public void setUp() throws Exception&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser = new CommandLineParser();&lt;br /&gt;
		commandLineParser.setPossibleSwitches(possibleSwitches);&lt;br /&gt;
		commandLineParser.setImplicitSwitch(implicitSwitch);&lt;br /&gt;
		newCommandLineParser = new CommandLineParser(); // Not to be initialized&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Performs cleanup of objects created in setUp() method.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @throws Exception&lt;br /&gt;
	 *             An exception to indicate problems during tear-down.&lt;br /&gt;
	 */&lt;br /&gt;
	@Override&lt;br /&gt;
	@After&lt;br /&gt;
	public void tearDown() throws Exception&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser = null;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/*&lt;br /&gt;
	 * Trivial tests&lt;br /&gt;
	 */&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testCommandLineParser()&lt;br /&gt;
	{&lt;br /&gt;
		assertFalse(commandLineParser.isInitialized());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testSetPossibleSwitches()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setPossibleSwitches(possibleThreeSwitches);&lt;br /&gt;
		final int requiredNumberOfSwitches = 3;&lt;br /&gt;
		assertEquals(requiredNumberOfSwitches, commandLineParser&lt;br /&gt;
				.getPossibleSwitches().size());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testSetCommandLine()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineUsage);&lt;br /&gt;
		assertEquals(cmdlineUsage, commandLineParser.getOriginalCommandLine());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testSetArguments()&lt;br /&gt;
	{&lt;br /&gt;
		String commandLine;&lt;br /&gt;
		String[] arguments;&lt;br /&gt;
		arguments = new String[] { &amp;quot;--prev&amp;quot;, &amp;quot;--usage&amp;quot;, &amp;quot;--next&amp;quot; };&lt;br /&gt;
		commandLineParser.setArguments(arguments);&lt;br /&gt;
		assertEquals(&amp;quot;--prev --usage --next&amp;quot;,&lt;br /&gt;
				commandLineParser.getOriginalCommandLine());&lt;br /&gt;
&lt;br /&gt;
		arguments = new String[] { &amp;quot;--prev&amp;quot;, &amp;quot;--usage&amp;quot;,&lt;br /&gt;
				&amp;quot;usagevalue1 usagevalue2&amp;quot;, &amp;quot;--next&amp;quot; };&lt;br /&gt;
		commandLineParser.setArguments(arguments);&lt;br /&gt;
		commandLine = commandLineParser.getOriginalCommandLine();&lt;br /&gt;
		assertEquals(&amp;quot;--prev --usage \&amp;quot;usagevalue1 usagevalue2\&amp;quot; --next&amp;quot;,&lt;br /&gt;
				commandLine);&lt;br /&gt;
		arguments = commandLineParser.getArguments();&lt;br /&gt;
		assertTrue(arguments.length == 4);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testParse()&lt;br /&gt;
	{&lt;br /&gt;
		// String[] arguments = new String[] { &amp;quot;--prev&amp;quot;, &amp;quot;--usage&amp;quot;, &amp;quot;--next&amp;quot; };&lt;br /&gt;
&lt;br /&gt;
		// commandLineParser.setCommandLine(cmdlineUsage); // Calls parse()&lt;br /&gt;
		// method&lt;br /&gt;
		// assertTrue(commandLineParser.isInitialized());&lt;br /&gt;
&lt;br /&gt;
		newCommandLineParser.setPossibleSwitches(possibleSwitches);&lt;br /&gt;
&lt;br /&gt;
		// Try parsing without specifying command line arguments&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			newCommandLineParser.parse();&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException ise)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			newCommandLineParser.doParsing(null);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException ise)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		newCommandLineParser.setCommandLine(cmdlineUsage);&lt;br /&gt;
		assertTrue(newCommandLineParser.isInitialized());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testGetArguments()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineQuote);&lt;br /&gt;
		String[] arguments = commandLineParser.getArguments();&lt;br /&gt;
		assertTrue(arguments.length == 4);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testAddPossibleSwitch()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setPossibleSwitches(possibleThreeSwitches);&lt;br /&gt;
		assertTrue(commandLineParser.isPossibleSwitch(&amp;quot;--prev&amp;quot;));&lt;br /&gt;
		assertFalse(commandLineParser.isPossibleSwitch(&amp;quot;--usage&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
		// Try adding a malformed switch&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.addPossibleSwitch(&amp;quot;--usage&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.addPossibleSwitch(&amp;quot;--usage(0)&amp;quot;);&lt;br /&gt;
		assertTrue(commandLineParser.isPossibleSwitch(&amp;quot;--usage&amp;quot;));&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testRemovePossibleSwitch()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setPossibleSwitches(possibleThreeSwitches);&lt;br /&gt;
&lt;br /&gt;
		// Try removing a non-existing switch&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.removePossibleSwitch(&amp;quot;--nonexisting&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Remove switch with fully-specified name (including cardinality)&lt;br /&gt;
		commandLineParser.removePossibleSwitch(&amp;quot;--next(0)&amp;quot;);&lt;br /&gt;
		int requiredNumberOfSwitches = 2;&lt;br /&gt;
		assertEquals(requiredNumberOfSwitches, commandLineParser&lt;br /&gt;
				.getPossibleSwitches().size());&lt;br /&gt;
&lt;br /&gt;
		// Try removing a malformed switch&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.removePossibleSwitch(&amp;quot;--prev(0&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Try removing a malformed switch&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.removePossibleSwitch(&amp;quot;--prev0)&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Remove switch with omitted cardinality&lt;br /&gt;
		commandLineParser.removePossibleSwitch(&amp;quot;--prev&amp;quot;);&lt;br /&gt;
		requiredNumberOfSwitches = 1;&lt;br /&gt;
		assertEquals(requiredNumberOfSwitches, commandLineParser&lt;br /&gt;
				.getPossibleSwitches().size());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testIsSwitchPresent()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineUsage);&lt;br /&gt;
		assertTrue(commandLineParser.isSwitchPresent(commandLineParser&lt;br /&gt;
				.getUsageSwitchName()));&lt;br /&gt;
		assertFalse(commandLineParser.isSwitchPresent(commandLineParser&lt;br /&gt;
				.getHelpSwitchName()));&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testGetSwitchValue()&lt;br /&gt;
	{&lt;br /&gt;
		String switchValue = null;&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineNoValue);&lt;br /&gt;
		assertTrue(commandLineParser.isSwitchPresent(&amp;quot;--novalue&amp;quot;));&lt;br /&gt;
		assertTrue(commandLineParser.isSwitchPresent(&amp;quot;--next&amp;quot;));&lt;br /&gt;
		assertFalse(commandLineParser.isSwitchPresent(&amp;quot;--prev&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.getSwitchValue(&amp;quot;--prev&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		switchValue = commandLineParser.getSwitchValue(&amp;quot;--novalue&amp;quot;);&lt;br /&gt;
		assertEquals(switchValue, &amp;quot;&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineOneValue);&lt;br /&gt;
		assertTrue(commandLineParser.isSwitchPresent(&amp;quot;--onevalue&amp;quot;));&lt;br /&gt;
		switchValue = commandLineParser.getSwitchValue(&amp;quot;--onevalue&amp;quot;);&lt;br /&gt;
		assertEquals(switchValue, &amp;quot;val&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineOneValueOnEnd);&lt;br /&gt;
		assertTrue(commandLineParser.isSwitchPresent(&amp;quot;--onevalue&amp;quot;));&lt;br /&gt;
		switchValue = commandLineParser.getSwitchValue(&amp;quot;--onevalue&amp;quot;);&lt;br /&gt;
		assertEquals(switchValue, &amp;quot;val&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineTwoValues);&lt;br /&gt;
		assertTrue(commandLineParser.isSwitchPresent(&amp;quot;--twovalues&amp;quot;));&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			// An exception must be thrown if switch has more than one value&lt;br /&gt;
			switchValue = commandLineParser.getSwitchValue(&amp;quot;--twovalues&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testGetSwitchValues()&lt;br /&gt;
	{&lt;br /&gt;
		List&amp;lt;String&amp;gt; switchValues = null;&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineNoValue);&lt;br /&gt;
		assertTrue(commandLineParser.isSwitchPresent(&amp;quot;--novalue&amp;quot;));&lt;br /&gt;
		assertTrue(commandLineParser.isSwitchPresent(&amp;quot;--next&amp;quot;));&lt;br /&gt;
		assertFalse(commandLineParser.isSwitchPresent(&amp;quot;--prev&amp;quot;));&lt;br /&gt;
		switchValues = commandLineParser.getSwitchValues(&amp;quot;--novalue&amp;quot;);&lt;br /&gt;
		assertEquals(switchValues.size(), 0);&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineOneValue);&lt;br /&gt;
		switchValues = commandLineParser.getSwitchValues(&amp;quot;--onevalue&amp;quot;);&lt;br /&gt;
		assertEquals(switchValues.size(), 1);&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineOneValueOnEnd);&lt;br /&gt;
		assertEquals(switchValues.size(), 1);&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineTwoValues);&lt;br /&gt;
		switchValues = commandLineParser.getSwitchValues(&amp;quot;--twovalues&amp;quot;);&lt;br /&gt;
		assertEquals(switchValues.size(), 2);&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineTwoValuesOnEnd);&lt;br /&gt;
		switchValues = commandLineParser.getSwitchValues(&amp;quot;--twovalues&amp;quot;);&lt;br /&gt;
		assertEquals(switchValues.size(), 2);&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineThreeValues);&lt;br /&gt;
		switchValues = commandLineParser.getSwitchValues(&amp;quot;--threevalues&amp;quot;);&lt;br /&gt;
		assertEquals(switchValues.size(), 3);&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineThreeValuesOnEnd);&lt;br /&gt;
		switchValues = commandLineParser.getSwitchValues(&amp;quot;--threevalues&amp;quot;);&lt;br /&gt;
		assertEquals(switchValues.size(), 3);&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineOneToThree);&lt;br /&gt;
		switchValues = commandLineParser.getSwitchValues(&amp;quot;--onetothree&amp;quot;);&lt;br /&gt;
		assertEquals(switchValues.size(), 2);&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineMultiValues);&lt;br /&gt;
		switchValues = commandLineParser.getSwitchValues(&amp;quot;--multi&amp;quot;);&lt;br /&gt;
		assertTrue(switchValues.size() &amp;gt;= 4);&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineMultiValuesOnEnd);&lt;br /&gt;
		switchValues = commandLineParser.getSwitchValues(&amp;quot;--multi&amp;quot;);&lt;br /&gt;
		assertTrue(switchValues.size() &amp;gt;= 4);&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineMany);&lt;br /&gt;
		switchValues = commandLineParser.getSwitchValues(&amp;quot;--many&amp;quot;);&lt;br /&gt;
		assertTrue(switchValues.size() &amp;gt;= 4);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testCreateSwitch()&lt;br /&gt;
	{&lt;br /&gt;
		Switch s;&lt;br /&gt;
&lt;br /&gt;
		s = commandLineParser.createSwitch(&amp;quot;--prev(0)&amp;quot;);&lt;br /&gt;
		assertTrue(s != null &amp;amp;&amp;amp; s.getMinValues() == 0 &amp;amp;&amp;amp; s.getMaxValues() == 0);&lt;br /&gt;
		// Now try creating the switch by only specifying its name - without&lt;br /&gt;
		// cardinality&lt;br /&gt;
		s = commandLineParser.createSwitch(&amp;quot;--prev&amp;quot;);&lt;br /&gt;
		assertTrue(s != null &amp;amp;&amp;amp; s.getMinValues() == 0 &amp;amp;&amp;amp; s.getMaxValues() == 0);&lt;br /&gt;
&lt;br /&gt;
		// Attempt to redefine cardinality of existing switch (&amp;quot;--prev(0)&amp;quot;)&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.addPossibleSwitch(&amp;quot;--prev(1)&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Try creating a switch with malformed specification&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.createSwitch(&amp;quot;--prev(0&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.createSwitch(&amp;quot;--prev0)&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Try creating a switch with incorrect cardinality (different from&lt;br /&gt;
		// what was specified in setPossibleSwitches()&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.createSwitch(&amp;quot;--prev(1)&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Try creating a switch with incorrect cardinality (non-number)&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.addPossibleSwitch(&amp;quot;--myswitch1(x)&amp;quot;);&lt;br /&gt;
			commandLineParser.createSwitch(&amp;quot;--myswitch1(x)&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.addPossibleSwitch(&amp;quot;--myswitch2(1-x)&amp;quot;);&lt;br /&gt;
			commandLineParser.createSwitch(&amp;quot;--myswitch2(1-x)&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.addPossibleSwitch(&amp;quot;--myswitch3(x-100)&amp;quot;);&lt;br /&gt;
			commandLineParser.createSwitch(&amp;quot;--myswitch3(x-100)&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Try creating a switch which does not exist among possible switches&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.createSwitch(&amp;quot;--nonexisting(0)&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/*******************************&lt;br /&gt;
	 * Trivial tests&lt;br /&gt;
	 *******************************/&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testGetSwitchValueCount()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineThreeValues);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValueCount(&amp;quot;--threevalues&amp;quot;), 3);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testGetOriginalCommandLine()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineUsage);&lt;br /&gt;
		assertEquals(cmdlineUsage, commandLineParser.getOriginalCommandLine());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testIsUsageRequested()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineHelp);&lt;br /&gt;
		assertFalse(commandLineParser.isUsageRequested());&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineUsage);&lt;br /&gt;
		assertTrue(commandLineParser.isUsageRequested());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testIsHelpRequested()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineUsage);&lt;br /&gt;
		assertFalse(commandLineParser.isHelpRequested());&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineHelp);&lt;br /&gt;
		assertTrue(commandLineParser.isHelpRequested());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testIsVersionRequested()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineEmpty);&lt;br /&gt;
		assertFalse(commandLineParser.isVersionRequested());&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineVersion);&lt;br /&gt;
		assertTrue(commandLineParser.isVersionRequested());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testGetUsageSwitch()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setUsageSwitchName(&amp;quot;a&amp;quot;);&lt;br /&gt;
		assertEquals(&amp;quot;a&amp;quot;, commandLineParser.getUsageSwitchName());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testSetUsageSwitch()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setUsageSwitchName(&amp;quot;a&amp;quot;);&lt;br /&gt;
		assertEquals(&amp;quot;a&amp;quot;, commandLineParser.getUsageSwitchName());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testGetHelpSwitch()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setHelpSwitchName(&amp;quot;a&amp;quot;);&lt;br /&gt;
		assertEquals(&amp;quot;a&amp;quot;, commandLineParser.getHelpSwitchName());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testSetHelpSwitch()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setHelpSwitchName(&amp;quot;a&amp;quot;);&lt;br /&gt;
		assertEquals(&amp;quot;a&amp;quot;, commandLineParser.getHelpSwitchName());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testGetVersionSwitch()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setVersionSwitchName(&amp;quot;a&amp;quot;);&lt;br /&gt;
		assertEquals(&amp;quot;a&amp;quot;, commandLineParser.getVersionSwitchName());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testSetVersionSwitch()&lt;br /&gt;
	{&lt;br /&gt;
		commandLineParser.setVersionSwitchName(&amp;quot;a&amp;quot;);&lt;br /&gt;
		assertEquals(&amp;quot;a&amp;quot;, commandLineParser.getVersionSwitchName());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testRequireInitialization()&lt;br /&gt;
	{&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			newCommandLineParser.requireInitialization();&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			newCommandLineParser.setPossibleSwitches(possibleSwitches);&lt;br /&gt;
			newCommandLineParser.requireInitialization();&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			// Specify erroneous command line to prevent parse() from completing&lt;br /&gt;
			newCommandLineParser.setCommandLine(cmdlineUsage + &amp;quot; --unknown&amp;quot;);&lt;br /&gt;
			// newCommandLineParser.requireInitialization();&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
			// fail(ERRONEOUS_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			newCommandLineParser.requireInitialization();&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException iae)&lt;br /&gt;
		{&lt;br /&gt;
			// fail(ERRONEOUS_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.requireInitialization();&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Initialize&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineUsage);&lt;br /&gt;
		commandLineParser.parse();&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.requireInitialization();&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException iae)&lt;br /&gt;
		{&lt;br /&gt;
			fail(ERRONEOUS_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testForbidNullString()&lt;br /&gt;
	{&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.forbidNullString(&amp;quot;&amp;quot;, &amp;quot;No error message&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
			fail(ERRONEOUS_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.forbidNullString(null, &amp;quot;Error message&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
			assertTrue(iae.getMessage().contains(&amp;quot;Error message&amp;quot;));&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testForbidEmptyString()&lt;br /&gt;
	{&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.forbidEmptyString(&amp;quot;Non-empty string&amp;quot;,&lt;br /&gt;
					&amp;quot;No error message&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
			fail(ERRONEOUS_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.forbidEmptyString(null, &amp;quot;Error message&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
			assertTrue(iae.getMessage().contains(&amp;quot;Error message&amp;quot;));&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.forbidEmptyString(&amp;quot;&amp;quot;, &amp;quot;Error message&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
			assertTrue(iae.getMessage().contains(&amp;quot;Error message&amp;quot;));&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testForbid()&lt;br /&gt;
	{&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.forbid(false, &amp;quot;No error message&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
			fail(ERRONEOUS_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.forbid(true, &amp;quot;Error message&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testRemoveCardinality()&lt;br /&gt;
	{&lt;br /&gt;
		// String switchSpecification = &amp;quot;--next(1-10)&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
		// Null string&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.removeCardinality(null);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Forgotten left brace&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.removeCardinality(&amp;quot;--next(1-10&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Forgotten right brace&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.removeCardinality(&amp;quot;--next1-10)&amp;quot;);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		assertEquals(commandLineParser.removeCardinality(&amp;quot;--next(1-10)&amp;quot;),&lt;br /&gt;
				&amp;quot;--next&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		// If cardinality is not specified, return unmodified string&lt;br /&gt;
		assertEquals(commandLineParser.removeCardinality(&amp;quot;--next&amp;quot;), &amp;quot;--next&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/*******************************&lt;br /&gt;
	 * Initialization sequence tests&lt;br /&gt;
	 *******************************/&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testInitSequence()&lt;br /&gt;
	{&lt;br /&gt;
		// Disallowed operations on an uninitialized command line parser&lt;br /&gt;
&lt;br /&gt;
		// Require setPossibleSwitches() before setCommandLine()&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			newCommandLineParser.setCommandLine(cmdlineUsage);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testGetters()&lt;br /&gt;
	{&lt;br /&gt;
		CommandLineParser p = newCommandLineParser;&lt;br /&gt;
&lt;br /&gt;
		// Require setPossibleSwitches() before getPossibleSwitches()&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			p.getPossibleSwitches();&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException ise)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
		// Require setImplicitSwitch() before getImplicitSwitch()&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			p.getImplicitSwitch();&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException ise)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
		// Require setCommandLine() before getCommandLine()&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			p.getOriginalCommandLine();&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException ise)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
		// Require setArguments() before getArguments()&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			p.getArguments();&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException ise)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
		p.setPossibleSwitches(possibleSwitches);&lt;br /&gt;
		assertTrue(p.isPossibleSwitch(&amp;quot;--prev&amp;quot;));&lt;br /&gt;
		p.setImplicitSwitch(&amp;quot;--file(1)&amp;quot;);&lt;br /&gt;
		assertEquals(&amp;quot;--file&amp;quot;, p.getImplicitSwitch());&lt;br /&gt;
		p.setCommandLine(cmdlineOneValue);&lt;br /&gt;
		assertEquals(cmdlineOneValue, p.getOriginalCommandLine());&lt;br /&gt;
		assertEquals(p.getSwitchMap().size(), 2);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/*********************&lt;br /&gt;
	 * Less trivial tests&lt;br /&gt;
	 *********************/&lt;br /&gt;
&lt;br /&gt;
	/** Test command line with quotes in arguments */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testDoubleQuotesInCommandLine()&lt;br /&gt;
	{&lt;br /&gt;
		String s;&lt;br /&gt;
		s = &amp;quot;--onevalue \&amp;quot;val\&amp;quot; --next&amp;quot;;&lt;br /&gt;
		commandLineParser.setCommandLine(s);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(&amp;quot;--onevalue&amp;quot;), &amp;quot;val&amp;quot;);&lt;br /&gt;
		s = &amp;quot;--onevalue \&amp;quot;val1 val2\&amp;quot; --next&amp;quot;;&lt;br /&gt;
		commandLineParser.setCommandLine(s);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(&amp;quot;--onevalue&amp;quot;),&lt;br /&gt;
				&amp;quot;val1 val2&amp;quot;);&lt;br /&gt;
		s = &amp;quot;--onevalue \&amp;quot;val1 --next val2\&amp;quot; --next&amp;quot;;&lt;br /&gt;
		commandLineParser.setCommandLine(s);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(&amp;quot;--onevalue&amp;quot;),&lt;br /&gt;
				&amp;quot;val1 --next val2&amp;quot;);&lt;br /&gt;
		s = &amp;quot;--onevalue \&amp;quot;val3   val4\&amp;quot;&amp;quot;;&lt;br /&gt;
		commandLineParser.setCommandLine(s);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(&amp;quot;--onevalue&amp;quot;),&lt;br /&gt;
				&amp;quot;val3   val4&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** Test command line with several switch values per switch */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testSwitchCardinality()&lt;br /&gt;
	{&lt;br /&gt;
		List&amp;lt;String&amp;gt; switchValues = null;&lt;br /&gt;
&lt;br /&gt;
		// Require an exception in case of too few switch values&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.setCommandLine(cmdlineOneToThreeTooFew);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser.setCommandLine(cmdlineManyTooFew);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
		// Require an exception in case of too many switch values&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			commandLineParser&lt;br /&gt;
					.setCommandLine(cmdlineImplicitSwitchTooManyValues);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException iae)&lt;br /&gt;
		{&lt;br /&gt;
			String message = iae.getMessage();&lt;br /&gt;
			assertTrue(message != null&lt;br /&gt;
					&amp;amp;&amp;amp; message.contains(&amp;quot;already has a value&amp;quot;));&lt;br /&gt;
			assertTrue(message != null &amp;amp;&amp;amp; message.contains(&amp;quot;implicit&amp;quot;));&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// In case of too many switch values, assure that the 'overflow' values&lt;br /&gt;
		// get assigned to implicit switch and to switchless arguments:&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineOneToThreeTooMany);&lt;br /&gt;
		switchValues = commandLineParser.getSwitchValues(&amp;quot;--onetothree&amp;quot;);&lt;br /&gt;
		assertTrue(switchValues.size() == 3);&lt;br /&gt;
		String implicitSwitchValue = commandLineParser&lt;br /&gt;
				.getSwitchValue(commandLineParser.getImplicitSwitch());&lt;br /&gt;
		assertEquals(implicitSwitchValue, &amp;quot;val4&amp;quot;);&lt;br /&gt;
		String[] switchlessArguments = commandLineParser&lt;br /&gt;
				.getSwitchlessArguments();&lt;br /&gt;
		assertEquals(switchlessArguments[0], &amp;quot;val4&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		commandLineParser.setCommandLine(cmdlineMany);&lt;br /&gt;
		switchValues = commandLineParser.getSwitchValues(&amp;quot;--many&amp;quot;);&lt;br /&gt;
		assertTrue(switchValues.size() == 6);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** Test command line with a switch-less value */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testSwitchlessValue()&lt;br /&gt;
	{&lt;br /&gt;
		String s;&lt;br /&gt;
		s = &amp;quot;--onevalue val1 val2 --next&amp;quot;;&lt;br /&gt;
		commandLineParser.setCommandLine(s);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(&amp;quot;--onevalue&amp;quot;), &amp;quot;val1&amp;quot;);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(commandLineParser&lt;br /&gt;
				.getImplicitSwitch()), &amp;quot;val2&amp;quot;);&lt;br /&gt;
		String[] switchlessArguments = commandLineParser&lt;br /&gt;
				.getSwitchlessArguments();&lt;br /&gt;
		assertEquals(switchlessArguments[0], &amp;quot;val2&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** Test succession of command lines */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testCommandLineSuccession()&lt;br /&gt;
	{&lt;br /&gt;
		String s;&lt;br /&gt;
		s = &amp;quot;--onevalue val1 val2 --next&amp;quot;;&lt;br /&gt;
		commandLineParser.setCommandLine(s);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(&amp;quot;--onevalue&amp;quot;), &amp;quot;val1&amp;quot;);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(commandLineParser&lt;br /&gt;
				.getImplicitSwitch()), &amp;quot;val2&amp;quot;);&lt;br /&gt;
		s = &amp;quot;--onevalue val3 val4 --next&amp;quot;;&lt;br /&gt;
		commandLineParser.setCommandLine(s);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(&amp;quot;--onevalue&amp;quot;), &amp;quot;val3&amp;quot;);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(commandLineParser&lt;br /&gt;
				.getImplicitSwitch()), &amp;quot;val4&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/** Test succession of possible parameters */&lt;br /&gt;
	@Test&lt;br /&gt;
	public final void testSuccessionOfPossibleParameters()&lt;br /&gt;
	{&lt;br /&gt;
		String s;&lt;br /&gt;
		commandLineParser.setPossibleSwitches(&amp;quot;--one(1) --next(0)&amp;quot;);&lt;br /&gt;
		s = &amp;quot;--one val1 val2 --next&amp;quot;;&lt;br /&gt;
		commandLineParser.setCommandLine(s);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(&amp;quot;--one&amp;quot;), &amp;quot;val1&amp;quot;);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(commandLineParser&lt;br /&gt;
				.getImplicitSwitch()), &amp;quot;val2&amp;quot;);&lt;br /&gt;
		commandLineParser.setPossibleSwitches(&amp;quot;--another(1) --prev(0)&amp;quot;);&lt;br /&gt;
		// Require old switch to be invalid&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			s = &amp;quot;--another val3 val4 --next&amp;quot;;&lt;br /&gt;
			commandLineParser.setCommandLine(s);&lt;br /&gt;
			fail(MISSING_EXCEPTION);&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalArgumentException iae)&lt;br /&gt;
		{&lt;br /&gt;
		}&lt;br /&gt;
		s = &amp;quot;--prev --another val3 val4&amp;quot;;&lt;br /&gt;
		commandLineParser.setCommandLine(s);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(&amp;quot;--another&amp;quot;), &amp;quot;val3&amp;quot;);&lt;br /&gt;
		assertEquals(commandLineParser.getSwitchValue(commandLineParser&lt;br /&gt;
				.getImplicitSwitch()), &amp;quot;val4&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;</description>
			<pubDate>Sat, 22 Oct 2011 22:31:57 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:CommandLineParserTest.java</comments>		</item>
		<item>
			<title>CommandLineParser.java</title>
			<link>https://www.taitl.com/andrey/CommandLineParser.java</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
package com.taitl.world.commandline;&lt;br /&gt;
&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.LinkedHashMap;&lt;br /&gt;
import java.util.LinkedHashSet;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
import java.util.Map;&lt;br /&gt;
import java.util.Set;&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright 2011 Taitl Design. All rights reserved.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * CommandLineParser class takes command line arguments and parses them into a&lt;br /&gt;
 * map of [switch, list of values] pairs. A switch on the command line can have&lt;br /&gt;
 * no, one, or many values. The command line can also have arguments that are&lt;br /&gt;
 * not related to any switches.&lt;br /&gt;
 * &amp;lt;p&amp;gt;&lt;br /&gt;
 * Usage:&lt;br /&gt;
 * &amp;lt;p&amp;gt;&lt;br /&gt;
 * &lt;br /&gt;
 * &amp;lt;pre&amp;gt;&lt;br /&gt;
 * public static void main(String[] arguments)&lt;br /&gt;
 * {&lt;br /&gt;
 * 	CommandLineParser commandLineParser = new CommandLineParser();&lt;br /&gt;
 * 	commandLineParser.setPossibleSwitches(&amp;amp;quot;--version(0) --usage(0)&amp;amp;quot;&lt;br /&gt;
 * 			+ &amp;amp;quot; --help(0) --multi(*)&amp;amp;quot;);&lt;br /&gt;
 * 	commandLineParser.setEmptyCommandLineSwitch(&amp;amp;quot;--usage&amp;amp;quot;);&lt;br /&gt;
 * 	commandLineParser.setImplicitSwitch(&amp;amp;quot;--file(1)&amp;amp;quot;);&lt;br /&gt;
 * 	commandLineParser.setArguments(arguments);&lt;br /&gt;
 * 	// ...same as commandLineParser.parse(arguments);&lt;br /&gt;
 * &lt;br /&gt;
 * 	if (commandLineParser.isUsageRequested())&lt;br /&gt;
 * 	{&lt;br /&gt;
 * 		System.out.println(usageText);&lt;br /&gt;
 * 	}&lt;br /&gt;
 * 	else if (commandLineParser.isVersionRequested())&lt;br /&gt;
 * 	{&lt;br /&gt;
 * 		System.out.println(VERSION_STRING);&lt;br /&gt;
 * 	}&lt;br /&gt;
 * 	else if (commandLineParser.isHelpRequested())&lt;br /&gt;
 * 	{&lt;br /&gt;
 * 		System.out.println(helpText);&lt;br /&gt;
 * 	}&lt;br /&gt;
 * 	else if (commandLineParser.isSwitchPresent(&amp;amp;quot;--multi&amp;amp;quot;))&lt;br /&gt;
 * 	{&lt;br /&gt;
 * 		// Process custom switch&lt;br /&gt;
 * 		List&amp;amp;lt;String&amp;amp;gt; switchValues = commandLineParser&lt;br /&gt;
 * 				.getSwitchValues(&amp;amp;quot;--multi&amp;amp;quot;);&lt;br /&gt;
 * 		String[] arguments = commandLineParser.getSwitchlessArguments();&lt;br /&gt;
 * 		// ...&lt;br /&gt;
 * 	}&lt;br /&gt;
 * }&lt;br /&gt;
 * &amp;lt;/pre&amp;gt;&lt;br /&gt;
 * &lt;br /&gt;
 * @author Andrey Potekhin, 03/08/2011&lt;br /&gt;
 */&lt;br /&gt;
public class CommandLineParser&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
	/** Default usage switch name: --usage. */&lt;br /&gt;
	static final String DEFAULT_USAGE_SWITCH = &amp;quot;--usage&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	/** Default help switch name: --help. */&lt;br /&gt;
	static final String DEFAULT_HELP_SWITCH = &amp;quot;--help&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	/** Default version switch name: --version. */&lt;br /&gt;
	static final String DEFAULT_VERSION_SWITCH = &amp;quot;--version&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	/** Default switch name prefixes: -- and -. */&lt;br /&gt;
	static final String DEFAULT_SWITCH_PREFIXES = &amp;quot;(--|-)&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	/** Default value of usage switch, defaulted to --usage. */&lt;br /&gt;
	private String usageSwitchName = DEFAULT_USAGE_SWITCH;&lt;br /&gt;
&lt;br /&gt;
	/** Default value of usage switch, defaulted to --help. */&lt;br /&gt;
	private String helpSwitchName = DEFAULT_HELP_SWITCH;&lt;br /&gt;
&lt;br /&gt;
	/** Default value of usage switch, defaulted to --version. */&lt;br /&gt;
	private String versionSwitchName = DEFAULT_VERSION_SWITCH;&lt;br /&gt;
&lt;br /&gt;
	/** Default value of switch prefixes, defaulted to (--|-). */&lt;br /&gt;
	private String switchPrefixesMask = DEFAULT_SWITCH_PREFIXES;&lt;br /&gt;
&lt;br /&gt;
	/** The original command line. */&lt;br /&gt;
	private String originalCommandLine = null;&lt;br /&gt;
&lt;br /&gt;
	/** The original arguments array, as usually passed in the main() method. */&lt;br /&gt;
	private String[] arguments = null;&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * The command line arguments that do not have a switch corresponding to&lt;br /&gt;
	 * them.&lt;br /&gt;
	 */&lt;br /&gt;
	private String[] switchlessArguments = null;&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Has this object been properly initialized? This is false unless both&lt;br /&gt;
	 * setPossibleSwitches() and setArguments()/parse() has been called.&lt;br /&gt;
	 */&lt;br /&gt;
	private boolean isInitialized = false;&lt;br /&gt;
&lt;br /&gt;
	/** The set of possible switches. */&lt;br /&gt;
	private Set&amp;lt;String&amp;gt; possibleSwitches = new LinkedHashSet&amp;lt;String&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * The default (implicit) switch to which all switchless arguments are&lt;br /&gt;
	 * attributed.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @see CommandLineParser#setImplicitSwitch&lt;br /&gt;
	 */&lt;br /&gt;
	private String implicitSwitch = null;&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Main structure to hold information about the parsed command line switches&lt;br /&gt;
	 * and their values and switchless arguments.&lt;br /&gt;
	 */&lt;br /&gt;
	private Map&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt; switchMap = new LinkedHashMap&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
	/** Command line switches as result of parsing. */&lt;br /&gt;
	private Map&amp;lt;String, Switch&amp;gt; switches = new LinkedHashMap&amp;lt;String, Switch&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Default constructor.&lt;br /&gt;
	 */&lt;br /&gt;
	public CommandLineParser()&lt;br /&gt;
	{&lt;br /&gt;
		originalCommandLine = null;&lt;br /&gt;
		isInitialized = false;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Sets command line arguments form the array of arguments passed to Java&lt;br /&gt;
	 * program's main() method, and parses them into map of [switch-list of&lt;br /&gt;
	 * values] pairs by calling&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * According to Java documentation, the arguments that are passed to mail()&lt;br /&gt;
	 * method conform to the following rules:&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * - On the command line, Java program arguments are separated by spaces and&lt;br /&gt;
	 * tabs, except when an argument is enclosed by double-quotes.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * - Leading and trailing whitespace characters are removed from the values&lt;br /&gt;
	 * stored in the arguments array.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * - The double-quoted argument is not broken into several separate&lt;br /&gt;
	 * arguments.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * - For arguments enclosed with double-quotes, the double-quotes are&lt;br /&gt;
	 * removed.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param args&lt;br /&gt;
	 *            Command line arguments to parse.&lt;br /&gt;
	 */&lt;br /&gt;
	public final void setArguments(final String[] args)&lt;br /&gt;
	{&lt;br /&gt;
		forbid(args == null, &amp;quot;Args argument must not be null.&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		StringBuffer commandLine = new StringBuffer();&lt;br /&gt;
		String arg;&lt;br /&gt;
&lt;br /&gt;
		for (String arg2 : args)&lt;br /&gt;
		{&lt;br /&gt;
			forbid(arg2 == null, &amp;quot;Null value in argument array.&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
			arg = arg2.trim();&lt;br /&gt;
&lt;br /&gt;
			// If argument has spaces in it, enclose it in double quotes&lt;br /&gt;
			if (arg.matches(&amp;quot;.*\\s.*&amp;quot;))&lt;br /&gt;
			{&lt;br /&gt;
				arg = &amp;quot;\&amp;quot;&amp;quot; + arg + &amp;quot;\&amp;quot;&amp;quot;;&lt;br /&gt;
			}&lt;br /&gt;
			if (commandLine.length() &amp;gt; 0 &amp;amp;&amp;amp; arg.length() &amp;gt; 0)&lt;br /&gt;
			{&lt;br /&gt;
				commandLine.append(&amp;quot; &amp;quot;);&lt;br /&gt;
			}&lt;br /&gt;
			commandLine.append(arg);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		originalCommandLine = commandLine.toString();&lt;br /&gt;
		arguments = args;&lt;br /&gt;
		switches.clear();&lt;br /&gt;
		switchlessArguments = null;&lt;br /&gt;
&lt;br /&gt;
		// Uninitialize the object&lt;br /&gt;
		isInitialized = false;&lt;br /&gt;
&lt;br /&gt;
		// Immediately parse&lt;br /&gt;
		parse();&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * An alternative to &amp;lt;code&amp;gt;setArguments()&amp;lt;/code&amp;gt; method, sets arguments&lt;br /&gt;
	 * using a command line string.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * According to Java documentation, the arguments that are passed to mail()&lt;br /&gt;
	 * method conform to the following rules:&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * - On the command line, Java program arguments are separated by spaces and&lt;br /&gt;
	 * tabs, except when an argument is enclosed by double-quotes.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * - Leading and trailing whitespace characters are removed from the values&lt;br /&gt;
	 * stored in the arguments array.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * - The double-quoted argument is not broken into several separate&lt;br /&gt;
	 * arguments.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * - For arguments enclosed with double-quotes, the double-quotes are&lt;br /&gt;
	 * removed.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param commandLine&lt;br /&gt;
	 *            Command line to parse.&lt;br /&gt;
	 */&lt;br /&gt;
	public void setCommandLine(String commandLine)&lt;br /&gt;
	{&lt;br /&gt;
		forbidNullString(commandLine, &amp;quot;commandLine&amp;quot;);&lt;br /&gt;
		originalCommandLine = commandLine;&lt;br /&gt;
&lt;br /&gt;
		// String[] args = commandLine.split(&amp;quot;\\s&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		// Go over command line, splitting it into whitespace-separated&lt;br /&gt;
		// arguments, preserving arguments enclosed with double quotes,&lt;br /&gt;
		// treating them as a single argument, while also removing their&lt;br /&gt;
		// enclosing double quotes.&lt;br /&gt;
		boolean insideDoubleQuotes = false;&lt;br /&gt;
		boolean inWord = false;&lt;br /&gt;
		boolean isWhitespace = false;&lt;br /&gt;
		StringBuffer arg = new StringBuffer();&lt;br /&gt;
		List&amp;lt;String&amp;gt; args = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
		for (int i = 0; i &amp;lt; commandLine.length(); i++)&lt;br /&gt;
		{&lt;br /&gt;
			char c = commandLine.charAt(i);&lt;br /&gt;
			boolean finalizeArgument = false;&lt;br /&gt;
			boolean isClosingDoubleQuote = false;&lt;br /&gt;
&lt;br /&gt;
			if (c == '&amp;quot;')&lt;br /&gt;
			{&lt;br /&gt;
				if (!insideDoubleQuotes)&lt;br /&gt;
				{&lt;br /&gt;
					insideDoubleQuotes = true;&lt;br /&gt;
					continue;&lt;br /&gt;
				}&lt;br /&gt;
				else&lt;br /&gt;
				{&lt;br /&gt;
					insideDoubleQuotes = false;&lt;br /&gt;
					isClosingDoubleQuote = true;&lt;br /&gt;
					finalizeArgument = true;&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			if (insideDoubleQuotes)&lt;br /&gt;
			{&lt;br /&gt;
				arg.append(c);&lt;br /&gt;
				continue;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			isWhitespace = Character.isWhitespace(c);&lt;br /&gt;
&lt;br /&gt;
			if (isWhitespace)&lt;br /&gt;
			{&lt;br /&gt;
				if (inWord)&lt;br /&gt;
				{&lt;br /&gt;
					inWord = false;&lt;br /&gt;
					finalizeArgument = true;&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
			else&lt;br /&gt;
			{&lt;br /&gt;
				if (!inWord &amp;amp;&amp;amp; !isClosingDoubleQuote)&lt;br /&gt;
				{&lt;br /&gt;
					inWord = true;&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			if (inWord)&lt;br /&gt;
			{&lt;br /&gt;
				arg.append(c);&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			if (finalizeArgument)&lt;br /&gt;
			{&lt;br /&gt;
				// Finalize argument&lt;br /&gt;
				args.add(arg.toString());&lt;br /&gt;
				arg.setLength(0);&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Finalize last argument&lt;br /&gt;
		if (arg.length() &amp;gt; 0)&lt;br /&gt;
		{&lt;br /&gt;
			args.add(arg.toString());&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Convert to String[]&lt;br /&gt;
		String[] argArray = new String[args.size()];&lt;br /&gt;
		for (int i = 0; i &amp;lt; args.size(); i++)&lt;br /&gt;
		{&lt;br /&gt;
			argArray[i] = args.get(i);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Immediately parse&lt;br /&gt;
		setArguments(argArray);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Gets command line arguments previously set by call to&lt;br /&gt;
	 * &amp;lt;code&amp;gt; setArguments()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;setCommandLine()&amp;lt;/code&amp;gt;.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return String array of arguments previously set by a call to&lt;br /&gt;
	 *         setArguments() or setCommandLine(). Throws IllegalStateException&lt;br /&gt;
	 *         if user tries to get arguments that have not yet been set.&lt;br /&gt;
	 */&lt;br /&gt;
	public final String[] getArguments()&lt;br /&gt;
	{&lt;br /&gt;
		forbidState(arguments == null,&lt;br /&gt;
				&amp;quot;You must call setArguments() or setCommandLine() before calling this method.&amp;quot;);&lt;br /&gt;
		return arguments;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns the set of possible command line switches previously specified&lt;br /&gt;
	 * with a call to setPossibleSwitches().&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return The set of possible switches previously specified with a call to&lt;br /&gt;
	 *         setPossibleSwitches().&lt;br /&gt;
	 */&lt;br /&gt;
	public Set&amp;lt;String&amp;gt; getPossibleSwitches()&lt;br /&gt;
	{&lt;br /&gt;
		forbidState(possibleSwitches.isEmpty(),&lt;br /&gt;
				&amp;quot;You must call setPossibleSwitches() before calling this method.&amp;quot;);&lt;br /&gt;
		return possibleSwitches;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * This method lets the CommandLineParser object know of the legitimate&lt;br /&gt;
	 * switches. You are required to call this method after you constructed the&lt;br /&gt;
	 * CommandLineParser object, and before proceeding with any further work&lt;br /&gt;
	 * with it.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * Each switch is succeed by parenthesis indicating the potential number of&lt;br /&gt;
	 * it arguments, for example:&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * (0) - no arguments,&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * (1) - one argument,&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * (0-3) - from zero to three arguments,&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * (*) - any number of arguments,&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * (3-*) - from three to any greater number of arguments, and so on.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * Example:&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;code&amp;gt;setPossibleSwitches(&amp;quot;--version(0) --usage(0)&amp;quot; &lt;br /&gt;
	 * 		+ &amp;quot; --help(0-1) --multi(1-*)&amp;quot;);&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param possibleSwitchesList&lt;br /&gt;
	 *            Space-separated list of allowed command line switches, each of&lt;br /&gt;
	 *            which is followed by parenthesized number of switch values.&lt;br /&gt;
	 *            This can be either a precise number, a min-max expression, or&lt;br /&gt;
	 *            expression involving an asterisk (*), which stands for any&lt;br /&gt;
	 *            number of arguments.&lt;br /&gt;
	 */&lt;br /&gt;
	public final void setPossibleSwitches(String possibleSwitchesList)&lt;br /&gt;
	{&lt;br /&gt;
		forbidEmptyString(possibleSwitchesList, &amp;quot;possibleSwitchesList&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		// Forget old switches that were specified earlier&lt;br /&gt;
		possibleSwitches.clear();&lt;br /&gt;
&lt;br /&gt;
		// Parse comma-separated list into hash map&lt;br /&gt;
		for (String switchSpecification : possibleSwitchesList.split(&amp;quot; &amp;quot;))&lt;br /&gt;
		{&lt;br /&gt;
			addPossibleSwitch(switchSpecification);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// The implicit switch, if specified, must be a part of possible&lt;br /&gt;
		// switch collection. Since we have emptied the set of&lt;br /&gt;
		// possible switches in the beginning of this method, we&lt;br /&gt;
		// need to add the implicit switch again to it.&lt;br /&gt;
		if (implicitSwitch != null)&lt;br /&gt;
		{&lt;br /&gt;
			setImplicitSwitch(implicitSwitch);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Sets the implicit switch, that is, the switch to which the argument value&lt;br /&gt;
	 * attributed of the first argument that is not preceded by any switch.&lt;br /&gt;
	 * Example:&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * If you call setImplicitSwitch(&amp;quot;--file(1)&amp;quot;) and process command line&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;code&amp;gt;myprogram --vertical --split myfile.txt&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * then CommandLineParser will attribute &amp;quot;myfile.txt&amp;quot; value to the &amp;quot;--file&amp;quot;&lt;br /&gt;
	 * switch, and you will be able to retrieve this value by calling&lt;br /&gt;
	 * &amp;lt;code&amp;gt;getSwitchValue(&amp;quot;--file&amp;quot;)&amp;lt;/code&amp;gt;.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * Note: you don't have to specify the implicit switch in the call to&lt;br /&gt;
	 * &amp;lt;code&amp;gt;setPossibleSwitches()&amp;lt;/code&amp;gt;.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param implicitSwitchSpecification&lt;br /&gt;
	 *            Default (implicit) switch will correspond to command line&lt;br /&gt;
	 *            parameter that is not preceded with any switch. If there are&lt;br /&gt;
	 *            several such parameters, they are united in a list and this&lt;br /&gt;
	 *            list is set a list of values that correspond to this implicit&lt;br /&gt;
	 *            switch.&lt;br /&gt;
	 */&lt;br /&gt;
	public void setImplicitSwitch(String implicitSwitchSpecification)&lt;br /&gt;
	{&lt;br /&gt;
		assert possibleSwitches != null;&lt;br /&gt;
		forbid(possibleSwitches.isEmpty(),&lt;br /&gt;
				&amp;quot;You must call setPossibleSwitches() before calling this method.&amp;quot;);&lt;br /&gt;
		forbidEmptyString(implicitSwitchSpecification,&lt;br /&gt;
				&amp;quot;implicitSwitchSpecification&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		/*&lt;br /&gt;
		 * if (implicitSwitch != null &amp;amp;&amp;amp;&lt;br /&gt;
		 * isPossibleSwitch(removeCardinality(implicitSwitch))) {&lt;br /&gt;
		 * removePossibleSwitch(implicitSwitch); }&lt;br /&gt;
		 */&lt;br /&gt;
&lt;br /&gt;
		implicitSwitch = implicitSwitchSpecification;&lt;br /&gt;
		// addPossibleSwitch(implicitSwitch);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns the implicit command line switch, that is, the implicit switch&lt;br /&gt;
	 * (set earlier with a call to setImplicitSwitch()), which corresponds to&lt;br /&gt;
	 * command line parameter(s) that are not part of values of any other&lt;br /&gt;
	 * switch. Example:&lt;br /&gt;
	 * &lt;br /&gt;
	 * &amp;lt;pre&amp;gt;&lt;br /&gt;
	 * command line: myprogram --verbose file.txt&lt;br /&gt;
	 * ...&lt;br /&gt;
	 * setPossibleSwitches(&amp;quot;--verbose(0)&amp;quot;);&lt;br /&gt;
	 * setImplicitSwitch(&amp;amp;quot;--file&amp;amp;quot;);&lt;br /&gt;
	 * ...&lt;br /&gt;
	 * String filename = getSwitchValue(getImplicitSwitch());&lt;br /&gt;
	 * &amp;lt;/pre&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * A call to this method must be preceded by a call to setImplicitSwitch(),&lt;br /&gt;
	 * otherwise an IllegalStateException is thrown.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * To get the implicit switch value(s), call &amp;lt;code&amp;gt;getSwitchValue()&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * or &amp;lt;code&amp;gt;getSwitchValues()&amp;lt;/code&amp;gt; with the name of implicit switch.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return The name of the implicit switch, e.g. &amp;quot;--file&amp;quot;.&lt;br /&gt;
	 * @throws IllegalArgumentException&lt;br /&gt;
	 *             when this method is not preceded by a call to&lt;br /&gt;
	 *             setImplicitSwitch()&lt;br /&gt;
	 */&lt;br /&gt;
	public String getImplicitSwitch() throws IllegalArgumentException&lt;br /&gt;
	{&lt;br /&gt;
		forbidState(&lt;br /&gt;
				implicitSwitch == null,&lt;br /&gt;
				&amp;quot;Null in implicit switch value. You need to call setImplicitSwitch() before calling this method.&amp;quot;);&lt;br /&gt;
		return removeCardinality(implicitSwitch);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Parses command line arguments specified in a call to setArguments() or&lt;br /&gt;
	 * setCommandLine() and prepares as a result the data that reflect which&lt;br /&gt;
	 * command line switches were specified and with which values. This data can&lt;br /&gt;
	 * be retrieved with calls to getSwitchValue(), getSwitchValues(),&lt;br /&gt;
	 * getSwitchValueCount(), isSwitchPresent().&lt;br /&gt;
	 * &lt;br /&gt;
	 * @throws IllegalArgumentException&lt;br /&gt;
	 *             - when a violation of parsing rules is encountered, for&lt;br /&gt;
	 *             example, when a switch is present on command line which was&lt;br /&gt;
	 *             not specified in a call to setPossibleSwitches().&lt;br /&gt;
	 * @throws IllegalStateException&lt;br /&gt;
	 *             when the CommandLineParser object is not initialized, for&lt;br /&gt;
	 *             instance, when its the command line arguments have not been&lt;br /&gt;
	 *             set yet. You must at a minimum call setPossibleSwitches(),&lt;br /&gt;
	 *             setArguments() or setCommandLine() before calling this&lt;br /&gt;
	 *             method.&lt;br /&gt;
	 */&lt;br /&gt;
	protected void parse() throws IllegalArgumentException,&lt;br /&gt;
			IllegalStateException&lt;br /&gt;
	{&lt;br /&gt;
		forbidState(&lt;br /&gt;
				originalCommandLine == null || arguments == null,&lt;br /&gt;
				&amp;quot;You must call setArguments() or setCommandLine() method before calling parse() method without arguments.&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		// Here happens the magic!&lt;br /&gt;
		doParsing(arguments);&lt;br /&gt;
&lt;br /&gt;
		isInitialized = true;&lt;br /&gt;
&lt;br /&gt;
		// Make sure the object is now properly initialized&lt;br /&gt;
		requireInitialization();&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Is command line switch present?&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param switchString&lt;br /&gt;
	 *            The command line switch, e.g. --version.&lt;br /&gt;
	 * @return True or false depending on whether the switch is present on the&lt;br /&gt;
	 *         command line.&lt;br /&gt;
	 */&lt;br /&gt;
	public boolean isSwitchPresent(String switchString)&lt;br /&gt;
	{&lt;br /&gt;
		requireInitialization();&lt;br /&gt;
		forbidEmptyString(switchString, &amp;quot;switchString&amp;quot;);&lt;br /&gt;
		forbid(!looksLikeSwitch(switchString),&lt;br /&gt;
				&amp;quot;Switch must start with one of switch prefixes defined by the switchPrefixesMask mask.&amp;quot;);&lt;br /&gt;
		boolean returnValue = switches.containsKey(switchString);&lt;br /&gt;
		return returnValue;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns switch value.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * Example:&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * For command line &amp;lt;code&amp;gt;myprogram --version&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;code&amp;gt;getSwitchValue(&amp;quot;--version&amp;quot;)&amp;lt;/code&amp;gt; will return an empty string.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * For command line &amp;lt;code&amp;gt;myprogram --version ver&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;code&amp;gt;getSwitchValue(&amp;quot;--version&amp;quot;)&amp;lt;/code&amp;gt; will return string &amp;quot;ver&amp;quot;.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * If the switch is not present, the &amp;lt;code&amp;gt;IllegalArgumentException&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * is thrown.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param switchString&lt;br /&gt;
	 *            The name of command line switch.&lt;br /&gt;
	 * @return The value of command line switch, empty string if switch has no&lt;br /&gt;
	 *         value (as, for example, in case with --version switch).&lt;br /&gt;
	 */&lt;br /&gt;
	public String getSwitchValue(String switchString)&lt;br /&gt;
	{&lt;br /&gt;
		String returnValue = null;&lt;br /&gt;
&lt;br /&gt;
		requireInitialization();&lt;br /&gt;
		forbidEmptyString(switchString, &amp;quot;switchString&amp;quot;);&lt;br /&gt;
		forbid(!isSwitchPresent(switchString),&lt;br /&gt;
				&amp;quot;Switch &amp;quot;&lt;br /&gt;
						+ switchString&lt;br /&gt;
						+ &amp;quot; is not present on the command line. Use isSwitchPresent() to check for a switch.&amp;quot;);&lt;br /&gt;
		List&amp;lt;String&amp;gt; valueList = getSwitchValues(switchString);&lt;br /&gt;
		assert valueList != null;&lt;br /&gt;
		if (valueList.size() == 0)&lt;br /&gt;
		{&lt;br /&gt;
			returnValue = &amp;quot;&amp;quot;;&lt;br /&gt;
		}&lt;br /&gt;
		else&lt;br /&gt;
		{&lt;br /&gt;
			returnValue = valueList.get(0);&lt;br /&gt;
		}&lt;br /&gt;
		forbid(valueList.size() &amp;gt; 1,&lt;br /&gt;
				&amp;quot;Switch &amp;quot;&lt;br /&gt;
						+ switchString&lt;br /&gt;
						+ &amp;quot; has several values. Use getSwitchValueCount() to check for number of switch values.&amp;quot;&lt;br /&gt;
						+ &amp;quot; Use getSwitchValues() to get the list of switch values.&amp;quot;);&lt;br /&gt;
		assert returnValue != null; // Make sure we didn't skip any branches in&lt;br /&gt;
									// logic&lt;br /&gt;
		return returnValue;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns the list of values of command switch. In the command line, the&lt;br /&gt;
	 * values are assigned to a switch until either the next switch is&lt;br /&gt;
	 * encountered. Next switch is not considered such if it appears inside a&lt;br /&gt;
	 * single or double quoted string.&lt;br /&gt;
	 * &lt;br /&gt;
	 * The arguments that&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * Example:&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * For command line &amp;lt;code&amp;gt;myprogram --version&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;code&amp;gt;getSwitchValue(&amp;quot;--version&amp;quot;)&amp;lt;/code&amp;gt; will return an empty list.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * For command line myprogram --version ver&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;code&amp;gt;getSwitchValue(&amp;quot;--version&amp;quot;)&amp;lt;/code&amp;gt; will return a list with one&lt;br /&gt;
	 * element, string &amp;quot;ver&amp;quot;.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * For command line &amp;lt;code&amp;gt;myprogram --version ver ber&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;code&amp;gt;getSwitchValue(&amp;quot;--version&amp;quot;)&amp;lt;/code&amp;gt; will return a list with two&lt;br /&gt;
	 * elements: strings &amp;quot;ver&amp;quot; and &amp;quot;ber&amp;quot;.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * If the switch is not present, the &amp;lt;code&amp;gt;IllegalArgumentException&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * is thrown.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param switchString&lt;br /&gt;
	 *            The name of command line switch.&lt;br /&gt;
	 * @return The list of values of command line switch.&lt;br /&gt;
	 */&lt;br /&gt;
	public List&amp;lt;String&amp;gt; getSwitchValues(String switchString)&lt;br /&gt;
	{&lt;br /&gt;
		List&amp;lt;String&amp;gt; returnValue = null;&lt;br /&gt;
&lt;br /&gt;
		requireInitialization();&lt;br /&gt;
		forbidEmptyString(switchString, &amp;quot;switchString&amp;quot;);&lt;br /&gt;
		forbid(!isSwitchPresent(switchString),&lt;br /&gt;
				&amp;quot;Switch &amp;quot;&lt;br /&gt;
						+ switchString&lt;br /&gt;
						+ &amp;quot; is not present on the command line. Use isSwitchPresent() to check for a switch.&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		List&amp;lt;String&amp;gt; valueList = switchMap.get(switchString);&lt;br /&gt;
		assert valueList != null;&lt;br /&gt;
		returnValue = valueList;&lt;br /&gt;
		assert returnValue != null; // Make sure we didn't skip any branches in&lt;br /&gt;
									// logic&lt;br /&gt;
		return returnValue;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns value count of switch.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * Example:&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * For command line &amp;lt;code&amp;gt;myprogram --version&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;code&amp;gt;getSwitchValueCount(&amp;quot;--version&amp;quot;)&amp;lt;/code&amp;gt; will return 0.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * For command line &amp;lt;code&amp;gt;myprogram --version ver&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;code&amp;gt;getSwitchValueCount(&amp;quot;--version&amp;quot;)&amp;lt;/code&amp;gt; will return 1.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * If the switch is not present, the &amp;lt;code&amp;gt;IllegalArgumentException&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * is thrown.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param switchString&lt;br /&gt;
	 *            The name of command line switch.&lt;br /&gt;
	 * @return The number of values of command line switch.&lt;br /&gt;
	 */&lt;br /&gt;
	public int getSwitchValueCount(String switchString)&lt;br /&gt;
	{&lt;br /&gt;
		Integer returnValue = null;&lt;br /&gt;
&lt;br /&gt;
		requireInitialization();&lt;br /&gt;
		forbidEmptyString(switchString, &amp;quot;switchString&amp;quot;);&lt;br /&gt;
		forbid(!isSwitchPresent(switchString),&lt;br /&gt;
				&amp;quot;Switch &amp;quot;&lt;br /&gt;
						+ switchString&lt;br /&gt;
						+ &amp;quot; is not present on the command line. Use isSwitchPresent() to check for a switch.&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		List&amp;lt;String&amp;gt; valueList = switchMap.get(switchString);&lt;br /&gt;
		assert valueList != null;&lt;br /&gt;
		returnValue = new Integer(valueList.size());&lt;br /&gt;
		assert returnValue != null; // Make sure we didn't skip any branches in&lt;br /&gt;
									// logic&lt;br /&gt;
		return returnValue.intValue();&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns the original command line that was parsed.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return The command line that has been passed to setCommandLine() method,&lt;br /&gt;
	 *         or to setArguments() as array of arguments.&lt;br /&gt;
	 */&lt;br /&gt;
	public String getOriginalCommandLine()&lt;br /&gt;
	{&lt;br /&gt;
		forbidState(originalCommandLine == null,&lt;br /&gt;
				&amp;quot;You must call setCommandLine() or setArguments() before calling this method.&amp;quot;);&lt;br /&gt;
		return originalCommandLine;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns true if usageSwitchName (default --usage) is present on the&lt;br /&gt;
	 * command line.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return True if usageSwitchName is present on command line.&lt;br /&gt;
	 */&lt;br /&gt;
	public boolean isUsageRequested()&lt;br /&gt;
	{&lt;br /&gt;
		requireInitialization();&lt;br /&gt;
		return isSwitchPresent(usageSwitchName);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns true if helpSwitchName (default --help) is present on the command&lt;br /&gt;
	 * line.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return True if helpSwitchName is present on command line.&lt;br /&gt;
	 */&lt;br /&gt;
	public boolean isHelpRequested()&lt;br /&gt;
	{&lt;br /&gt;
		requireInitialization();&lt;br /&gt;
		return isSwitchPresent(helpSwitchName);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns true if versionSwitchName (default --version) is present on the&lt;br /&gt;
	 * command line.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return True if versionSwitchName is present on command line.&lt;br /&gt;
	 */&lt;br /&gt;
	public boolean isVersionRequested()&lt;br /&gt;
	{&lt;br /&gt;
		requireInitialization();&lt;br /&gt;
		return isSwitchPresent(versionSwitchName);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns the default value of usageSwitchName (default --usage).&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return The default value of usageSwitchName.&lt;br /&gt;
	 */&lt;br /&gt;
	public String getUsageSwitchName()&lt;br /&gt;
	{&lt;br /&gt;
		return usageSwitchName;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Sets the default value of usageSwitchName.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param usageSwitch&lt;br /&gt;
	 *            New default value of usageSwitchName.&lt;br /&gt;
	 */&lt;br /&gt;
	public void setUsageSwitchName(String usageSwitch)&lt;br /&gt;
	{&lt;br /&gt;
		forbid(looksLikeSwitch(usageSwitch),&lt;br /&gt;
				&amp;quot;Switch must start with an appropriate switch prefix.&amp;quot;);&lt;br /&gt;
		this.usageSwitchName = usageSwitch;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns the default value of helpSwitchName (default --help).&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return The default value of helpSwitchName.&lt;br /&gt;
	 */&lt;br /&gt;
	public String getHelpSwitchName()&lt;br /&gt;
	{&lt;br /&gt;
		return helpSwitchName;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Sets the default value of helpSwitchName.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param helpSwitch&lt;br /&gt;
	 *            New default value of helpSwitchName.&lt;br /&gt;
	 */&lt;br /&gt;
	public void setHelpSwitchName(String helpSwitch)&lt;br /&gt;
	{&lt;br /&gt;
		this.helpSwitchName = helpSwitch;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns the default value of versionSwitchName (default --version).&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return The default value of versionSwitchName.&lt;br /&gt;
	 */&lt;br /&gt;
	public String getVersionSwitchName()&lt;br /&gt;
	{&lt;br /&gt;
		return versionSwitchName;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Sets the default value of versionSwitchName.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param versionSwitch&lt;br /&gt;
	 *            New default value of versionSwitchName.&lt;br /&gt;
	 */&lt;br /&gt;
	public void setVersionSwitchName(String versionSwitch)&lt;br /&gt;
	{&lt;br /&gt;
		this.versionSwitchName = versionSwitch;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Checks if CommandLineParser has not been properly initialized, that is,&lt;br /&gt;
	 * the methods &amp;lt;code&amp;gt;setPossibleSwitches()&amp;lt;/code&amp;gt; and&lt;br /&gt;
	 * &amp;lt;code&amp;gt;setArguments()&amp;lt;/code&amp;gt; have been called, and therefore, the parsing&lt;br /&gt;
	 * process has been carried out.&lt;br /&gt;
	 * &lt;br /&gt;
	 * Throws &amp;lt;code&amp;gt;IllegalStateException&amp;lt;/code&amp;gt; exception if the&lt;br /&gt;
	 * &amp;lt;code&amp;gt;CommandLineParser&amp;lt;/code&amp;gt; has not been initialized, that is, the&lt;br /&gt;
	 * methods &amp;lt;code&amp;gt;setPossibleSwitches()&amp;lt;/code&amp;gt; and&lt;br /&gt;
	 * &amp;lt;code&amp;gt;setArguments()&amp;lt;/code&amp;gt; have not been called.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @throws IllegalStateException&lt;br /&gt;
	 *             if CommandLineParser has not been initialized.&lt;br /&gt;
	 */&lt;br /&gt;
	protected void requireInitialization() throws IllegalStateException&lt;br /&gt;
	{&lt;br /&gt;
		assert possibleSwitches != null;&lt;br /&gt;
&lt;br /&gt;
		if (possibleSwitches.isEmpty())&lt;br /&gt;
		{&lt;br /&gt;
			throw new IllegalStateException(&lt;br /&gt;
					&amp;quot;This class must be initialized first by calling setPossibleSwitches() and setArguments() method.&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
		if (arguments == null)&lt;br /&gt;
		{&lt;br /&gt;
			throw new IllegalStateException(&lt;br /&gt;
					&amp;quot;This class must be initialized first by calling setArguments() or setCommandLine() method.&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
		if (!isInitialized)&lt;br /&gt;
		{&lt;br /&gt;
			throw new IllegalStateException(&lt;br /&gt;
					&amp;quot;This class must be initialized first by calling setArguments() or parse() method.&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Throws IllegalArgumentException if the specified string is null.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param s&lt;br /&gt;
	 *            The string to check for nullability.&lt;br /&gt;
	 * @param parameterName&lt;br /&gt;
	 *            The name of parameter of the caller method that we want to&lt;br /&gt;
	 *            include in exception description if the tested string is null.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @throws IllegalArgumentException&lt;br /&gt;
	 *             if passed-in string is null.&lt;br /&gt;
	 */&lt;br /&gt;
	protected void forbidNullString(String s, String parameterName)&lt;br /&gt;
			throws IllegalArgumentException&lt;br /&gt;
	{&lt;br /&gt;
		if (s == null)&lt;br /&gt;
		{&lt;br /&gt;
			throw new IllegalArgumentException(&lt;br /&gt;
					&amp;quot;Non-null value required in parameter &amp;quot; + parameterName&lt;br /&gt;
							+ &amp;quot;.&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Throws IllegalArgumentException if the specified string is a null or&lt;br /&gt;
	 * empty string.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param s&lt;br /&gt;
	 *            The string to check for nullability or emptiness.&lt;br /&gt;
	 * @param parameterName&lt;br /&gt;
	 *            The name of parameter of the caller method that we want to&lt;br /&gt;
	 *            include in exception description if the tested string is null&lt;br /&gt;
	 *            or empty.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @throws IllegalArgumentException&lt;br /&gt;
	 *             if passed-in string is null or empty.&lt;br /&gt;
	 */&lt;br /&gt;
	protected void forbidEmptyString(String s, String parameterName)&lt;br /&gt;
			throws IllegalArgumentException&lt;br /&gt;
	{&lt;br /&gt;
		if (s == null)&lt;br /&gt;
		{&lt;br /&gt;
			throw new IllegalArgumentException(&lt;br /&gt;
					&amp;quot;Non-null value required in parameter &amp;quot; + parameterName&lt;br /&gt;
							+ &amp;quot;.&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
		if (s.length() == 0)&lt;br /&gt;
		{&lt;br /&gt;
			throw new IllegalArgumentException(&lt;br /&gt;
					&amp;quot;Non-empty value required in parameter &amp;quot; + parameterName&lt;br /&gt;
							+ &amp;quot;.&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Throws IllegalArgumentException if the specified boolean condition is&lt;br /&gt;
	 * false.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param condition&lt;br /&gt;
	 *            The boolean condition string to check for falseness.&lt;br /&gt;
	 * @param message&lt;br /&gt;
	 *            The message to set to the thrown IllegalArgumentException.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @throws IllegalArgumentException&lt;br /&gt;
	 *             if passed-in boolean condition is false.&lt;br /&gt;
	 */&lt;br /&gt;
	protected void forbid(boolean condition, String message)&lt;br /&gt;
			throws IllegalArgumentException&lt;br /&gt;
	{&lt;br /&gt;
		if (condition)&lt;br /&gt;
		{&lt;br /&gt;
			throw new IllegalArgumentException(message);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Throws IllegalStateException if the specified boolean condition is false.&lt;br /&gt;
	 * Same as forbid() except an IllegalStateException is thrown rather than&lt;br /&gt;
	 * IllegalArgumentException.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param condition&lt;br /&gt;
	 *            The boolean condition string to check for falseness.&lt;br /&gt;
	 * @param message&lt;br /&gt;
	 *            The message to set to the thrown IllegalArgumentException.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @throws IllegalStateException&lt;br /&gt;
	 *             if passed-in string is null or empty.&lt;br /&gt;
	 */&lt;br /&gt;
	protected void forbidState(boolean condition, String message)&lt;br /&gt;
			throws IllegalStateException&lt;br /&gt;
	{&lt;br /&gt;
		if (condition)&lt;br /&gt;
		{&lt;br /&gt;
			throw new IllegalStateException(message);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns command line arguments that do not have any switch corresponding&lt;br /&gt;
	 * to them.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &lt;br /&gt;
	 * Alternatively, you can use &amp;lt;code&amp;gt;getValues(getImlicitSwitchName())&amp;lt;/code&amp;gt;.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return The string array consisting of values of switchless arguments in&lt;br /&gt;
	 *         the order they appear in the command line.&lt;br /&gt;
	 */&lt;br /&gt;
	public String[] getSwitchlessArguments()&lt;br /&gt;
	{&lt;br /&gt;
		requireInitialization();&lt;br /&gt;
		forbidState(switchlessArguments == null,&lt;br /&gt;
				&amp;quot;Member switchlessArguments must not be null.&amp;quot;);&lt;br /&gt;
		// forbidState(switchlessArguments.length == 0,&lt;br /&gt;
		// &amp;quot;Member switchlessArguments must not be empty.&amp;quot;);&lt;br /&gt;
		return switchlessArguments;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/*&lt;br /&gt;
	 * Deep implementation methods&lt;br /&gt;
	 */&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Implements the algorithm for parsing command line arguments into&lt;br /&gt;
	 * meaningful pairs of switch name - switch value(s).&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &lt;br /&gt;
	 * This method considers the argument array to follow Java rules for the&lt;br /&gt;
	 * array of arguments passed into Java program's main() method:&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * - On the command line, Java program arguments are separated by spaces and&lt;br /&gt;
	 * tabs, except when an argument is enclosed in double quotes.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * - Leading and trailing whitespace characters are removed from the values&lt;br /&gt;
	 * stored in the arguments array.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * - The double-quoted argument is not broken into several separate&lt;br /&gt;
	 * arguments.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * - For arguments enclosed with double quotes, the double quotes are&lt;br /&gt;
	 * removed.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param args&lt;br /&gt;
	 *            Command line arguments.&lt;br /&gt;
	 * @throws IllegalArgumentException&lt;br /&gt;
	 *             when a violation of parsing rules is encountered, for&lt;br /&gt;
	 *             example, when a switch is present on command line which was&lt;br /&gt;
	 *             not specified in a call to setPossibleSwitches().&lt;br /&gt;
	 * @throws IllegalStateException&lt;br /&gt;
	 *             when the CommandLineParser object is not initialized, for&lt;br /&gt;
	 *             instance, when its the command line arguments have not been&lt;br /&gt;
	 *             set yet. You must at a minimum call setPossibleSwitches(),&lt;br /&gt;
	 *             setArguments() or setCommandLine() before calling this&lt;br /&gt;
	 *             method.&lt;br /&gt;
	 */&lt;br /&gt;
	protected void doParsing(String[] args) throws IllegalArgumentException,&lt;br /&gt;
			IllegalStateException&lt;br /&gt;
	{&lt;br /&gt;
		assert possibleSwitches != null;&lt;br /&gt;
&lt;br /&gt;
		if (possibleSwitches.isEmpty())&lt;br /&gt;
		{&lt;br /&gt;
			throw new IllegalStateException(&lt;br /&gt;
					&amp;quot;This class must be initialized first by calling setPossibleSwitches() and setArguments() method.&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
		if (args == null)&lt;br /&gt;
		{&lt;br /&gt;
			throw new IllegalArgumentException(&lt;br /&gt;
					&amp;quot;This arguments array must not be null.&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Process array elements one by one, accumulating&lt;br /&gt;
		// the found switches and their values into switch-to-value list map.&lt;br /&gt;
&lt;br /&gt;
		Switch curSwitch = null;&lt;br /&gt;
		Map&amp;lt;String, Switch&amp;gt; switchByName = new LinkedHashMap&amp;lt;String, Switch&amp;gt;();&lt;br /&gt;
		List&amp;lt;String&amp;gt; switchless = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
		// Implicit switch to hold the switchless arguments&lt;br /&gt;
		Switch implicit = null;&lt;br /&gt;
		if (implicitSwitch != null)&lt;br /&gt;
		{&lt;br /&gt;
			implicit = createSwitch(implicitSwitch);&lt;br /&gt;
			implicit.setImplicit(true);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// MAIN LOOP: go over arguments one by one, making decisions&lt;br /&gt;
		for (String arg : args)&lt;br /&gt;
		{&lt;br /&gt;
			String argument = arg;&lt;br /&gt;
			// boolean isLastArgument = (i == args.length - 1);&lt;br /&gt;
			boolean isSwitch = looksLikeSwitch(argument);&lt;br /&gt;
			String switchName = isSwitch ? argument : null;&lt;br /&gt;
			boolean needToFinalizePreviousSwitch = (curSwitch != null)&lt;br /&gt;
					&amp;amp;&amp;amp; isSwitch;&lt;br /&gt;
			boolean needToInilializeNewSwitch = isSwitch;&lt;br /&gt;
&lt;br /&gt;
			// Parsing rule 1: forbid unknown switches. All switches must be&lt;br /&gt;
			// declared in a call to setPossibleSwitches().&lt;br /&gt;
			forbid(isSwitch &amp;amp;&amp;amp; !isPossibleSwitch(argument),&lt;br /&gt;
					&amp;quot;Unknown switch found: &amp;quot;&lt;br /&gt;
							+ argument&lt;br /&gt;
							+ &amp;quot;. You must specify this switch in a call to setPossibleSwitches() first.&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
			if (needToFinalizePreviousSwitch)&lt;br /&gt;
			{&lt;br /&gt;
				// Finalize previous switch&lt;br /&gt;
				if (curSwitch != null)&lt;br /&gt;
				{&lt;br /&gt;
					curSwitch = null;&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
			if (needToInilializeNewSwitch)&lt;br /&gt;
			{&lt;br /&gt;
				// Initialize data structures for newly discovered switch&lt;br /&gt;
				curSwitch = createSwitch(argument);&lt;br /&gt;
				switchByName.put(switchName, curSwitch);&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			if (isSwitch)&lt;br /&gt;
			{&lt;br /&gt;
				// Next will come the switch value, if any&lt;br /&gt;
				continue;&lt;br /&gt;
			}&lt;br /&gt;
			else&lt;br /&gt;
			{&lt;br /&gt;
				boolean tooManyArguments = false;&lt;br /&gt;
&lt;br /&gt;
				if (curSwitch != null)&lt;br /&gt;
				{&lt;br /&gt;
					tooManyArguments = (curSwitch.getValues().size() &amp;gt;= curSwitch&lt;br /&gt;
							.getMaxValues());&lt;br /&gt;
				}&lt;br /&gt;
&lt;br /&gt;
				if (curSwitch != null &amp;amp;&amp;amp; !tooManyArguments)&lt;br /&gt;
				{&lt;br /&gt;
					curSwitch.addValue(argument);&lt;br /&gt;
				}&lt;br /&gt;
				else&lt;br /&gt;
				{&lt;br /&gt;
					// Assign argument to switchless arguments&lt;br /&gt;
					switchless.add(argument);&lt;br /&gt;
&lt;br /&gt;
					// If there is an implicit switch, assign argument to it,&lt;br /&gt;
					// too.&lt;br /&gt;
					if (implicitSwitch != null)&lt;br /&gt;
					{&lt;br /&gt;
						implicit.addValue(argument);&lt;br /&gt;
					}&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		// END OF MAIN LOOP&lt;br /&gt;
&lt;br /&gt;
		// Add implicit switch values, if any&lt;br /&gt;
		if (implicitSwitch != null &amp;amp;&amp;amp; implicit.getValues().size() &amp;gt; 0)&lt;br /&gt;
		{&lt;br /&gt;
			switchByName.put(getImplicitSwitch(), implicit);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Copy accumulated switch map to CommandLineParser's switch&lt;br /&gt;
		// collection.&lt;br /&gt;
		switches.clear();&lt;br /&gt;
		switchMap.clear();&lt;br /&gt;
&lt;br /&gt;
		try&lt;br /&gt;
		{&lt;br /&gt;
			for (String switchName : switchByName.keySet())&lt;br /&gt;
			{&lt;br /&gt;
				Switch sw = switchByName.get(switchName);&lt;br /&gt;
				sw.validate();&lt;br /&gt;
				switches.put(switchName, sw);&lt;br /&gt;
				List&amp;lt;String&amp;gt; values = new ArrayList&amp;lt;String&amp;gt;(sw.getValues());&lt;br /&gt;
				// Collections.copy(values, sw.getValues());&lt;br /&gt;
				switchMap.put(switchName, values);&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		catch (IllegalStateException ise)&lt;br /&gt;
		{&lt;br /&gt;
			switches.clear();&lt;br /&gt;
			switchMap.clear();&lt;br /&gt;
			throw new IllegalArgumentException(ise.getMessage());&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Not to forget the rest of the arguments&lt;br /&gt;
		switchlessArguments = new String[switchless.size()];&lt;br /&gt;
		for (int i = 0; i &amp;lt; switchless.size(); i++)&lt;br /&gt;
		{&lt;br /&gt;
			switchlessArguments[i] = switchless.get(i);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		// Considered all possible switches?&lt;br /&gt;
&lt;br /&gt;
		forbidState(switches == null,&lt;br /&gt;
				&amp;quot;Post-condition failure: member 'switches' is null&amp;quot;);&lt;br /&gt;
		forbidState(switchMap == null,&lt;br /&gt;
				&amp;quot;Post-condition failure: member 'switchMap' is null&amp;quot;);&lt;br /&gt;
		forbidState(&lt;br /&gt;
				switchMap.size() != switches.size(),&lt;br /&gt;
				&amp;quot;Post-condition failure: member 'switchMap' has different size from member 'switches'&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns true if CommandLineParser has been initialized, that is the&lt;br /&gt;
	 * setPossibleSwitches() and setArguments() methods have been called.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return True if CommandLineParser has been initialized.&lt;br /&gt;
	 */&lt;br /&gt;
	public boolean isInitialized()&lt;br /&gt;
	{&lt;br /&gt;
		return isInitialized;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns the mapping of switch names to list of their corresponding&lt;br /&gt;
	 * values. Example: for command line &amp;quot;--version major minor&amp;quot; a call to&lt;br /&gt;
	 * getSwitchMap() will return a map consisting of one element which maps&lt;br /&gt;
	 * switch &amp;quot;--version&amp;quot; to list of two values, &amp;quot;major&amp;quot; and &amp;quot;minor&amp;quot; (without&lt;br /&gt;
	 * quotes).&lt;br /&gt;
	 * &lt;br /&gt;
	 * @return The mapping of command line switch names to lists of their&lt;br /&gt;
	 *         corresponding values.&lt;br /&gt;
	 */&lt;br /&gt;
	public Map&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt; getSwitchMap()&lt;br /&gt;
	{&lt;br /&gt;
		return switchMap;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Adds a switch to the required switches.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * Example:&lt;br /&gt;
	 * &lt;br /&gt;
	 * &amp;lt;pre&amp;gt;&lt;br /&gt;
	 * addPossibleSwitch(&amp;amp;quot;--switch(0-*)&amp;amp;quot;);&lt;br /&gt;
	 * &amp;lt;/pre&amp;gt;&lt;br /&gt;
	 * &lt;br /&gt;
	 * will allow for the command line switch &amp;lt;code&amp;gt;--switch&amp;lt;/code&amp;gt; with zero to&lt;br /&gt;
	 * infinite number of arguments.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param switchSpecification&lt;br /&gt;
	 *            Name and cardinality of switch, e.g. --file(1), meaning that&lt;br /&gt;
	 *            the --file switch requires exactly one argument.&lt;br /&gt;
	 */&lt;br /&gt;
	protected void addPossibleSwitch(String switchSpecification)&lt;br /&gt;
	{&lt;br /&gt;
		forbidEmptyString(switchSpecification, &amp;quot;switchSpecification&amp;quot;);&lt;br /&gt;
		switchSpecification = switchSpecification.trim();&lt;br /&gt;
		String switchName = removeCardinality(switchSpecification);&lt;br /&gt;
		forbid(isPossibleSwitch(switchName), &amp;quot;The possible switch &amp;quot;&lt;br /&gt;
				+ switchName + &amp;quot; is already defined for this object.&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		if (!possibleSwitches.contains(switchSpecification))&lt;br /&gt;
		{&lt;br /&gt;
			if (!switchSpecification.endsWith(&amp;quot;)&amp;quot;))&lt;br /&gt;
			{&lt;br /&gt;
				throw new IllegalArgumentException(&lt;br /&gt;
						&amp;quot;Incorrect format of switch:&amp;quot;&lt;br /&gt;
								+ &amp;quot; each switch must be followed by numbers in parenthesis specifying the required number&amp;quot;&lt;br /&gt;
								+ &amp;quot; of its values, e.g. \&amp;quot;--version(0), --file(1) --twomore(2-*) --any(*)\&amp;quot;&amp;quot;);&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			possibleSwitches.add(switchSpecification);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Removes a previously specified with a call to&lt;br /&gt;
	 * &amp;lt;code&amp;gt;addPossibleSwitch()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;setPossibleSwitches()&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * switch from the set of required switches.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * Example:&lt;br /&gt;
	 * &lt;br /&gt;
	 * &amp;lt;pre&amp;gt;&lt;br /&gt;
	 * removePossibleSwitch(&amp;amp;quot;--switch(0-*)&amp;amp;quot;);&lt;br /&gt;
	 * -or-&lt;br /&gt;
	 * removePossibleSwitch(&amp;amp;quot;--switch&amp;amp;quot;);&lt;br /&gt;
	 * &amp;lt;/pre&amp;gt;&lt;br /&gt;
	 * &lt;br /&gt;
	 * will remove the switch &amp;lt;code&amp;gt;--switch&amp;lt;/code&amp;gt; from the set of possible&lt;br /&gt;
	 * command line switches.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param switchSpecification&lt;br /&gt;
	 *            Name and cardinality of switch, e.g. --file(1), specifying the&lt;br /&gt;
	 *            --file switch which requires exactly one argument.&lt;br /&gt;
	 */&lt;br /&gt;
	protected void removePossibleSwitch(String switchSpecification)&lt;br /&gt;
	{&lt;br /&gt;
		forbidEmptyString(switchSpecification, &amp;quot;switchSpecification&amp;quot;);&lt;br /&gt;
		switchSpecification = switchSpecification.trim();&lt;br /&gt;
		int firstBrace = switchSpecification.indexOf(&amp;quot;(&amp;quot;);&lt;br /&gt;
		boolean bracePresent = firstBrace != -1;&lt;br /&gt;
		String switchName = bracePresent ? switchSpecification.substring(0,&lt;br /&gt;
				firstBrace).trim() : switchSpecification;&lt;br /&gt;
		boolean found = false;&lt;br /&gt;
&lt;br /&gt;
		if (bracePresent &amp;amp;&amp;amp; !switchSpecification.endsWith(&amp;quot;)&amp;quot;))&lt;br /&gt;
		{&lt;br /&gt;
			throw new IllegalArgumentException(&lt;br /&gt;
					&amp;quot;Incorrect format of switch:&amp;quot;&lt;br /&gt;
							+ &amp;quot; each switch must be followed by numbers in parenthesis specifying the required number&amp;quot;&lt;br /&gt;
							+ &amp;quot; of its values, e.g. \&amp;quot;--version(0), --file(1) --twomore(2-*) --any(*)\&amp;quot;&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		for (String possibleSwitch : possibleSwitches)&lt;br /&gt;
		{&lt;br /&gt;
			int brace = possibleSwitch.indexOf(&amp;quot;(&amp;quot;);&lt;br /&gt;
			assert brace != -1;&lt;br /&gt;
			String name = possibleSwitch.substring(0, brace).trim();&lt;br /&gt;
&lt;br /&gt;
			if (bracePresent &amp;amp;&amp;amp; possibleSwitch.equals(switchSpecification)&lt;br /&gt;
					|| !bracePresent &amp;amp;&amp;amp; switchName.equals(name))&lt;br /&gt;
			{&lt;br /&gt;
				possibleSwitches.remove(possibleSwitch);&lt;br /&gt;
				found = true;&lt;br /&gt;
				break;&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		if (!found)&lt;br /&gt;
		{&lt;br /&gt;
			throw new IllegalArgumentException(&lt;br /&gt;
					&amp;quot;Switch &amp;quot;&lt;br /&gt;
							+ switchSpecification&lt;br /&gt;
							+ &amp;quot; is not found in the collection of possible switches. You must first call &amp;quot;&lt;br /&gt;
							+ &amp;quot; setPossibleSwitches() or setImplicitSwitch(), or check the spelling of the argument.&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Creates a new Switch object for the specified switchName. Switch name&lt;br /&gt;
	 * must be one of possible switches (see&lt;br /&gt;
	 * {@link CommandLineParser#setPossibleSwitches}() method)&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param switchName&lt;br /&gt;
	 *            The name of switch, e.g. --file, or its specification, e.g.&lt;br /&gt;
	 *            --file(1).&lt;br /&gt;
	 * @return The newly created Switch object.&lt;br /&gt;
	 */&lt;br /&gt;
	public Switch createSwitch(String switchName)&lt;br /&gt;
	{&lt;br /&gt;
		boolean switchSpecificationFound = false;&lt;br /&gt;
		String name = null;&lt;br /&gt;
		int minValues = -1;&lt;br /&gt;
		int maxValues = -1;&lt;br /&gt;
&lt;br /&gt;
		Set&amp;lt;String&amp;gt; switchCandidates = new LinkedHashSet&amp;lt;String&amp;gt;(&lt;br /&gt;
				possibleSwitches);&lt;br /&gt;
&lt;br /&gt;
		if (implicitSwitch != null)&lt;br /&gt;
		{&lt;br /&gt;
			switchCandidates.add(implicitSwitch);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		for (String switchSpecification : switchCandidates)&lt;br /&gt;
		{&lt;br /&gt;
			assert switchSpecification.endsWith(&amp;quot;)&amp;quot;);&lt;br /&gt;
			if (switchSpecification.startsWith(switchName))&lt;br /&gt;
			{&lt;br /&gt;
				boolean nameIsSpecification = switchName&lt;br /&gt;
						.equals(switchSpecification);&lt;br /&gt;
				int leftBrace = switchSpecification.lastIndexOf(&amp;quot;(&amp;quot;);&lt;br /&gt;
				int rightBrace = switchSpecification.lastIndexOf(&amp;quot;)&amp;quot;);&lt;br /&gt;
				forbid(leftBrace == -1,&lt;br /&gt;
						&amp;quot;Malformed switch specification: missing left brace.&amp;quot;);&lt;br /&gt;
				forbid(rightBrace == -1,&lt;br /&gt;
						&amp;quot;Malformed switch specification: missing right brace.&amp;quot;);&lt;br /&gt;
				forbid(leftBrace &amp;gt; rightBrace,&lt;br /&gt;
						&amp;quot;Malformed switch specification: left brace found after right brace.&amp;quot;);&lt;br /&gt;
				forbid(leftBrace == rightBrace - 1,&lt;br /&gt;
						&amp;quot;Malformed switch specification: a number or an interval&amp;quot;&lt;br /&gt;
								+ &amp;quot; must be specified between left and right braces.&amp;quot;);&lt;br /&gt;
				name = switchSpecification.substring(0, leftBrace);&lt;br /&gt;
&lt;br /&gt;
				if (switchSpecificationFound)&lt;br /&gt;
				{&lt;br /&gt;
					break;&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		forbid(!switchSpecificationFound,&lt;br /&gt;
				&amp;quot;Switch &amp;quot;&lt;br /&gt;
						+ switchName&lt;br /&gt;
						+ &amp;quot; is not found among the possible switches or implicit switch. &amp;quot;&lt;br /&gt;
						+ &amp;quot; Check if this switch is specified in a call to setPossibleSwitches() or setImplicitSwitch()&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		Switch newSwitch = new Switch(name, minValues, maxValues);&lt;br /&gt;
		return newSwitch;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns true if switch name looks like a switch, that is, starts with one&lt;br /&gt;
	 * of the prefixes specified by the &amp;lt;code&amp;gt;switchPrefixesMask&amp;lt;/code&amp;gt;.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param switchName&lt;br /&gt;
	 *            Name of switch.&lt;br /&gt;
	 * @return True if switch name starts with one of the prefixes specified by&lt;br /&gt;
	 *         the &amp;lt;code&amp;gt;switchPrefixesMask&amp;lt;/code&amp;gt;.&lt;br /&gt;
	 */&lt;br /&gt;
	protected boolean looksLikeSwitch(String switchName)&lt;br /&gt;
	{&lt;br /&gt;
		boolean looksLikeSwitch = switchName.matches(&amp;quot;^&amp;quot; + switchPrefixesMask&lt;br /&gt;
				+ &amp;quot;.*&amp;quot;);&lt;br /&gt;
		return looksLikeSwitch;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns true if switch is among possible switches that are specified with&lt;br /&gt;
	 * a call to &amp;lt;code&amp;gt;setPossibleSwitches()&amp;lt;/code&amp;gt; or&lt;br /&gt;
	 * &amp;lt;code&amp;gt;setImplicitSwitch()&amp;lt;/code&amp;gt;.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param switchName&lt;br /&gt;
	 *            Name of switch, e.g. --file&lt;br /&gt;
	 * @return True if switch is among possible switches.&lt;br /&gt;
	 */&lt;br /&gt;
	public boolean isPossibleSwitch(String switchName)&lt;br /&gt;
	{&lt;br /&gt;
		boolean result = false;&lt;br /&gt;
&lt;br /&gt;
		for (String possibleSwitch : possibleSwitches)&lt;br /&gt;
		{&lt;br /&gt;
			int leftBrace = possibleSwitch.indexOf(&amp;quot;(&amp;quot;);&lt;br /&gt;
			forbid(leftBrace == -1, &amp;quot;Misformed possible switch element: &amp;quot;&lt;br /&gt;
					+ possibleSwitch);&lt;br /&gt;
			String name = possibleSwitch.substring(0, leftBrace);&lt;br /&gt;
			if (switchName.equals(name))&lt;br /&gt;
			{&lt;br /&gt;
				result = true;&lt;br /&gt;
				break;&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		return result;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Removes switch cardinality (the numbers in braces in the end of switch&lt;br /&gt;
	 * specification) from switch specification. If cardinality is not present,&lt;br /&gt;
	 * returns the same string as the passed-in argument.&lt;br /&gt;
	 * &amp;lt;p&amp;gt;&lt;br /&gt;
	 * Example: &amp;lt;code&amp;gt;removeCardinality(&amp;quot;--file(0-*)&amp;quot;)&amp;lt;/code&amp;gt; will return string&lt;br /&gt;
	 * &amp;quot;--file&amp;quot;&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param switchSpecification&lt;br /&gt;
	 *            Name and cardinality of switch, e.g. --file(0-*)&lt;br /&gt;
	 * @return Name of switch without cardinality, e.g. --file.&lt;br /&gt;
	 */&lt;br /&gt;
	protected String removeCardinality(String switchSpecification)&lt;br /&gt;
	{&lt;br /&gt;
		forbidNullString(switchSpecification, &amp;quot;switchSpecification&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		String result;&lt;br /&gt;
&lt;br /&gt;
		int leftBrace = switchSpecification.indexOf(&amp;quot;(&amp;quot;);&lt;br /&gt;
		int rightBrace = switchSpecification.indexOf(&amp;quot;)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
		if (leftBrace != -1)&lt;br /&gt;
		{&lt;br /&gt;
			forbid(rightBrace == -1,&lt;br /&gt;
					&amp;quot;Malformed switch specification: must have left and right braces&amp;quot;);&lt;br /&gt;
			forbid(rightBrace &amp;lt; leftBrace,&lt;br /&gt;
					&amp;quot;Malformed switch specification: must have right brace after left brace&amp;quot;);&lt;br /&gt;
			result = switchSpecification.substring(0, leftBrace);&lt;br /&gt;
		}&lt;br /&gt;
		else&lt;br /&gt;
		{&lt;br /&gt;
			forbid(rightBrace != -1,&lt;br /&gt;
					&amp;quot;Malformed switch specification: must have left and right braces or have neither&amp;quot;);&lt;br /&gt;
			result = switchSpecification;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		return result;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;</description>
			<pubDate>Sat, 22 Oct 2011 22:30:30 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:CommandLineParser.java</comments>		</item>
		<item>
			<title>My 2 cents for Humanity</title>
			<link>https://www.taitl.com/andrey/My_2_cents_for_Humanity</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here is a utility class I wrote to help writing command line applications. It is surprising how often a command line app directly validates its argument array instead of delegating it to some kind of parser. Use it as you like. &lt;br /&gt;
&lt;br /&gt;
[[CommandLineParser.java]]&lt;br /&gt;
&lt;br /&gt;
[[CommandLineParserTest.java]]&lt;/div&gt;</description>
			<pubDate>Sat, 22 Oct 2011 22:26:26 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:My_2_cents_for_Humanity</comments>		</item>
		<item>
			<title>P-Patterns!</title>
			<link>https://www.taitl.com/andrey/P-Patterns%21</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== P-Patterns ==&lt;br /&gt;
&lt;br /&gt;
Some design patterns I came up with.&lt;br /&gt;
&lt;br /&gt;
== Smart Button ==&lt;br /&gt;
&lt;br /&gt;
Suppose you bought a futuristic DVD player - a slick black box with a single tiny hole for the microphone. It does amazing job of recognizing voice commands. To open the drive bay you would say 'Open the drive'. To pause, you would say 'Do pause', and so on. You liked it in the beginning, but unfortunately, you have to always consult the product manual, because you have to memorize exact commands. For example, one day you might say 'Pause' instead of 'Do pause', and it wouldn't react. Finally, you start wanting a more traditional interface - the one with the buttons, so to operate it you wouldn't have to remember commands. With several buttons, you have complete control over the device and do not have to consult the manual!&lt;br /&gt;
&lt;br /&gt;
The design pattern is:&lt;br /&gt;
&lt;br /&gt;
 Expose a generic object in the interface in order to simplify class use.&lt;br /&gt;
&lt;br /&gt;
This is the case for exposing an object in the interface. I know that there is opposition for doing so, and even a law ([http://www.ccs.neu.edu/home/lieber/LoD.html Law of Demeter]). However, I choose to say that exposing an object that belongs to a generic, well-known class (in the above case, a button) in the interface provides for simpler, more intuitive designs. Compare this to hiding everything under a monolith interface with a laundry list of methods. &lt;br /&gt;
&lt;br /&gt;
Not being entirely opposed to the Law of Demeter, however I often felt that it stems from assumption that we couldn't fully control an object after exposing it in the interface. While it is true in some OO languages, there are designs and approaches that provide for such control ([[Overriding_in_Owner_Class |Example]]). In the presence of such mechanisms, the Law of Demeter may be less crucial.&lt;br /&gt;
&lt;br /&gt;
On the other hand, I'm definetely not advocating for breaking encapsulation and exposing inner objects in class interface. In our example with a DVD player, it only would make sense to expose some objects (buttons, indicators) while keeping others - the one that really do the job - encapsulated. Therefore, more precisely, we want to only expose very generic objects providing for intuitive control, while keeping implementation-specific parts still hidden inside the class.&lt;br /&gt;
&lt;br /&gt;
== Postponed Validation ==&lt;br /&gt;
&lt;br /&gt;
Let's say we have a setProperty() method which changes the value of an object field. One typically validates method arguments within in the body of the method; and sometimes you need more extensive validations. Extra validations may be taxing on performance, especially when the method is called multiple times. In the Postponed Validation pattern, you perform extra validations in the time of a commit. If a validation rule is broken, perform a rollback. Postponing validations until commit lets you ensure data integrity while maintaining high performance.&lt;br /&gt;
&lt;br /&gt;
 Postpone object validation until a checkpoint/commit, so that validations do not hinder performance.&lt;br /&gt;
&lt;br /&gt;
One way to implement postponed validation is '''Detached Validation'''. Create a flag in your class to switch validations on and off, and perform a full validation each time the flag is turned on. While in the 'detached' state (flag is off), skip validations thus improving performance. Enforce each object to be back to its non-detached (flag on) state before a commit/checkpoint.&lt;br /&gt;
&lt;br /&gt;
== Static resolution ==&lt;br /&gt;
&lt;br /&gt;
Hide a class behind a static interface to cut on code verbosity and improve readability. As an alternative to creating a Singleton and retrieving it through getInstance() method, create an extra class that implements same interface through static methods, and provide one instance of your Singleton as a work horse for that class. The client code which looked like &lt;br /&gt;
&lt;br /&gt;
 Logging logging = Logging.getInstance();&lt;br /&gt;
 logging.error(&amp;quot;my message&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
becomes this:&lt;br /&gt;
&lt;br /&gt;
 Log.error(&amp;quot;my message&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
where Log is a static interface now serving as a facade for the Logging class. Bonus: you get a better English capitalization this way!&lt;br /&gt;
&lt;br /&gt;
== Pure Business Objects ==&lt;br /&gt;
&lt;br /&gt;
Divide model classes into pure business objects and their application-specific implementations related through inheritance. In the client code, only deal with pure business object classes. In instantiating/factory/injection code, only instantiate their application-specific implementations. Utilize Bridge design pattern to decouple inheritance graphs of the two hierarchies, thus separating the model from application-specific behavior.&lt;/div&gt;</description>
			<pubDate>Mon, 06 Feb 2006 20:09:27 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:P-Patterns%21</comments>		</item>
		<item>
			<title>Conclusion</title>
			<link>https://www.taitl.com/andrey/Conclusion</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The main point of this article is to consider the nature of an '''object's behavior as consisting of two aspects''': the general behavior, provided by its class, and the behavior specific to a particular context in which the object is used. Conceptually, overriding in owner class is the '''''mechanism for separating the object's behavior specific to a particular context (role) from the behavior specific to its class'''''. Thus, overriding in owner class introduces a '''different way to organize the code defining the behavior'''. Looking at the examples within this article, one can see that it can make it easier to accomplish a variety of tasks, such as: &lt;br /&gt;
&lt;br /&gt;
* to enforce class invariants concerning a member, as is done in the example with [[Definition_of_Overriding_in_Owner_Class#Lee_jeans|''lee_jeans'']]; &lt;br /&gt;
* to create an object managing two-way input and output, as is done in the example with [[Applications_of_Overriding_in_Owner#Visual_cash|''visual_cash'']];&lt;br /&gt;
* to solve the updates issue, as is done in the example with [[Applications_of_Overriding_in_Owner#Working_girl|''working_girl'']];&lt;br /&gt;
* to write a class for immediate reuse without implementing all the minor details, as in the example with [[Applications_of_Overriding_in_Owner#Circular_gauge|''circular_gauge'']];&lt;br /&gt;
* to simplify and &amp;quot;objectify&amp;quot; the interface of a class, as in the example with [[Applications_of_Overriding_in_Owner#Int_property|''int_property'']].&lt;br /&gt;
 &lt;br /&gt;
Overriding in owner class can affect the way you create the designs, particularly in how you set up objects of unrelated classes to work together; how the processing of an action is split from its cause; how reusable classes are written. I think that this approach will help minimize dependencies between classes; reduce their need for updates; avoid redundant subclassing and dynamic structures; provide fuller control over class members. The ease with which this mechanism fits into C++ and the fact that it does not, in my opinion, create inconsistencies in the language makes me think that it logically and usefully extends the language.&lt;br /&gt;
&lt;br /&gt;
== Acknowledgments ==&lt;br /&gt;
I am grateful to Vitaly Shmatikov for reviewing the draft of this paper and helping in its improvement. &lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] A. Potekhin “[[Implementation_of_Overriding_in_Owner_Class|Implementation of Overriding in Owner Class]].”&lt;br /&gt;
&lt;br /&gt;
[2] E. Gamma et al., “Design Patterns, Elements of Reusable Object-Oriented Software”&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''[[Main_Page|To Main Page]]'''&lt;/div&gt;</description>
			<pubDate>Fri, 03 Feb 2006 07:04:58 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:Conclusion</comments>		</item>
		<item>
			<title>Inside the Overriding Mechanism</title>
			<link>https://www.taitl.com/andrey/Inside_the_Overriding_Mechanism</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Among possible implementations of overriding in owner class the most natural is to use the same mechanism of dynamic binding through the virtual method tables as for the usual overriding. The implementation described below is simple, straightforward, does not require special data structures, does not interfere with other language features, and does not require additional memory. The efficiency of a call to an overridden member’s method is the same as of a call to a virtual function of an object, when not optimized by knowing the exact type.&lt;br /&gt;
&lt;br /&gt;
The process of overriding in owner class consists of the following steps:&lt;br /&gt;
&lt;br /&gt;
# A new virtual method table is created for the member. &lt;br /&gt;
# The new table is initialized with the data from the original table. &lt;br /&gt;
# In the new table, the address of the original function(s) is changed to the address of the function override(s). &lt;br /&gt;
# The pointer to the virtual method table in the member object is modified to point to the new table.&lt;br /&gt;
# When entering the body of the function override, ''this'' pointer value is modified to point to the owner object. &lt;br /&gt;
&lt;br /&gt;
Step 5 needs further explanation. When entering the override, ''this'' points to the member for which the function has is been called. But the code of the override resides in the owner class, and deals with the owner class - its other members, its environment, etc. It requires ''this'' pointer to point to the owner object. Therefore, ''this'' must be modified. It is modified by subtracting the offset between the owner and the member object. The place of the member inside the owner class is known in the compile time, so the modification of ''this'' is merely the subtracting of a constant.&lt;br /&gt;
&lt;br /&gt;
[[Image:Overriding.gif|center]]&lt;br /&gt;
&lt;br /&gt;
The first three steps are done one time, when the owner class is initialized. Step four is done every time the owner class is instantiated. Step five is done every time the overridden function is called, when entering the body of the function override. The result of steps 1-4 is a structure shown above. In the figure, PVTBL is a pointer to a virtual method table from an instance of a class; A VTBL is a virtual method table of class A, and B VTBL for B::a is virtual method table created in step one. &lt;br /&gt;
&lt;br /&gt;
One concern that may arise is about the stability of the pointer to the member’s new virtual method table. Can it suffer when an object of the owner class, or its subclass, is assigned to with an object of the owner class when C++ copies objects &amp;quot;by bits&amp;quot;, i.e. when the default assignment operator is used? Fortunately, the answer is no. C++ guarantees that such copying affects neither the pointer to the virtual method table in the owner object, nor the pointers to the virtual method tables in its members.&lt;br /&gt;
&lt;br /&gt;
Algorithm 1-5 was used when implementing overriding in owner class in [1]. [1] contains description of this work, more details, including the information on the modification of ''this'' in case of multiple inheritance, the source code, and the usage sample. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Next: [[Conclusion]]'''&lt;/div&gt;</description>
			<pubDate>Fri, 03 Feb 2006 06:59:32 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:Inside_the_Overriding_Mechanism</comments>		</item>
		<item>
			<title>Questions</title>
			<link>https://www.taitl.com/andrey/Questions</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Does overriding in owner class break strict type checking?'' No, because the signature of the function override must conform to the signature of the overridden function. The conformity is checked in the compile time.&lt;br /&gt;
&lt;br /&gt;
''Does the new overriding break encapsulation?'' No, because the override has the same access rights to the member as the owner class itself.&lt;br /&gt;
&lt;br /&gt;
''Does it break the logic of the member's use by allowing arbitrary changes to its behavior?'' No, because only a virtual function can be overridden. The presence of a virtual function indicates the class was designed to allow change.&lt;br /&gt;
&lt;br /&gt;
''Can the use of overriding in owner class lead to losing control over an already debugged and tested class?'' New overriding uses only the public interface of the member's class; hence its application is a use of the class, not its extension or modification.&lt;br /&gt;
&lt;br /&gt;
''Is the desire to change an object's behavior without subclassing an indicator of a poor design?'' Yes - when the change in behavior is big enough to form a new class. When the change is minor, this is not the case.&lt;br /&gt;
&lt;br /&gt;
''Some compilers use the knowledge of the exact type of an object to optimize a virtual method call. If methods of a class member can be overridden, the compiler can no longer count on the member’s exact type. This can lead to the generation of slower code.'' This is a very serious question, and losing this kind of optimization would be a big sacrifice. There are several ways to resolve this issue:&lt;br /&gt;
&lt;br /&gt;
* The first method is to limit the depth of overriding to just one level, forbidding overriding the methods of a member’s members. The override will be in the same place as the member, and the compiler will know how to make an explicit call. &lt;br /&gt;
&lt;br /&gt;
* The second way is to introduce another language keyword, something similar to ''volatile'', to indicate that the compiler cannot count on knowing the exact member’s class. &lt;br /&gt;
&lt;br /&gt;
* The third solution, which appeals to me the most, is to reuse an existing language keyword. I would like to use the qualifier ''virtual'' to indicate that the compiler cannot count on knowing the exact member’s class. I leave the question of what way is the best and what other solutions might be for further discussion.&lt;br /&gt;
&lt;br /&gt;
''Does overriding in owner class break legacy code?'' No, because it does not change the member's interface. The behavior of the member changes, and this change, provided that it preserves the type, is the purpose of polymorphism in general and overriding in owner class in particular.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Next: [[Inside the Overriding Mechanism]]'''&lt;/div&gt;</description>
			<pubDate>Fri, 03 Feb 2006 06:58:15 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:Questions</comments>		</item>
		<item>
			<title>Discussion of Alternatives</title>
			<link>https://www.taitl.com/andrey/Discussion_of_Alternatives</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Let's consider the alternatives that are used in the existing designs for C++. Suppose Helen has a pager: &lt;br /&gt;
&lt;br /&gt;
 class motorola2sx{ public: void call(); };&lt;br /&gt;
&lt;br /&gt;
 class working_girl{ public: motorola2sx pager; } Helen;&lt;br /&gt;
&lt;br /&gt;
The task is to provide a mechanism that will enable ''working_girl'' class to know that the ''call()'' function of its ''pager'' member has been called. There can be the following solutions:&lt;br /&gt;
&lt;br /&gt;
1. Hide the member and make its interface a subset of the interface of the owner class, not allowing the client to directly access the member:&lt;br /&gt;
&lt;br /&gt;
 class working_girl { public: callpager(); protected: motorola2sx pager; };&lt;br /&gt;
&lt;br /&gt;
Drawbacks: it will be necessary to maintain the duplication of the pager interface by ''working_girl'' class. It will also be necessary to rewrite the telephony algorithms to make them work with the ''working_girl'' class (or its base class).&lt;br /&gt;
&lt;br /&gt;
2. Create a pointer to the owner in the member.&lt;br /&gt;
&lt;br /&gt;
 motorola2sx::call() { owner-&amp;gt;on_incomingcall(); }&lt;br /&gt;
&lt;br /&gt;
This introduces the knowledge about ''working_girl'' into ''motorola2sx'', which is not very logical. This in turn creates two dependencies: a dependency of classes, and a run-time dependency. We could create an abstract class for accepting pager calls with ''working_girl'' inheriting from this class, but that would again increase the number of classes and dependencies in the system.&lt;br /&gt;
&lt;br /&gt;
3. Make motorola2sx a template.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class working_girl&lt;br /&gt;
{ &lt;br /&gt;
public: &lt;br /&gt;
	motorola2sx&amp;lt;working_girl&amp;gt; pager; &lt;br /&gt;
	void on_incomingcall();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This requires ''working_girl'' to support a certain interface. The manufacturers of pagers, telephones, etc. will have to agree on a standard protocol for call notification. Any changes in the protocol will involve all the products and all people, which can result in high costs.&lt;br /&gt;
&lt;br /&gt;
4. Create a special custom wrapper class ''working_girl_pager'':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class working_girl_pager : public motorola2sx &lt;br /&gt;
{ public: void call() { callback(); } };&lt;br /&gt;
&lt;br /&gt;
class working_girl&lt;br /&gt;
{ public: working_girl_pager pager; friend class working_girl_pager; };&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The ''working_girl_pager'' will have unnecessary access to the internals of ''motorola2sx''. It will have to be adjusted every time the interface of motorola2sx changes. ''Working_girl_pager'' will have to have full access to the internals of ''working_girl'', because it has to implement the reaction to a call. Besides, ''working_girl_pager'' is absolutely non-reusable.&lt;br /&gt;
&lt;br /&gt;
5. 	Encapsulate the concept, which varies. Because the processing of calls is what can be different for different owners, take it outside of ''motorola2sx'', encapsulating it in a separate class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class pager_calls_acceptor { public: virtual void on_call()=0; };&lt;br /&gt;
&lt;br /&gt;
motorola2sx::call() { calls_acceptor-&amp;gt;on_call(); }&lt;br /&gt;
&lt;br /&gt;
class working_girl_calls_acceptor : public pager_calls_acceptor { &lt;br /&gt;
	working_girl* girl; &lt;br /&gt;
public: &lt;br /&gt;
	void on_call() { girl-&amp;gt;callback(); } &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Drawbacks: creation and maintenance of the additional dynamic structures (a pager keeps a pointer to a ''pager_calls_acceptor'', pointing to a concrete ''working_girl_calls_acceptor'', which in turn keeps a pointer to ''working_girl'' which owns an instance of the pager as a member).&lt;br /&gt;
&lt;br /&gt;
6. 	Create a broadcast mechanism and send a message that Helen's pager has been called&lt;br /&gt;
&lt;br /&gt;
 motorola2sx::call() { broadcast-&amp;gt;message(somebody_is_calling, this); }&lt;br /&gt;
&lt;br /&gt;
The approach can incur a computational overhead, since the message will have to be processed in all of the objects reacting to the broadcast. In addition, the broadcast code is difficult to write and debug. Finally, any object reacting to the broadcast may be able to overhear calls to Helen.&lt;br /&gt;
&lt;br /&gt;
7. 	Inherit ''working_girl'' from ''motorola2sx''&lt;br /&gt;
 class working_girl : public motorola2sx { public: void call() { callback(); }};&lt;br /&gt;
&lt;br /&gt;
Although it is logically unsatisfying, let's include this approach as well. It distorts the purpose of ''working_girl'', making it a subtype of ''motorola2sx''. The internals of the base classes are exposed to ''working_girl'', breaking encapsulation. If there is more than one object for ''working_girl'' to control, we need multiple inheritance, possibly opening the door to ambiguity problems.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The approaches 1-7 also have the following traits. &lt;br /&gt;
* Approach 1, as the approach 7, distorts the purpose of working_girl, making it a subtype of motorola2sx.&lt;br /&gt;
* Approaches 2, 3, 5, and 6, in order to adapt the member pager to work with its owner, modify the class of pager by adding the code for notifications. This leads to problems discussed earlier in “[[Applications_of_Overriding_in_Owner#Problem_of_Dependencies|The Problem of Dependencies]]”, “[[Applications_of_Overriding_in_Owner#Problem_of_Notifications|The Problem of Notifications]]”, and “[[Applications_of_Overriding_in_Owner#Code_Reusability|Code Reusability]]” sections. &lt;br /&gt;
* The disadvantages of approach 4 are shown in “[[Applications_of_Overriding_in_Owner#Exporting_a_Member_in_the_Interface|Exporting a Member Object in the Interface]]” section. &lt;br /&gt;
* Approaches 1, 2, 4, 5, 6 add new members, functions or classes to the system. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
8. 	Using overriding in owner class differs from most of these approaches by narrower area of application, because an object for which the overriding is done must be included in the owner class as a contained member, while most of the other approaches can be used when the object is accessible through a pointer or a reference. However, in its area of application, overriding in owner class will have certain advantages. ''Override the function in the owner class:''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class motorola2sx{ public: virtual void call()=0; };&lt;br /&gt;
&lt;br /&gt;
class working_girl&lt;br /&gt;
{ &lt;br /&gt;
public:&lt;br /&gt;
	motorola2sx pager;&lt;br /&gt;
	void pager::call() { callback(); }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The results of are summarized in the following table. The columns represent the numbers of described solutions:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;width:75%;&amp;quot; border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;0&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot; &lt;br /&gt;
|  || width=&amp;quot;25&amp;quot; align=&amp;quot;center&amp;quot; | 1 || width=&amp;quot;25&amp;quot; align=&amp;quot;center&amp;quot; | 2 || width=&amp;quot;25&amp;quot; align=&amp;quot;center&amp;quot; | 3 || width=&amp;quot;25&amp;quot; align=&amp;quot;center&amp;quot; | 4 || width=&amp;quot;25&amp;quot; align=&amp;quot;center&amp;quot; |  5 || width=&amp;quot;25&amp;quot; align=&amp;quot;center&amp;quot; | 6 || width=&amp;quot;25&amp;quot; align=&amp;quot;center&amp;quot; | 7 || width=&amp;quot;25&amp;quot; align=&amp;quot;center&amp;quot; | 8 &lt;br /&gt;
|-&lt;br /&gt;
| Increasing class dependencies ||  || bgcolor=black |  || bgcolor=black |  || bgcolor=black |  || bgcolor=black |  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
| Necessity of updates ||  || bgcolor=black |  || bgcolor=black |  ||  || bgcolor=black |  || bgcolor=black |  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
| Breaking encapsulation ||  ||  ||  || bgcolor=black | ||  ||  || bgcolor=black | ||  &lt;br /&gt;
|-&lt;br /&gt;
| Client code changes || bgcolor=black |  ||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
| Creation and maintenance of additional classes ||  ||  ||  || bgcolor=black |  || bgcolor=black |  || bgcolor=black |  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
| Creation and maintenance of special dynamic struc-tures ||  ||  ||  ||  || bgcolor=black |  || bgcolor=black |  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
| Reducing efficiency, redundancy ||  ||  ||  ||  ||  || bgcolor=black |  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
| Complication of debugging ||  || bgcolor=black |  ||  ||  || bgcolor=black |  ||   bgcolor=black |  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
| Changing the logic of use || bgcolor=black |  ||  ||  ||  ||  ||  || bgcolor=black |  ||  &lt;br /&gt;
|-&lt;br /&gt;
| Making it harder to understand the code || bgcolor=black |  ||  ||  ||  ||  ||  || bgcolor=black |  ||  &lt;br /&gt;
|-&lt;br /&gt;
| Undermining reusability ||  || bgcolor=black |  || bgcolor=black |  ||  || bgcolor=black |  || bgcolor=black |  ||  ||  &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Next: [[Questions]]'''&lt;/div&gt;</description>
			<pubDate>Fri, 03 Feb 2006 06:52:17 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:Discussion_of_Alternatives</comments>		</item>
		<item>
			<title>Applying to Design Patterns</title>
			<link>https://www.taitl.com/andrey/Applying_to_Design_Patterns</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Overriding in owner class can also be applied to the design patterns described in [2]. Moreover, it can simplify the applications of some patterns. &lt;br /&gt;
&lt;br /&gt;
=== Pluggable adapter ===&lt;br /&gt;
&lt;br /&gt;
The Pluggable Adapter pattern is used when code requires a special interface from an object. To provide such an interface, a class called adapter is created and passed to the code.&lt;br /&gt;
&lt;br /&gt;
[[Image:Adapter.gif|center]]&lt;br /&gt;
&lt;br /&gt;
Client code works with an abstract adapter, defining the interface. The actual adapter is an instance of a concrete adapter class, working with the particular class it adapts. Process of adaptation consists of creation of the concrete adapter class, overriding the functions of abstract adapter, and giving an instance of a concrete adapter to the client code. &lt;br /&gt;
&lt;br /&gt;
Overriding in owner class allows doing the adaptation without creating a concrete adapter class and an object. &lt;br /&gt;
&lt;br /&gt;
'''Example.''' A pluggable adapter without need for an intermediate class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// abstract adapter&lt;br /&gt;
class interview_adapter&lt;br /&gt;
{ &lt;br /&gt;
public: &lt;br /&gt;
	virtual answer ask(question q) =0; &lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// class to adapt – no concrete adapters&lt;br /&gt;
class person&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
	interview_adapter interviewee;&lt;br /&gt;
	answer interviewee::ask(question q) { return think_hard(q); }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Chain Of Responsibility ===&lt;br /&gt;
&lt;br /&gt;
This pattern deals with the situation when an object processing a request is not known to the object initiating a request.&lt;br /&gt;
&lt;br /&gt;
[[Image:Chain.gif|center]]&lt;br /&gt;
&lt;br /&gt;
Suppose the user has pressed a dialog button, such as a help button, but the knowledge about what to do is not in the button class, and not in its owner, the dialog, but is in the class which owns the dialog - the application's main window. The object ‘main window’ is not known to the object ‘button’. To transmit the request to the processing object, Chain Of Responsibility uses inheritance: we make the button, the dialog, and the main window subclasses of a class that is able to process the request (for instance, of the class ''HelpHandler'' with a virtual function ''Help()''). When the request comes, the button uses the default implementation of the handler, which redirects the request to the owner (''owner-&amp;gt;Help()''), and it is repeated until the request reaches an object capable of processing it. To apply the Chain Of Responsibility pattern, we have to make the classes in the chain a) share an interface for processing a particular request, and b) know about the owner object. Such requirements mean that the Chain Of Responsibility can be applied only if all its participants were designed to participate in it. It will be necessary to modify all the classes in the chain if we need to process a new type of request (e.g. another button).&lt;br /&gt;
&lt;br /&gt;
Overriding in owner class can help eliminate these drawbacks. Indeed, it is a vehicle for transferring the request from the initiator to the acceptor with no need for the participants to implement any interface(s), and with no need to create additional classes and protocols.&lt;br /&gt;
&lt;br /&gt;
'''Example.''' An example of an application SaveAs dialog.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class button &lt;br /&gt;
{ &lt;br /&gt;
public: &lt;br /&gt;
	virtual void press() =0; &lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class saveas_dialog&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
	string new_name;&lt;br /&gt;
	button OK;&lt;br /&gt;
	button Help;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class main_window&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
	saveas_dialog saveas;&lt;br /&gt;
&lt;br /&gt;
	saveas::OK::press() { save(active_document(), saveas.new_name); }&lt;br /&gt;
	saveas::Help::press() { run_help(HLP_SAVEAS); }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note, that the code of the intermediate class, ''saveas_dialog'', remains free from processing and transporting the requests. It does not even have to know it participates in the Chain Of Responsibility!&lt;br /&gt;
&lt;br /&gt;
=== Mediator ===&lt;br /&gt;
&lt;br /&gt;
The Mediator pattern is used to decrease dependencies when objects work in a composition. When the objects have to manipulate each other, it may have a negative impact on the reusability of their code. It also complicates the modifications of the composition, “because its behavior is distributed among objects” [2]. As a solution, a mediator object is created, which encapsulates collective behavior, removing the need for the parts of the composition to directly ‘talk’ to each other. All the requests come from and go to the mediator object:&lt;br /&gt;
&lt;br /&gt;
[[Image:Mediator.gif|center]]&lt;br /&gt;
&lt;br /&gt;
The Mediator pattern frees the classes of the participants from needing to know about each other. However, classes still have to know about the mediator class, and how to deal with the mediator object. Applying overriding in owner class to the Mediator pattern lets us to avoid the creation of: a) a special mediator class; b) a run-time mediator object; and c) a protocol participant-to-mediator. This leaves the code of the participant classes not only free from having to know about each other, but also free from knowing about their participation in the composition.&lt;br /&gt;
&lt;br /&gt;
'''Example.''' Consider a form displaying database records, consisting of a list of items in the database (''items'' listbox), a picture representing a currently selected item (''picture'' image), a description of the item (''description'' editbox). The user can edit the description. When the user finishes editing, he/she presses the ''apply'' button to enter the data in the database. The behavior of the parts in the form should be coordinated: picture and description must refresh when the current item selection changes; the ''apply'' button should be disabled until the user changes the item description, after pressing ''apply'', the changes should be written to the database. These interactions are shown in the figure below. The arrows show how the objects influence each other. Underlined objects are the ones that are visible to the user. In the Mediator pattern, we need to create an object that accepts requests from the participants, processes them and issues requests/updates to other participants. Overriding in owner class will allow us to eliminate the mediator class, the mediator object, and the mediator protocol by overriding member functions of the participants in the composition class. '''The classes of the participants will not have to support the protocol of mutual requests and updates. They will not have to deal with some designated object to send the notifications to.''' Instead, the owner class will take care of all the traits in the behavior of its components specific for the work in the form.&lt;br /&gt;
&lt;br /&gt;
[[Image:Form.gif|center]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class dbform&lt;br /&gt;
{&lt;br /&gt;
	database&amp;amp; data;&lt;br /&gt;
	long current_index;&lt;br /&gt;
	dbitem current_item;&lt;br /&gt;
&lt;br /&gt;
public:&lt;br /&gt;
	listbox items; // list of items in stock&lt;br /&gt;
	bitmap picture; // current item image&lt;br /&gt;
	editbox description; // current item description&lt;br /&gt;
	button apply; // apply changes button&lt;br /&gt;
&lt;br /&gt;
	// constructor&lt;br /&gt;
	dbform(database&amp;amp; d) : data(d) {} &lt;br /&gt;
&lt;br /&gt;
	// listbox handlers&lt;br /&gt;
	void items::init() &lt;br /&gt;
	{ &lt;br /&gt;
		items.source = data.list();&lt;br /&gt;
		items::change();&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	void items::change() &lt;br /&gt;
	{ &lt;br /&gt;
		// selecting an item&lt;br /&gt;
		current_index = items.selection(); &lt;br /&gt;
		current_item = data(current_index); &lt;br /&gt;
		description.update();&lt;br /&gt;
		picture.update(); &lt;br /&gt;
		apply.enable(false);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// editbox handlers&lt;br /&gt;
	void description::update() &lt;br /&gt;
	{ &lt;br /&gt;
		description.set(current_item.description); &lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	void description::changed() &lt;br /&gt;
	{ &lt;br /&gt;
		apply.enable(true); &lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// bitmap handler&lt;br /&gt;
	void picture::update() &lt;br /&gt;
	{ &lt;br /&gt;
		picture.set(current_item.picture); &lt;br /&gt;
		picture.redraw(); &lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	// button handler&lt;br /&gt;
	// called when user presses the button&lt;br /&gt;
	void apply::press()&lt;br /&gt;
	{ &lt;br /&gt;
		current_item.description = description.get(); &lt;br /&gt;
		data.set(current_index, current_item); &lt;br /&gt;
	} &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Next: [[Discussion of Alternatives]]'''&lt;/div&gt;</description>
			<pubDate>Fri, 03 Feb 2006 06:48:49 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:Applying_to_Design_Patterns</comments>		</item>
		<item>
			<title>Applications of Overriding in Owner</title>
			<link>https://www.taitl.com/andrey/Applications_of_Overriding_in_Owner</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Overriding in owner class can be helpful in the following situations: composing objects for collective work; customizing an object’s behavior for a particular use; propagating requests from components to aggregates; implementing control over manipulations on class members; and others.&lt;br /&gt;
We will consider the application of overriding in owner class to the following problems:&lt;br /&gt;
&lt;br /&gt;
== Problem of Dependencies ==&lt;br /&gt;
Consider class F with members a and b, belonging to classes A and B respectively. Code C1 works with class B. When changes happen to F::b, F::a must be changed. This means that B, otherwise not tied with A, must know about A's existence to send a notification (the knowledge about A is brought into B, introducing member-to-member dependency). Alternatively, B must know about F's existence, to get F to notify F::a (the knowledge about F is brought into B, introducing member-to-owner dependency). The third solution is to rewrite code C1 to work with F instead of B (the knowledge about F is brought in C1, the interface of B or its part is included in the interface of F).&lt;br /&gt;
&lt;br /&gt;
[[Image:over1.gif|center]]&lt;br /&gt;
&lt;br /&gt;
'''Problem:''' Developing a system from components, one has to make the code of some components depend on the existence, interface, and/or behavior of others. &lt;br /&gt;
&lt;br /&gt;
Overriding in owner class (overriding methods of F::b in F) allows one to leave the code of B and C1 free from the need to know about owners and neighbors. It lets B remain simpler and more reusable, and leaves C1 unchanged. The details specific to F stay where they should: in class F.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;Visual_cash&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example.''' The ''visual_cash'' class is a screen representation of money, made of two parts: ''cash'', providing data for the financial algorithms, and ''editbox'', reflecting changes made by the algorithms on screen and letting the user edit the amounts. Problem: changes made to one part must be reflected in the other. How to write these classes?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class visual_cash&lt;br /&gt;
{	&lt;br /&gt;
public:&lt;br /&gt;
	cash value; // data&lt;br /&gt;
	editbox face; // representation&lt;br /&gt;
&lt;br /&gt;
	// overriden functions connect &lt;br /&gt;
	// data with representation&lt;br /&gt;
&lt;br /&gt;
	void value::set(float l) &lt;br /&gt;
	{ &lt;br /&gt;
		value.cash::set(l);&lt;br /&gt;
		face.set_text(value.get());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	void face::init() &lt;br /&gt;
	{ &lt;br /&gt;
		face.set_text(value.get()); &lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	void face::changed() &lt;br /&gt;
	{&lt;br /&gt;
		// when the user changes the text &lt;br /&gt;
		// in the widget, value is updated. &lt;br /&gt;
		value.cash::set(to_float(face.get_text())); &lt;br /&gt;
	}&lt;br /&gt;
} visual_money;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// Pass visual_money.value to an algotithm,&lt;br /&gt;
// and if it is changed, the display is updated.&lt;br /&gt;
invest(&amp;amp;visual_money.value);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The components of ''visual_cash'' class are coordinated, however classes ''editbox'', ''cash'' and the algorithms like ''create_profit()'' remain free from the need to know about each other. The composition-specific peculiarities remain in the composition class.&lt;br /&gt;
&lt;br /&gt;
== Problem of Notifications ==&lt;br /&gt;
&lt;br /&gt;
There is a class B with a member a, belonging to class A. Code C1 works with objects of class A, in particular with the member of B. B should know what C1 does to B::a. Thus A should notify B about the operations performed on itself, or C1 should be changed to notify B about them.&lt;br /&gt;
&lt;br /&gt;
[[Image:over2.gif|center]]&lt;br /&gt;
&lt;br /&gt;
'''Problem:''' The code that makes changes must issue notifications about them. &lt;br /&gt;
&lt;br /&gt;
Overriding in owner class (overriding methods of B::a in B) allows to one leave the code of A and C1 free from sending notifications to B about what happens to B::a. The code of A remains simpler and more reusable, the code C1 does not change.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;Working_girl&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example.''' Helen has a pager. She has to know about incoming calls.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// pager class&lt;br /&gt;
class motorola2sx&lt;br /&gt;
{ &lt;br /&gt;
public:&lt;br /&gt;
	virtual void call();&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class working_girl&lt;br /&gt;
{ &lt;br /&gt;
public:&lt;br /&gt;
	motorola2sx pager;&lt;br /&gt;
&lt;br /&gt;
	// making owner know about call&lt;br /&gt;
	void pager::call() { callback(); }&lt;br /&gt;
} Helen;&lt;br /&gt;
&lt;br /&gt;
// calling Helen&lt;br /&gt;
Helen.pager.call();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, thanks to using overriding in owner class, notifications are completely absent. Reacting to the incoming calls is the responsibility of the pager owner, and, according to this, the code reacting to calls resides in the owner class. The ''motorola2sx'' class is focused on solving its own problems. The vendor of ''motorola2sx'' does not have to take care of the need to notify somebody about the calls.&lt;br /&gt;
&lt;br /&gt;
== Code Reusability ==&lt;br /&gt;
&lt;br /&gt;
Class A is adapted for work with B, but the tight coupling to the interface and behavior of B makes it hard to adapt A for work with C.&lt;br /&gt;
&lt;br /&gt;
[[Image:over3.gif|center]]&lt;br /&gt;
&lt;br /&gt;
'''Problem:''' The peculiarities specific to how an object's works with other objects find themselves in the object's class and make the latter less reusable. &lt;br /&gt;
&lt;br /&gt;
With the help of overriding in owner class, the code specific to the role played by B::a in B can be placed in class B, with the result of improving A's reusability.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;Circular_gauge&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example.''' This is an example of a circular gauge that can be used in various devices:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class circular_gauge &lt;br /&gt;
{ &lt;br /&gt;
public: &lt;br /&gt;
	virtual float get_data() =0; // called by draw() to display current value&lt;br /&gt;
	void draw(); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class voltmeter &lt;br /&gt;
{ &lt;br /&gt;
public: &lt;br /&gt;
	circular_gauge face;&lt;br /&gt;
	float face::get_data() { return voltage(schema, loc); } &lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class compass &lt;br /&gt;
{ &lt;br /&gt;
public: &lt;br /&gt;
	circular_gauge face;&lt;br /&gt;
	float face::get_data() { return azimuth(); } &lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class speedometer&lt;br /&gt;
{ &lt;br /&gt;
public: &lt;br /&gt;
	circular_gauge face;&lt;br /&gt;
	float face::get_data() { return weel[0].rotationspeed() * weel[0].radius; } &lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because the ''circular_gauge'' class does not contain code specific to the various devices and does not use special protocols, like notifications, it is well suited for use with any of them. Note that even though ''circular_gauge'' has a pure virtual method, in the presence of a new overriding mechanism it is a complete, ‘off the shelf’, ready to use component. &lt;br /&gt;
&lt;br /&gt;
== Exporting a Member in the Interface ==&lt;br /&gt;
&lt;br /&gt;
I want to let the client of B apply code C1 to B's member B::a, but B is not aware of the changes C1 can make.&lt;br /&gt;
&lt;br /&gt;
[[Image:over4.gif|center]]&lt;br /&gt;
&lt;br /&gt;
'''Problem:''' Exporting (making public) class members in C++ is complicated by the absence of mechanisms allowing a class to control what happens to its members. &lt;br /&gt;
&lt;br /&gt;
Currently the necessary job is done by adding new classes and dependencies to the code. For example, one solution is to make B::a belonging to a class A1, inheriting from class A extended to notify B. It has a number of drawbacks, which are discussed later in detail in the “Comparison with Alternatives” section. Overriding in owner class (overriding of the methods of B::a in B) lets B control its members without resorting to subclassing or introducing new dependencies.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;Text_editor&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Example.''' Consider an ''editor'' having a ''selected_text'' having a ''font''. When changing the ''font'', it is necessary for the ''editor'' to ''update_display()''. We can do it by overriding the methods of the ''font'' member of the ''editor'' class’s ''selected_text'' member.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class text_font &lt;br /&gt;
{ &lt;br /&gt;
	int size;&lt;br /&gt;
public: &lt;br /&gt;
	virtual void set_size(int i){ size = i; } &lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class text_run &lt;br /&gt;
{ &lt;br /&gt;
public: &lt;br /&gt;
	text_font font; &lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class editor_app &lt;br /&gt;
{ &lt;br /&gt;
	text_run all_text;&lt;br /&gt;
public: &lt;br /&gt;
	text_run selected_text;&lt;br /&gt;
&lt;br /&gt;
	virtual void selected_text::font::set_size(int i)&lt;br /&gt;
	{ &lt;br /&gt;
		// call the original&lt;br /&gt;
		selected_text.font.text_font::set_size(i); 	&lt;br /&gt;
		// draw changes&lt;br /&gt;
		update_display(selected_text); &lt;br /&gt;
	} &lt;br /&gt;
} editor;&lt;br /&gt;
&lt;br /&gt;
void some_func(text_run&amp;amp; text)&lt;br /&gt;
{&lt;br /&gt;
	text.font.set_size(default_size);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// see changes reflecting on the screen&lt;br /&gt;
some_func(editor.selected_text); &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the algorithms manipulating the text (''some_func'') need not know about the classes that depend on the changes. The code of ''text_run'' does not have to transmit requests from the receiving object (''text_run::font'') to the object that participates in their handling (''editor''), because it is taken care of by overriding in owner class mechanism. Note that the class ''text_run'' is not required to override functions of its member ''font''.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Concept of Property ==&lt;br /&gt;
&lt;br /&gt;
I want to interpret B::a not simply as a member of B, but as its ''property''. In other words, when one requests the value of B::a, I want some function to get called.&lt;br /&gt;
&lt;br /&gt;
'''Problem:''' There are no mechanisms in C++ allowing hiding a function behind a member's name. &lt;br /&gt;
&lt;br /&gt;
Here is how we can do something like a property with standard C++:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// abstract property class&lt;br /&gt;
class int_property &lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
	virtual operator int() =0; &lt;br /&gt;
	virtual int operator =(int i) =0;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class window;&lt;br /&gt;
&lt;br /&gt;
// concrete property class&lt;br /&gt;
class window_width : public int_property&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
	window *owner;&lt;br /&gt;
	operator int(); &lt;br /&gt;
	int operator =(int i);&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class window&lt;br /&gt;
{&lt;br /&gt;
public: &lt;br /&gt;
	window_width width;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// implementations&lt;br /&gt;
window_width::operator int() const { return ::get_window_width(owner); }&lt;br /&gt;
int window_width::operator =(int i) { ::set_window_width(owner, i); }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The ''window'' class exports an object of the class ''window_width'' in its interface. To begin with, there are disadvantages caused by exporting a member. Secondly, because ''window_width'' knows the interface of the owner class, the approach has the disadvantages described above in “The Problem of Dependencies”. Thirdly, the class ''window_width'' is absolutely non-reusable. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;Int_property&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Overriding in owner class allows us make a more elegant implementation of the property concept:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// an abstract class for all integer properties&lt;br /&gt;
class int_property &lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
	virtual operator int() =0; &lt;br /&gt;
	virtual int operator =(int i) =0;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class window&lt;br /&gt;
{&lt;br /&gt;
public: &lt;br /&gt;
	int_property width;&lt;br /&gt;
	&lt;br /&gt;
	// a property customized for the window class&lt;br /&gt;
	width::operator int() const { return ::get_window_width(this); };&lt;br /&gt;
	int width::operator =(int i) { ::set_window_width(this, i); } ;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Next: [[Applying to Design Patterns]]'''&lt;/div&gt;</description>
			<pubDate>Fri, 03 Feb 2006 06:12:20 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:Applications_of_Overriding_in_Owner</comments>		</item>
		<item>
			<title>Definition of Overriding in Owner Class</title>
			<link>https://www.taitl.com/andrey/Definition_of_Overriding_in_Owner_Class</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Consider a class A with a public virtual function f():&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A { &lt;br /&gt;
public: &lt;br /&gt;
	virtual int f();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class B contains a member a belonging to class A:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class B {&lt;br /&gt;
	A a;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is necessary to perform certain actions in B at the moment the user code calls method f() of its member B::a.&lt;br /&gt;
&lt;br /&gt;
:'''The mechanism of overriding in the owner class is an extension of polymorphism allowing a virtual function of class A to be overridden in class B which includes an object of A as a member.'''&lt;br /&gt;
&lt;br /&gt;
According to this definition, f() can be overridden ''in class B''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class B {&lt;br /&gt;
	A a;&lt;br /&gt;
	int a::f();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
so that the following is true:&lt;br /&gt;
 a.f(); // B::a::f() is called, instead of A::f()&lt;br /&gt;
 …&lt;br /&gt;
 A&amp;amp; r = a;&lt;br /&gt;
 r.f(); // B::a::f() is called, instead of A::f()&lt;br /&gt;
&lt;br /&gt;
Now every time the client calls f() on B::a, B is aware of it. B also has the possibility to adapt f()'s implementation to its own needs. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;Lee_jeans&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
'''Example.'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class size&lt;br /&gt;
{&lt;br /&gt;
	long value;&lt;br /&gt;
public:&lt;br /&gt;
	long get() { return value; }&lt;br /&gt;
	virtual void set(long l) { value == l; }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class lee_jeans&lt;br /&gt;
{&lt;br /&gt;
	size waist, inseam;&lt;br /&gt;
&lt;br /&gt;
	void waist::set(long l) &lt;br /&gt;
	{ &lt;br /&gt;
		assert(l &amp;gt; 28 &amp;amp;&amp;amp; l &amp;lt;= 36 ); &lt;br /&gt;
		// call the original&lt;br /&gt;
		waist.size::set(l); &lt;br /&gt;
	}	&lt;br /&gt;
	void inseam::set(long l) &lt;br /&gt;
	{ &lt;br /&gt;
		assert(l &amp;gt; 30 &amp;amp;&amp;amp; l &amp;lt;= 42 ); &lt;br /&gt;
		// call the original&lt;br /&gt;
		inseam.size::set(l); &lt;br /&gt;
	}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example shows how overriding in the owner can help a class to control the values assigned to its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Requirements to participating classes.'''&lt;br /&gt;
&lt;br /&gt;
Overriding in owner has the following requirements to participating classes:&lt;br /&gt;
&lt;br /&gt;
# The first participating class (class A) must declare a public virtual function.&lt;br /&gt;
# The second participating class (class B) must provide a function override and include an object of class A as a member.&lt;br /&gt;
# The declaration of the override associates the name of the overridden function with the name of the member for which the override is done. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Similarities with the usual overriding.'''&lt;br /&gt;
&lt;br /&gt;
New overriding has the following in common with the standard overriding of C++:&lt;br /&gt;
&lt;br /&gt;
*	When entering the body of override, ''this'' points to an instance of the overriding class (B), not of the original one (A). However, in our case these classes are not related through the mechanism of inheritance.&lt;br /&gt;
&lt;br /&gt;
*	The override is called not only when the original function is called directly on the member, but also when the original function is called on a pointer to, or reference to the member object, a pointer to one of the member's ancestors in the inheritance chain, or a pointer to one of the member's base classes in the multiple inheritance – in other words, when the exact type of the member is not known to the caller.&lt;br /&gt;
&lt;br /&gt;
*	The function override is called ''instead'' of the original function, and therefore it must call the original function from its body, if it is not a full substitute. The call should be qualified with the name of the member’s class, to disable the virtual binding and prevent the override from getting called again. In addition, the call should be qualified with the name of the member itself (in our case, a.A::f(), not just A::f()), because an original function can only be called on an object belonging to the type of the member (A).&lt;br /&gt;
&lt;br /&gt;
*	A property of transitivity: a class can override not only the functions of its members, but also the functions of the members of its members. If we declare B::a::f() virtual and override it in the class C (suppose C can access methods of B):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class C &lt;br /&gt;
{&lt;br /&gt;
	B b;&lt;br /&gt;
	int b::a::f();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
then the following is true&lt;br /&gt;
 b.a.f();		// C::b::a::f() is called&lt;br /&gt;
 b.a.B::a::f();		// B::a::f() is called&lt;br /&gt;
 b.a.A::f();		// A::f() is called&lt;br /&gt;
&lt;br /&gt;
If f() is not overridden in the class B, expression &lt;br /&gt;
 b.a.B::a::f();&lt;br /&gt;
is equivalent to b.a.A::f().&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Differences with to the usual overriding.'''&lt;br /&gt;
&lt;br /&gt;
Overriding in owner class is not simply the usual C++ overriding with the owner class playing the role of the derived class. There are some important differences:&lt;br /&gt;
&lt;br /&gt;
*	The overriding is done not for a class (A), but for a member of a class (B::a), that is, it does not affect any objects except the member object.&lt;br /&gt;
*	Only a function belonging to the public interface of the member's class can be overridden.&lt;br /&gt;
*	In the body of the function override, only the public interface of the member's class can be used. That is, overriding in owner class gives different access rights to its first participating class than the usual override. &lt;br /&gt;
*	A class can use an abstract class for its member. In this case, one must use overriding in owner class to provide implementation for any pure virtual functions of the member’s class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Note, that we only consider the overriding of methods of a contained member. Overriding at run time of methods of objects referred to by pointers initially looked very interesting. However, it turned out to be too complicated for debugging, because of its often unpredictable, extremely dynamic nature. I have left such applications outside of the scope of this article. In case of overriding for a contained member, the override declaration is close to the member, making it easy to understand the relationship. Besides, the implementations of the overriding mechanism can rely on the exact type of member object, known at compile time, instead of relying on the dynamic dependencies at run-time. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Overriding in owner class is not extending or modifying the interface of the owner class. Function overrides belong to the interface of the member, not to the interface of the owner. For instance, B::a::f() is part of the interface of B::a, ''not'' B. It cannot be called from an object of class B. Hence, the following calls are syntactically incorrect:&lt;br /&gt;
&lt;br /&gt;
 b::a::f();&lt;br /&gt;
 c.C::b::a::f();&lt;br /&gt;
&lt;br /&gt;
For the same reason, there is an additional rule: &lt;br /&gt;
&lt;br /&gt;
:The access rights of the overrides should be the same as the access rights of the member for which they are done (overrides for a public member should be public, etc.).&lt;br /&gt;
&lt;br /&gt;
An implementation of overriding in owner class in C++ is described in the “Inside the Overriding Mechanism” section later in this text. It is relatively straightforward: it does not require special data structures, does not interfere with other language features, and does not lead to losses in efficiency or memory. The same mechanism of dynamic binding through virtual method tables is used for overriding in owner class as for the usual overriding.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Next: [[Applications of Overriding in Owner]]'''&lt;/div&gt;</description>
			<pubDate>Fri, 03 Feb 2006 06:08:51 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:Definition_of_Overriding_in_Owner_Class</comments>		</item>
		<item>
			<title>Overriding in Owner Class</title>
			<link>https://www.taitl.com/andrey/Overriding_in_Owner_Class</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''''An extension of polymorphism allowing for better integration of objects for common work.'''''&lt;br /&gt;
&lt;br /&gt;
Andrey Potekhin&lt;br /&gt;
&lt;br /&gt;
Subject areas: Object-oriented design techniques; C++ language design; theoretical foundations.&lt;br /&gt;
&lt;br /&gt;
= Abstract = &lt;br /&gt;
&lt;br /&gt;
Classes in C++ are devoid of an important quality: the class is not aware of operations performed on its member objects by code outside of the class. A member of a class may serve as an argument of some algorithm, it may be a widget exposed to the end user, or can be manipulated by other members of the class. The changes happening to the member may require owner class attention. To enable the owner to see these changes, usually either the code of the member's class is modified to send notifications, or a special wrapper for the member is created. This paper introduces an alternative way of providing control over members: an extension of polymorphism that allows one to override a virtual function of member's class in the owner class. This can help objects to work together; the designs may benefit by minimizing class and run-time dependencies, resolving the update issues and improving reusability of code.&lt;br /&gt;
&lt;br /&gt;
= Introduction = &lt;br /&gt;
&lt;br /&gt;
Consider a spell check algorithm making changes to objects representing text. The class representing a text document passes its member text for correction. Because it must reflect the changes to the text, it must know what the spell checker has done to the text. In general, it is often desirable for a class to control the changes that happen to its member objects.&lt;br /&gt;
&lt;br /&gt;
There are several ways to make owner class aware of changes to a member. You can hide the member and embed its interface in the interface of the owner class. It may be necessary to rewrite the code working with member class to start working with the owner class. Alternatively, you can declare a pointer to the owner object inside the member class, and pass notifications about changes to the owner. This approach ties together the otherwise independent classes of owner and member through a pointer and notifications; forcing the member class to know about the owner class. You can also change the member to belong to a class derived from the original member class and implementing the code necessary for notifying the owner class. This will create a new class, giving it access rights to the member class's internals, coupling it with the owner class, and possibly giving it knowledge of the owner class 's internals. You could also derive the owner class from the member class. However, this would make the owner an instance of the member class (''is-a''), instead of intended usage of the member (''has-a''). This may become complicated if the owner class wants to control more than one member belonging to the same class. In an attempt to find a better solution, I decided to re-formulate the problem:&lt;br /&gt;
&lt;br /&gt;
:'''Given a class and its members, it is desirable that when a call is made to a member function of a member, some function of the owner class is called.'''&lt;br /&gt;
&lt;br /&gt;
Following the analogy with the usual overriding of a method in the derived class, I propose to allow the possibility to override a method in the enclosing class. &lt;br /&gt;
&lt;br /&gt;
This approach will be described in the section called “Definition”; its applications to some well-known programming problems in “Applications”; some analysis will be done in “Comparison with the Alternatives” and “Questions”; an implementation for C++ in “Inside the Overriding Mechanism”. And then I will conclude with some summarizing thoughts. By the way, the use of C++ in this text should not imply that the ideas cannot be used in other object-oriented programming languages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Next: [[Definition of Overriding in Owner Class]]'''&lt;/div&gt;</description>
			<pubDate>Fri, 03 Feb 2006 05:48:26 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:Overriding_in_Owner_Class</comments>		</item>
		<item>
			<title>The P-Bug</title>
			<link>https://www.taitl.com/andrey/The_P-Bug</link>
			<description>&lt;p&gt;Summary: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;From: Potekhin, Andrey&amp;lt;br&amp;gt;&lt;br /&gt;
Tuesday, February 01, 2005 10:00 PM&amp;lt;br&amp;gt;&lt;br /&gt;
To: Dev Team&amp;lt;br&amp;gt;&lt;br /&gt;
Subject: The P-bug&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Dear colleagues,&lt;br /&gt;
&lt;br /&gt;
Once in a while people put their names on things that they discover - Rorschach spots, Alzheimer's disease, Eiffel tower. Let me introduce '''Potekhin's Bug'''.&lt;br /&gt;
&lt;br /&gt;
Let's put this value into database:&lt;br /&gt;
 130 &lt;br /&gt;
&lt;br /&gt;
Let's retrieve it into a char using a usual ADO call:&lt;br /&gt;
 char c = (char)(short)f-&amp;gt;Item[&amp;quot;MyField&amp;quot;]-&amp;gt;Value; &lt;br /&gt;
&lt;br /&gt;
In the above code, we need to cast to &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt; since ADO doesn't know how to cast to &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt; directly. Then, we need to cast to &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt; to avoid compiler's warning of a 'possible loss of data'. Since that we are storing values which are not greater than 256, we are sure that no data is lost.&lt;br /&gt;
&lt;br /&gt;
Predictably, the retrieved value, as shown by debugger, is not '''130''', but '''-126'''. &lt;br /&gt;
&lt;br /&gt;
If you think that this is the bug I'm talking about, it is not. This is just a usual signed/unsigned issue. Since c is declared as a (signed) &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;, the moment the 1 hits its upper bit it becomes a negative value. This is not a bug, this is how it supposed to be. The bits are all the same. It is still '''130''' under the hood. Let's continue. &lt;br /&gt;
&lt;br /&gt;
Retrieve the same value as an integer:&lt;br /&gt;
 int i = (short)f-&amp;gt;Item[&amp;quot;MyField&amp;quot;]-&amp;gt;Value; &lt;br /&gt;
&lt;br /&gt;
As you probably guessed, this time the debugger shows '''130'''. &lt;br /&gt;
&lt;br /&gt;
=== Now, the question: what do you think will be the result of this code: ===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 if (c == i)&lt;br /&gt;
 {&lt;br /&gt;
    AfxMessageBox(&amp;quot;Heaven&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 else&lt;br /&gt;
 {&lt;br /&gt;
    AfxMessageBox(&amp;quot;Hell&amp;quot;);&lt;br /&gt;
 } &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the one hand, this is a case of '''-126''' vs. '''130'''. On the other, we know that: &lt;br /&gt;
&lt;br /&gt;
- '''Both values have the same bits''' - 10000010, which are read from the same database field. &lt;br /&gt;
&lt;br /&gt;
- '''Both values are signed values''', so there is no signed/unsigned mismatch. &lt;br /&gt;
&lt;br /&gt;
- When evaluating the expression, both values are implicitly converted to same type, '''int'''. So, when compared, they '''are of same size'''. In other words, compiler sees it as &amp;lt;code&amp;gt;if ((int)c == i)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
So, which message will you see? I'll have to tell you. Despite these assumptions, you'll be getting the else branch. This is what we call Potekhin's bug.&lt;br /&gt;
&lt;br /&gt;
== Explanation of the trick == &lt;br /&gt;
&lt;br /&gt;
One may say, of course, '''130''' is not '''-126''', that's why they don't match. However, this does not explain how did we end up with such results. Here's the explanation. Compare: &lt;br /&gt;
&lt;br /&gt;
Before conversion to int: &lt;br /&gt;
&lt;br /&gt;
 (int) 130 == 10000010&lt;br /&gt;
 (char) -126 == 10000010 &lt;br /&gt;
&lt;br /&gt;
After conversion to int: &lt;br /&gt;
&lt;br /&gt;
 (int) 130 == 10000010&lt;br /&gt;
 (int) -126 == 11111111111111111111111110000010 &lt;br /&gt;
&lt;br /&gt;
When a char gets converted, its negative bit gets propagated all the way to the left. &lt;br /&gt;
&lt;br /&gt;
Well, I knew it. Kind of. I knew that it gets propagated. The problem is, I didn't realize that it could lead to scenarios like one described above. &lt;br /&gt;
&lt;br /&gt;
Conclusions? Same old rule. Never use a signed char to store anything above 128. Or if you do, don't compare it to an integer :)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''[[Main_Page|To Main Page]]'''&lt;/div&gt;</description>
			<pubDate>Fri, 03 Feb 2006 05:22:39 GMT</pubDate>			<dc:creator>Andrey</dc:creator>			<comments>https://www.taitl.com/andrey/Talk:The_P-Bug</comments>		</item>
	</channel>
</rss>