diff options
| -rw-r--r-- | api/current.txt | 1 | ||||
| -rw-r--r-- | core/java/android/util/SizeF.java | 56 |
2 files changed, 57 insertions, 0 deletions
diff --git a/api/current.txt b/api/current.txt index 7bb4e0ef5362..bb8bc16035b6 100644 --- a/api/current.txt +++ b/api/current.txt @@ -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 { diff --git a/core/java/android/util/SizeF.java b/core/java/android/util/SizeF.java index ac4f18765a59..2edc4a7ff588 100644 --- a/core/java/android/util/SizeF.java +++ b/core/java/android/util/SizeF.java @@ -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} */ |