View Javadoc
1   package au.gov.amsa.geo;
2   
3   import java.io.File;
4   import java.util.List;
5   import java.util.regex.Pattern;
6   
7   import org.apache.log4j.Logger;
8   
9   import rx.Observable;
10  import rx.functions.Func1;
11  import rx.functions.Func2;
12  import au.gov.amsa.risky.format.Fix;
13  import au.gov.amsa.risky.format.HasPosition;
14  import au.gov.amsa.util.Files;
15  import au.gov.amsa.util.navigation.Position;
16  
17  public class Util {
18  
19      private static Logger log = Logger.getLogger(Util.class);
20  
21      public static String memoryUsage() {
22          Runtime r = Runtime.getRuntime();
23          long used = r.totalMemory() - r.freeMemory();
24          return "usedHeap=" + (used / 1000000) + "MB, percent=" + (100.0 * used / r.maxMemory());
25      }
26  
27      public static void logMemoryUsage() {
28          log.info(memoryUsage());
29      }
30  
31      public static Position toPos(HasPosition a) {
32          return new Position(a.lat(), a.lon());
33      }
34  
35      public static Observable<File> getFiles(String directory, final String pattern) {
36          return Observable.from(Files.find(new File(directory), Pattern.compile(pattern)));
37      }
38  
39      public static final Func2<Fix, Fix, Integer> COMPARE_FIXES_BY_POSITION_TIME = new Func2<Fix, Fix, Integer>() {
40          @Override
41          public Integer call(Fix a, Fix b) {
42              return Long.compare(a.time(), b.time());
43          }
44      };
45  
46      public static final Func1<List<Fix>, Observable<Fix>> TO_OBSERVABLE = new Func1<List<Fix>, Observable<Fix>>() {
47          @Override
48          public Observable<Fix> call(List<Fix> list) {
49              return Observable.from(list);
50          }
51      };
52  
53  }