You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SizeWithUnit.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import java.io.Serializable;
  18. import java.util.regex.Matcher;
  19. import java.util.regex.Pattern;
  20. import com.vaadin.server.Sizeable.Unit;
  21. import com.vaadin.shared.util.SharedUtil;
  22. /**
  23. * A class for representing a value-unit pair. Also contains utility methods for
  24. * parsing such pairs from a string.
  25. *
  26. * @since 7.4
  27. * @author Vaadin Ltd
  28. */
  29. public class SizeWithUnit implements Serializable {
  30. private float size;
  31. private Unit unit;
  32. private static final Pattern sizePattern = Pattern
  33. .compile(SharedUtil.SIZE_PATTERN);
  34. /**
  35. * Constructs a new SizeWithUnit object representing the pair (size, unit).
  36. *
  37. * @param size
  38. * a numeric value
  39. * @param unit
  40. * a unit
  41. */
  42. public SizeWithUnit(float size, Unit unit) {
  43. this.size = size;
  44. this.unit = unit;
  45. }
  46. /**
  47. * Returns the numeric value stored in this object.
  48. *
  49. * @return the value of this (value, unit) pair
  50. */
  51. public float getSize() {
  52. return size;
  53. }
  54. /**
  55. * Returns the unit stored in this object.
  56. *
  57. * @return the unit of this (value, unit) pair
  58. */
  59. public Unit getUnit() {
  60. return unit;
  61. }
  62. /**
  63. * Returns an object whose numeric value and unit are taken from the string
  64. * s. If s does not specify a unit and defaultUnit is not null, defaultUnit
  65. * is used as the unit. If defaultUnit is null and s is a nonempty string
  66. * representing a unitless number, an exception is thrown. Null or empty
  67. * string will produce {-1,Unit#PIXELS}.
  68. *
  69. * @param s
  70. * the string to be parsed
  71. * @param defaultUnit
  72. * The unit to be used if s does not contain any unit. Use null
  73. * for no default unit.
  74. * @return an object containing the parsed value and unit
  75. */
  76. public static SizeWithUnit parseStringSize(String s, Unit defaultUnit) {
  77. if (s == null) {
  78. return null;
  79. }
  80. s = s.trim();
  81. if ("".equals(s)) {
  82. return null;
  83. }
  84. float size = 0;
  85. Unit unit = null;
  86. Matcher matcher = sizePattern.matcher(s);
  87. if (matcher.find()) {
  88. size = Float.parseFloat(matcher.group(1));
  89. if (size < 0) {
  90. size = -1;
  91. unit = Unit.PIXELS;
  92. } else {
  93. String symbol = matcher.group(2);
  94. if ((symbol != null && symbol.length() > 0)
  95. || defaultUnit == null) {
  96. unit = Unit.getUnitFromSymbol(symbol);
  97. } else {
  98. unit = defaultUnit;
  99. }
  100. }
  101. } else {
  102. throw new IllegalArgumentException("Invalid size argument: \"" + s
  103. + "\" (should match " + sizePattern.pattern() + ")");
  104. }
  105. return new SizeWithUnit(size, unit);
  106. }
  107. /**
  108. * Returns an object whose numeric value and unit are taken from the string
  109. * s. Null or empty string will produce {-1,Unit#PIXELS}. An exception is
  110. * thrown if s specifies a number without a unit.
  111. *
  112. * @param s
  113. * the string to be parsed
  114. * @return an object containing the parsed value and unit
  115. */
  116. public static SizeWithUnit parseStringSize(String s) {
  117. return parseStringSize(s, null);
  118. }
  119. }