001package votorola.a.count; // Copyright 2013, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import votorola.a.*;
004import votorola.g.*;
005import votorola.g.lang.*;
006
007
008/** A count source that caches a single count.
009  */
010public final class CountSource1 implements CountSource
011{
012
013
014    /** Constructs a CountSource1.
015      *
016      *     @see #vsRun()
017      */
018    public CountSource1( VoteServer.Run _vsRun ) { vsRun = _vsRun; }
019
020
021
022   // ------------------------------------------------------------------------------------
023
024
025    /** The ultimate source of the count.
026      */
027    public final VoteServer.Run vsRun() { return vsRun; }
028
029
030        private final VoteServer.Run vsRun;
031
032
033
034   // - C o u n t - S o u r c e ----------------------------------------------------------
035
036
037    /** @throws IllegalArgumentException if a count was previously requested by this
038      *   method under a different poll name.
039      */
040    public Count count( final String pollName )
041    {
042        if( pollNameRequested == null )
043        {
044            try{ count = vsRun.scopePoll().ensurePoll(pollName).countToReportT(); }
045            catch( java.io.IOException|javax.script.ScriptException|java.sql.SQLException x )
046            {
047                throw new VotorolaRuntimeException( x );
048            }
049
050            pollNameRequested = pollName; // fetch attempted once only
051        }
052        if( count == null || pollName.equals(count.pollName()) ) return count;
053
054        throw new IllegalArgumentException( pollName + " requested but count already cached for "
055          + count.pollName() );
056    }
057
058
059
060//// P r i v a t e ///////////////////////////////////////////////////////////////////////
061
062
063    private Count count; // lazily fetched to save the effort
064
065
066
067    private String pollNameRequested;
068
069
070}