View Javadoc

1   /*
2    * $Id: CityStroller.java 21939 2011-05-18 13:32:09Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.example.gpswalker;
11  
12  import java.util.concurrent.atomic.AtomicBoolean;
13  
14  /**
15   * Generates a random walk around a city
16   */
17  public class CityStroller
18  {
19  
20      public static final GpsCoord SAN_FRANCISCO = new GpsCoord(37.789167f, -122.419281f);
21      public static final GpsCoord LONDON = new GpsCoord(37.788423f, -122.39823f);
22      public static final GpsCoord VALLETTA = new GpsCoord(35.898504f, 14.514313f);
23  
24  
25      private volatile GpsCoord currentCoord = SAN_FRANCISCO;
26      private AtomicBoolean firstTime = new AtomicBoolean(true);
27  
28      //could use a better algorithm here or real test data for better results
29      public GpsCoord generateNextCoord()
30      {
31          if (firstTime.get())
32          {
33              firstTime.set(false);
34          }
35          else
36          {
37              double dist = Math.random() * 0.002;
38              double angle = Math.random() * Math.PI;
39  
40  
41              float lat = currentCoord.getLatitude() + (float) (dist * Math.sin(angle));
42              float lng = currentCoord.getLongitude() + (float) (dist * Math.cos(angle));
43  
44              currentCoord = new GpsCoord(lat, lng);
45          }
46          return currentCoord;
47      }
48  
49      public GpsCoord getCurrentCoord()
50      {
51          return currentCoord;
52      }
53  
54      public void setCurrentCoord(GpsCoord currentCoord)
55      {
56          this.currentCoord = currentCoord;
57          firstTime.set(false);
58      }
59  }