Ejemplo n.º 1
0
        public void DepartFrom(ILocation location)
        {
            if (null == location)
            {
                throw new ArgumentNullException("location");
            }

            VoyageState stateBeforeTransition;
            VoyageState previousState = CurrentState;

            do
            {
                stateBeforeTransition = previousState;
                VoyageState newValue = stateBeforeTransition.DepartFrom(location);
                previousState = Interlocked.CompareExchange <VoyageState>(ref this.CurrentState, newValue, stateBeforeTransition);
            }while (previousState != stateBeforeTransition);

            if (!previousState.Equals(this.CurrentState))
            {
                VoyageEventArgs args = new VoyageEventArgs(CurrentState.LastKnownLocation, CurrentState.NextExpectedLocation);

                EventHandler <VoyageEventArgs> handler = Departed;
                if (null != handler)
                {
                    handler(this, args);
                }
            }
        }
Ejemplo n.º 2
0
        public void StopOverAt(ILocation location)
        {
            if (null == location)
            {
                throw new ArgumentNullException("location");
            }

            // Thread safe, lock free sincronization
            VoyageState stateBeforeTransition;
            VoyageState previousState = CurrentState;

            do
            {
                stateBeforeTransition = previousState;
                VoyageState newValue = stateBeforeTransition.StopOverAt(location);
                previousState = Interlocked.CompareExchange <VoyageState>(ref this.CurrentState, newValue, stateBeforeTransition);
            }while (previousState != stateBeforeTransition);

            if (!previousState.Equals(this.CurrentState))
            {
                VoyageEventArgs args = new VoyageEventArgs(previousState.LastKnownLocation, previousState.NextExpectedLocation);

                EventHandler <VoyageEventArgs> handler = Stopped;
                if (null != handler)
                {
                    handler(this, args);
                }
            }
        }