001 package com.croftsoft.apps.exemplar;
002
003 import com.croftsoft.core.lang.*;
004 import com.croftsoft.core.lang.lifecycle.Startable;
005 import com.croftsoft.core.lang.lifecycle.Updatable;
006 import com.croftsoft.core.math.MathConstants;
007 import com.croftsoft.core.util.mail.Mail;
008
009 /***********************************************************************
010 * Model.
011 *
012 * Maintains program state.
013 *
014 * @version
015 * $Id: ExemplarModelImp.java,v 1.5 2008/02/15 22:38:03 croft Exp $
016 * @since
017 * 2006-01-03
018 * @author
019 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
020 ***********************************************************************/
021
022 public final class ExemplarModelImp
023 implements ExemplarModel, Startable, Updatable
024 ////////////////////////////////////////////////////////////////////////
025 ////////////////////////////////////////////////////////////////////////
026 {
027
028 // private final instance variables
029
030 private final Mail<ExemplarMessage> mail;
031
032 // model state instance variables
033
034 private long clickCount;
035
036 private double phase;
037
038 private long lastUpdateTime;
039
040 ////////////////////////////////////////////////////////////////////////
041 ////////////////////////////////////////////////////////////////////////
042
043 public ExemplarModelImp (
044 final ExemplarConfig exemplarConfig,
045 final Mail<ExemplarMessage> mail )
046 ////////////////////////////////////////////////////////////////////////
047 {
048 NullArgumentException.checkArgs (
049 exemplarConfig,
050 this.mail = mail );
051
052 // clickCount = exemplarConfig.getClickCountInit ( );
053 }
054
055 ////////////////////////////////////////////////////////////////////////
056 // interface Accessor methods
057 ////////////////////////////////////////////////////////////////////////
058
059 public long getClickCount ( ) { return clickCount; }
060
061 public double getPhase ( ) { return phase; }
062
063 ////////////////////////////////////////////////////////////////////////
064 // lifecycle methods
065 ////////////////////////////////////////////////////////////////////////
066
067 public void start ( )
068 ////////////////////////////////////////////////////////////////////////
069 {
070 lastUpdateTime = System.nanoTime ( );
071 }
072
073 public void update ( )
074 ////////////////////////////////////////////////////////////////////////
075 {
076 final int size = mail.size ( );
077
078 for ( int i = 0; i < size; i++ )
079 {
080 final ExemplarMessage exemplarMessage = mail.get ( i );
081
082 final ExemplarMessage.Type type = exemplarMessage.getType ( );
083
084 switch ( type )
085 {
086 case INCREMENT_CLICK_COUNT:
087
088 clickCount++;
089
090 mail.offer ( ExemplarMessage.CLICK_COUNT_CHANGED_INSTANCE );
091
092 break;
093
094 default:
095
096 // ignore
097 }
098 }
099
100 final long currentTime = System.nanoTime ( );
101
102 final long deltaTimeNanos = currentTime - lastUpdateTime;
103
104 lastUpdateTime = currentTime;
105
106 phase
107 += MathConstants.TWO_PI
108 * 0.1 // frequency
109 * deltaTimeNanos * MathConstants.SECONDS_PER_NANOSECOND;
110
111 if ( phase >= MathConstants.TWO_PI )
112 {
113 phase -= MathConstants.TWO_PI;
114 }
115 }
116
117 ////////////////////////////////////////////////////////////////////////
118 ////////////////////////////////////////////////////////////////////////
119 }