PageRenderTime 1210ms CodeModel.GetById 233ms app.highlight 162ms RepoModel.GetById 812ms app.codeStats 0ms

/src/com/androzic/util/StringFormatter.java

http://androzic.googlecode.com/
Java | 169 lines | 145 code | 22 blank | 2 comment | 22 complexity | 2ffd6bbbdfc88cc84a0e1ed312d3ed26 MD5 | raw file
  1package com.androzic.util;
  2
  3import java.text.DecimalFormat;
  4import java.text.DecimalFormatSymbols;
  5import java.util.Locale;
  6
  7import com.jhlabs.map.GeodeticPosition;
  8import com.jhlabs.map.ReferenceException;
  9import com.jhlabs.map.UTMReference;
 10
 11public class StringFormatter
 12{
 13	// http://code.google.com/p/android/issues/detail?id=2626
 14	final static DecimalFormat coordDegFormat = new DecimalFormat("#0.000000", new DecimalFormatSymbols(Locale.ENGLISH));
 15	final static DecimalFormat coordIntFormat = new DecimalFormat("00", new DecimalFormatSymbols(Locale.ENGLISH));
 16	final static DecimalFormat coordMinFormat = new DecimalFormat("00.0000", new DecimalFormatSymbols(Locale.ENGLISH));
 17	final static DecimalFormat coordSecFormat = new DecimalFormat("00.000", new DecimalFormatSymbols(Locale.ENGLISH));
 18	
 19	final static DecimalFormat timeFormat = new DecimalFormat("00");
 20
 21	public static double distanceFactor = 1000.0;
 22	public static String distanceAbbr = "km";
 23	public static double distanceShortFactor = 1.0;
 24	public static String distanceShortAbbr = "m";
 25	
 26	public static final String distanceH(final double distance)
 27	{
 28		return distanceH(distance, 2000);
 29	}
 30
 31	public static final String distanceH(double distance, int threshold)
 32	{
 33		String[] dist = distanceC(distance, threshold);
 34		return dist[0] + " " + dist[1];
 35	}
 36
 37	public static final String distanceH(double distance, String format, int threshold)
 38	{
 39		String[] dist = distanceC(distance, format, threshold);
 40		return dist[0] + " " + dist[1];
 41	}
 42
 43	public static final String[] distanceC(final double distance)
 44	{
 45		return distanceC(distance, 2000);
 46	}
 47
 48	public static final String[] distanceC(final double distance, int threshold)
 49	{
 50//		return new String[] {String.valueOf(Math.round(dist)), distunit};
 51		return distanceC(distance, "%.0f", threshold);
 52	}
 53
 54	public static final String[] distanceC(final double distance, final String format, int threshold)
 55	{
 56		double dist = distance * distanceShortFactor;
 57		String distunit = distanceShortAbbr;
 58		if (Math.abs(dist) > threshold)
 59		{
 60			dist = dist / distanceShortFactor / 1000 * distanceFactor;
 61			distunit = distanceAbbr;
 62		}
 63
 64		return new String[] {String.format(format, dist), distunit};
 65	}
 66
 67	public static final String coordinate(int format, double coordinate)
 68	{
 69		switch (format)
 70		{
 71			case 0:
 72			{
 73				return coordDegFormat.format(coordinate);
 74			}
 75			case 1:
 76			{
 77				final double sign = Math.signum(coordinate);
 78				final double coord = Math.abs(coordinate);
 79				final int degrees = (int) Math.floor(coord);
 80				final double minutes = (coord - degrees) * 60;
 81				return coordIntFormat.format(sign*degrees) + "° "
 82						+ coordMinFormat.format(minutes) + "'";
 83			}
 84			case 2:
 85			{
 86				final double sign = Math.signum(coordinate);
 87				final double coord = Math.abs(coordinate);
 88				final int degrees = (int) Math.floor(coord);
 89				final double min = (coord - degrees) * 60;
 90				final int minutes = (int) Math.floor(min);
 91				final double seconds = (min - minutes) * 60;
 92				return coordIntFormat.format(sign*degrees) + "° "
 93						+ coordIntFormat.format(minutes) + "' "
 94						+ coordSecFormat.format(seconds) + "\"";
 95			}
 96		}
 97		return String.valueOf(coordinate);
 98	}
 99
100	public static final String coordinates(int format, String delimeter, double latitude, double longitude)
101	{
102		switch (format)
103		{
104			case 0:
105			case 1:
106			case 2:
107			{
108				return coordinate(format, latitude) + delimeter + coordinate(format, longitude);
109			}
110			case 3:
111			{
112				String coords; 
113				try
114				{
115					coords = UTMReference.toUTMRefString(new GeodeticPosition(latitude, longitude));
116					return coords;
117				}
118				catch (ReferenceException ex)
119				{
120				}
121				
122			}
123		}
124		return String.valueOf(latitude) + delimeter + String.valueOf(longitude);
125	}
126
127	public static String bearingH(double bearing)
128	{
129		return String.format("%.0f", bearing)+"°";
130	}
131
132	public static final String bearingSimpleH(double bearing)
133	{
134		if (bearing <  22 || bearing >= 338) return "N";
135		if (bearing <  67 && bearing >=  22) return "NE";
136		if (bearing < 112 && bearing >=  67) return "E";
137		if (bearing < 158 && bearing >= 112) return "SE";
138		if (bearing < 202 && bearing >= 158) return "S";
139		if (bearing < 248 && bearing >= 202) return "SW";
140		if (bearing < 292 && bearing >= 248) return "W";
141		if (bearing < 338 && bearing >= 292) return "NW";
142		return ".";
143	}
144	
145	public static final String timeH(int minutes)
146	{
147		String[] time = timeC(minutes);
148		return time[0] + " " + time[1];
149	}
150	
151	public static final String[] timeC(int minutes)
152	{
153		int hour = 0;
154		int min = minutes;
155
156		if (min <= 1)
157			return new String[] {"< 1", "min"};
158		
159		if (min > 59)
160		{
161			hour = (int) Math.floor(min / 60);
162			min = min - hour * 60;
163		}
164		if (hour > 23)
165			return new String[] {"> 24", "h"};
166		
167		return new String[] {timeFormat.format(hour)+":"+timeFormat.format(min), "min"};
168	}
169}