Merge "add android.util.SizeF.parseSizeF()" into lmp-dev

This commit is contained in:
Lajos Molnar
2014-09-11 00:55:04 +00:00
committed by Android (Google) Code Review
2 changed files with 57 additions and 0 deletions

View File

@@ -32132,6 +32132,7 @@ package android.util {
ctor public SizeF(float, float);
method public float getHeight();
method public float getWidth();
method public static android.util.SizeF parseSizeF(java.lang.String) throws java.lang.NumberFormatException;
}
public class SparseArray implements java.lang.Cloneable {

View File

@@ -16,6 +16,7 @@
package android.util;
import static com.android.internal.util.Preconditions.checkNotNull;
import static com.android.internal.util.Preconditions.checkArgumentFinite;
/**
@@ -95,6 +96,61 @@ public final class SizeF {
return mWidth + "x" + mHeight;
}
private static NumberFormatException invalidSizeF(String s) {
throw new NumberFormatException("Invalid SizeF: \"" + s + "\"");
}
/**
* Parses the specified string as a size value.
* <p>
* The ASCII characters {@code \}{@code u002a} ('*') and
* {@code \}{@code u0078} ('x') are recognized as separators between
* the width and height.</p>
* <p>
* For any {@code SizeF s}: {@code SizeF.parseSizeF(s.toString()).equals(s)}.
* However, the method also handles sizes expressed in the
* following forms:</p>
* <p>
* "<i>width</i>{@code x}<i>height</i>" or
* "<i>width</i>{@code *}<i>height</i>" {@code => new SizeF(width, height)},
* where <i>width</i> and <i>height</i> are string floats potentially
* containing a sign, such as "-10.3", "+7" or "5.2", but not containing
* an {@code 'x'} (such as a float in hexadecimal string format).</p>
*
* <pre>{@code
* SizeF.parseSizeF("3.2*+6").equals(new SizeF(3.2f, 6.0f)) == true
* SizeF.parseSizeF("-3x-6").equals(new SizeF(-3.0f, -6.0f)) == true
* SizeF.parseSizeF("4 by 3") => throws NumberFormatException
* }</pre>
*
* @param string the string representation of a size value.
* @return the size value represented by {@code string}.
*
* @throws NumberFormatException if {@code string} cannot be parsed
* as a size value.
* @throws NullPointerException if {@code string} was {@code null}
*/
public static SizeF parseSizeF(String string)
throws NumberFormatException {
checkNotNull(string, "string must not be null");
int sep_ix = string.indexOf('*');
if (sep_ix < 0) {
sep_ix = string.indexOf('x');
}
if (sep_ix < 0) {
throw invalidSizeF(string);
}
try {
return new SizeF(Float.parseFloat(string.substring(0, sep_ix)),
Float.parseFloat(string.substring(sep_ix + 1)));
} catch (NumberFormatException e) {
throw invalidSizeF(string);
} catch (IllegalArgumentException e) {
throw invalidSizeF(string);
}
}
/**
* {@inheritDoc}
*/