org.bolson.vote
Class NameVotingSystem

java.lang.Object
  extended by org.bolson.vote.NameVotingSystem
Direct Known Subclasses:
NamedApproval, NamedHistogram, NamedIRNR, NamedIRV, NamedRaw, NamedSTV, NamedTemplate, NamedVRR

public abstract class NameVotingSystem
extends java.lang.Object

Taking a bunch of (name,rating) pairs this type of voting system allows for write-ins and perhaps a somewhat simpler usage.


Nested Class Summary
static class NameVotingSystem.NameVote
           
static class NameVotingSystem.ReverseComparator
           
 
Constructor Summary
NameVotingSystem()
           
 
Method Summary
 void defaultMain(java.lang.String[] argv)
          Use this for making simple subclass main() to run a vote of that type.
static int denibble(char c)
           
static java.lang.String depercentHexify(java.lang.String in)
          un-escape ASCII string with %xx escapes in it to, converting such sequences to bytes.
static NameVotingSystem.NameVote[] fromUrlEncoded(java.lang.String s)
          Parse a name=value&name=value url query type string into a NameVote[].
 java.lang.String getDebug()
          If debug, return debugLog.toString(), else null.
static java.util.Iterator getImplNames()
           
static java.lang.Class getIpmlForName(java.lang.String name)
           
abstract  NameVotingSystem.NameVote[] getWinners()
           
 java.lang.String htmlExplain()
          Get HTML explaination of how the election worked.
 java.lang.StringBuffer htmlExplain(java.lang.StringBuffer sb)
          Get HTML explaination of how the election worked.
 java.lang.String htmlSummary()
          Get HTML summary of voting system state.
abstract  java.lang.StringBuffer htmlSummary(java.lang.StringBuffer sb)
          Get HTML summary of voting system state.
 int init(java.lang.String[] argv)
          Set options for voting system.
abstract  java.lang.String name()
           
static NameVotingSystem.NameVote[] nameEqStrToVoteArray(java.lang.String cd)
          Convert Name=Rating pairs in a string to an array of NameVote.
static NameVotingSystem.NameVote[] nameEqStrToVoteArray(java.lang.String cd, java.lang.String splitPattern, boolean trimWhitespace)
          Convert Name=Rating pairs in a string to an array of NameVote.
static java.lang.String percentHexify(java.lang.String s)
          It's just like return s.replaceAll( "%", "%25" ).replaceAll( "&", "%26" ).replaceAll( "=", "%3d" ); but faster.
 void readVotes(java.io.Reader r)
          Reads tab separated name=rating vote per line.
protected static void registerImpl(java.lang.String name, java.lang.Class c)
           
static void resultsHTMLDisplay(java.io.PrintWriter out, NameVotingSystem[] vs, boolean debug, int numSeats)
          Print full results.
static void sort(NameVotingSystem.NameVote[] they)
           
static java.lang.String urlEncode(NameVotingSystem.NameVote[] they)
          url encode a vote
 void voteNameEqStr(java.lang.String cd)
          Vote a tab-separated name=rating string.
 void voteOrderSpec(java.lang.String vote)
          Vote an ordering specified by a string.
 void voteRating(java.util.Map vote)
          Vote a set of ratings.
abstract  void voteRating(NameVotingSystem.NameVote[] vote)
          Vote a set of ratings.
static NameVotingSystem.NameVote[] voteSpecToNameVoteArray(java.lang.String cd)
          Convert a list of names separated by '>' or '='.
static void winnerSummaryHTMLTable(java.io.PrintWriter out, NameVotingSystem[] vs, boolean debug, int numSeats)
          Print summary table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NameVotingSystem

public NameVotingSystem()
Method Detail

getDebug

public java.lang.String getDebug()
If debug, return debugLog.toString(), else null.


init

public int init(java.lang.String[] argv)
Set options for voting system. Default implementation sets debug to true if it sees "debug". Skip pass null entries, which may have been yanked by a subclass. Unless there's an error, last line should be "return super.init( argv );" Multi-seat capable implementations should accept an option pair ("seats", <int>) to set the number of seats.

Parameters:
argv - array of options, just like main()
Returns:
0 on success

voteOrderSpec

public void voteOrderSpec(java.lang.String vote)
Vote an ordering specified by a string. Names are separated by '>' or '='. Name at the left of the list are preferred to names further right in the less when separated by '>' and considered equivalend when separated by '='. White space surrounding the separator is discarded. White space internal to a name is preserved. "Ralph Nader > Al Gore > George Bush" might translate to { ("Ralph Nader",2.0), ("Al Gore",1.0), ("George Bush",0.0) } and then be passed to VoteRating(NameVote[])

Parameters:
vote - A String of names separated by '>' or '='.
See Also:
voteRating(NameVote[])

voteRating

public void voteRating(java.util.Map vote)
Vote a set of ratings. Keys of the map should be strings (choice names) and values should be of a numeric type. The default implementation unpacks the map into a NameVote array and calls voteRating( NameVote[] )

Parameters:
vote - a set of (name,rating) pairs
See Also:
voteRating(NameVote[])

voteRating

public abstract void voteRating(NameVotingSystem.NameVote[] vote)
Vote a set of ratings. Keys of the map should be strings (choice names) and values should be of a numeric type. This is slightly faster than the version that takes a map because no conversion is done.

Parameters:
vote - a set of (name,rating) pairs
See Also:
voteRating(Map)

getWinners

public abstract NameVotingSystem.NameVote[] getWinners()
Returns:
A sorted array of (name,rating) pairs. Ties can be determined by rating.

