// Instance variables
private boolean _valid = false;
+ private boolean _cardinalGuessed = false;
protected int _cardinal = NORTH;
private int _degrees = 0;
private int _minutes = 0;
inString = inString.trim();
strLen = inString.length();
}
- if (strLen > 1)
+ if (strLen > 0)
{
// Check for cardinal character either at beginning or end
boolean hasCardinal = true;
hasCardinal = false;
// use default from concrete subclass
_cardinal = getDefaultCardinal();
+ _cardinalGuessed = true;
+ }
+ else if (isJustNumber(inString)) {
+ // it's just a number
+ hasCardinal = false;
+ _cardinalGuessed = true;
}
// count numeric fields - 1=d, 2=dm, 3=dm.m/dms, 4=dms.s
numFields++;
denoms[numFields-1] = 1;
}
- fields[numFields-1] = fields[numFields-1] * 10 + (currChar - '0');
- denoms[numFields-1] *= 10;
+ if (denoms[numFields-1] < 1E18) // ignore trailing characters if too big for long
+ {
+ fields[numFields-1] = fields[numFields-1] * 10 + (currChar - '0');
+ denoms[numFields-1] *= 10;
+ }
}
else
{
}
// parse fields according to number found
_degrees = (int) fields[0];
+ _asDouble = _degrees;
_originalFormat = hasCardinal?FORMAT_DEG:FORMAT_DEG_WITHOUT_CARDINAL;
_fracDenom = 10;
if (numFields == 2)
return cardinal;
}
+ /**
+ * @return true if cardinal was guessed, false if parsed
+ */
+ public boolean getCardinalGuessed() {
+ return _cardinalGuessed;
+ }
/**
* Get the cardinal from the given character
*/
public boolean equals(Coordinate inOther)
{
- return (inOther != null && _cardinal == inOther._cardinal
- && _degrees == inOther._degrees
- && _minutes == inOther._minutes
- && _seconds == inOther._seconds
- && _fracs == inOther._fracs);
+ return (_asDouble == inOther._asDouble);
}
}
case FORMAT_DEG_WHOLE_MIN:
{
- answer = "" + PRINTABLE_CARDINALS[_cardinal] + threeDigitString(_degrees) + "\u00B0"
- + (int) Math.floor(_minutes + _seconds / 60.0 + _fracs / 60.0 / _fracDenom + 0.5) + "'";
+ int deg = _degrees;
+ int min = (int) Math.floor(_minutes + _seconds / 60.0 + _fracs / 60.0 / _fracDenom + 0.5);
+ if (min == 60) {
+ min = 0; deg++;
+ }
+ answer = "" + PRINTABLE_CARDINALS[_cardinal] + threeDigitString(deg) + "\u00B0" + min + "'";
break;
}
case FORMAT_DEG:
case FORMAT_DECIMAL_FORCE_POINT:
{
// Forcing a decimal point instead of system-dependent commas etc
- answer = EIGHT_DP.format(_asDouble);
+ if (_originalFormat != FORMAT_DEG_WITHOUT_CARDINAL || answer.indexOf('.') < 0) {
+ answer = EIGHT_DP.format(_asDouble);
+ }
break;
}
case FORMAT_DEG_MIN_SEC_WITH_SPACES:
*/
protected abstract Coordinate makeNew(double inValue, int inFormat);
+ /**
+ * Try to parse the given string
+ * @param inString string to check
+ * @return true if it can be parsed as a number
+ */
+ private static boolean isJustNumber(String inString)
+ {
+ boolean justNum = false;
+ try {
+ double x = Double.parseDouble(inString);
+ justNum = (x >= -180.0 && x <= 360.0);
+ }
+ catch (NumberFormatException nfe) {} // flag remains false
+ return justNum;
+ }
/**
* Create a String representation for debug