htmlSummary

public abstract java.lang.StringBuffer htmlSummary(java.lang.StringBuffer sb)
Get HTML summary of voting system state.

Parameters:
sb - a valid StringBuffer to which HTML summary will be appended.
Returns:
same StringBuffer passed in, with HTML summary of voting system state appended.

name

public abstract java.lang.String name()
Returns:
name of this voting system (may be modified by settings from init)

htmlSummary

public java.lang.String htmlSummary()
Get HTML summary of voting system state. Returns toString() of a new StringBuffer passed to htmlSummary(StringBuffer)

Returns:
HTML summary of voting system state.
See Also:
htmlSummary(StringBuffer)

htmlExplain

public java.lang.StringBuffer htmlExplain(java.lang.StringBuffer sb)
Get HTML explaination of how the election worked. Typically show intermediate rounds or other counting state progress. Default implementation calls htmlSummary(StringBuffer). Can be optionally overrided for extra educational benefit. It may be really slow. It may be a complete re-implementation of the election method that emits stuff as it goes.

Parameters:
sb - a valid StringBuffer to which HTML explaination will be appended.
Returns:
same StringBuffer passed in, with HTML explaination of voting system process appended.

htmlExplain

public java.lang.String htmlExplain()
Get HTML explaination of how the election worked. Typically show intermediate rounds or other counting state progress. Default implementation calls htmlExplain(StringBuffer).

Returns:
HTML explaination of voting system decision process.
See Also:
htmlExplain(StringBuffer)

voteSpecToNameVoteArray

public static NameVotingSystem.NameVote[] voteSpecToNameVoteArray(java.lang.String cd)
Convert a list of names separated by '>' or '='. e.g. "Name One>Name Two=Name Three>Name Four"

Parameters:
cd - names
Returns:
equivalent NameVote array or null on error

nameEqStrToVoteArray

public static NameVotingSystem.NameVote[] nameEqStrToVoteArray(java.lang.String cd,
                                                               java.lang.String splitPattern,
                                                               boolean trimWhitespace)
Convert Name=Rating pairs in a string to an array of NameVote.

Parameters:
cd - String like "Name One=9 Name Two=3 Name Three=-9"
splitPattern - pattern to break cd on
trimWhitespace - remove excess whitespace at start and end of names
Returns:
NameVote array from cd

nameEqStrToVoteArray

public static NameVotingSystem.NameVote[] nameEqStrToVoteArray(java.lang.String cd)
Convert Name=Rating pairs in a string to an array of NameVote. Calls nameEqStrToVoteArray( cd, "\t", true ), splitting on tab and triming other whitespace.

Parameters:
cd - String like "Name One=9 Name Two=3 Name Three=-9"
Returns:
NameVote array from cd

voteNameEqStr

public void voteNameEqStr(java.lang.String cd)
Vote a tab-separated name=rating string.


sort

public static void sort(NameVotingSystem.NameVote[] they)

readVotes

public void readVotes(java.io.Reader r)
               throws java.io.IOException
Reads tab separated name=rating vote per line.

Throws:
java.io.IOException

defaultMain

public void defaultMain(java.lang.String[] argv)
Use this for making simple subclass main() to run a vote of that type. (new Sub()).defaultMain( argv[] );


percentHexify

public static java.lang.String percentHexify(java.lang.String s)
It's just like return s.replaceAll( "%", "%25" ).replaceAll( "&", "%26" ).replaceAll( "=", "%3d" ); but faster.

Parameters:
s - string to %hex escape
Returns:
string with no '&' or '=' chars, them having been %hex escaped

denibble

public static int denibble(char c)
                    throws java.lang.NumberFormatException
Throws:
java.lang.NumberFormatException

depercentHexify

public static java.lang.String depercentHexify(java.lang.String in)
un-escape ASCII string with %xx escapes in it to, converting such sequences to bytes.

Parameters:
in - string which may have %xx escapes in it
Returns:
unescaped string

fromUrlEncoded

public static NameVotingSystem.NameVote[] fromUrlEncoded(java.lang.String s)
Parse a name=value&name=value url query type string into a NameVote[].

Parameters:
s - the url query type string with candidate names and ratings
Returns:
an array of NameVote with candidate names and ratings

urlEncode

public static java.lang.String urlEncode(NameVotingSystem.NameVote[] they)
url encode a vote

Parameters:
they - vote to encode
Returns:
"name=rating&name=rating"...

winnerSummaryHTMLTable

public static void winnerSummaryHTMLTable(java.io.PrintWriter out,
                                          NameVotingSystem[] vs,
                                          boolean debug,
                                          int numSeats)
Print summary table. prints a table just containing each VotingSystem name and its winner.

Parameters:
out - receives the html table
vs - systems that have been voted and can getWinners
debug - sets if getWinners is to collect debug info
numSeats - number of seats available in this election

resultsHTMLDisplay

public static void resultsHTMLDisplay(java.io.PrintWriter out,
                                      NameVotingSystem[] vs,
                                      boolean debug,
                                      int numSeats)
Print full results. Prints each VotingSystem's html summary.

Parameters:
out - receives the html results
vs - systems that have been voted and can getWinners
debug - sets if getWinners is to collect debug info
numSeats - number of seats available in this election

registerImpl

protected static void registerImpl(java.lang.String name,
                                   java.lang.Class c)

getIpmlForName

public static java.lang.Class getIpmlForName(java.lang.String name)

getImplNames

public static java.util.Iterator getImplNames()