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.

NumberProperty.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fo.properties;
  19. import java.awt.Color;
  20. import org.apache.fop.apps.FOUserAgent;
  21. import org.apache.fop.datatypes.Length;
  22. import org.apache.fop.datatypes.Numeric;
  23. import org.apache.fop.datatypes.PercentBaseContext;
  24. import org.apache.fop.fo.FObj;
  25. import org.apache.fop.fo.PropertyList;
  26. import org.apache.fop.fo.expr.PropertyException;
  27. /**
  28. * Class for handling numeric properties
  29. */
  30. public class NumberProperty extends Property implements Numeric {
  31. /**
  32. * Inner class for making NumberProperty objects
  33. */
  34. public static class Maker extends PropertyMaker {
  35. /**
  36. * Constructor for NumberProperty.Maker
  37. * @param propId the id of the property for which a Maker should be created
  38. */
  39. public Maker(int propId) {
  40. super(propId);
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public Property convertProperty(Property p,
  46. PropertyList propertyList, FObj fo)
  47. throws PropertyException {
  48. if (p instanceof NumberProperty) {
  49. return p;
  50. }
  51. if (p instanceof EnumProperty) {
  52. return EnumNumber.getInstance(p);
  53. }
  54. Number val = p.getNumber();
  55. if (val != null) {
  56. return new NumberProperty(val);
  57. }
  58. return convertPropertyDatatype(p, propertyList, fo);
  59. }
  60. }
  61. /** cache holding all canonical NumberProperty instances */
  62. private static final PropertyCache cache = new PropertyCache();
  63. private final Number number;
  64. /**
  65. * Constructor for Number input
  66. * @param num Number object value for property
  67. */
  68. protected NumberProperty(Number num) {
  69. this.number = num;
  70. }
  71. /**
  72. * Constructor for double input
  73. * @param num double numeric value for property
  74. */
  75. protected NumberProperty(double num) {
  76. this.number = new Double(num);
  77. }
  78. /**
  79. * Constructor for integer input
  80. * @param num integer numeric value for property
  81. */
  82. protected NumberProperty(int num) {
  83. this.number = new Integer(num);
  84. }
  85. /**
  86. * Returns the canonical NumberProperty instance
  87. * corresponding to the given Number
  88. * @param num the base Number
  89. * @return the canonical NumberProperty
  90. */
  91. public static NumberProperty getInstance(Number num) {
  92. return (NumberProperty)cache.fetch(
  93. new NumberProperty(num));
  94. }
  95. /**
  96. * Returns the canonical NumberProperty instance
  97. * corresponding to the given double
  98. * @param num the base double value
  99. * @return the canonical NumberProperty
  100. */
  101. public static NumberProperty getInstance(double num) {
  102. return (NumberProperty)cache.fetch(
  103. new NumberProperty(num));
  104. }
  105. /**
  106. * Returns the canonical NumberProperty instance
  107. * corresponding to the given int
  108. * @param num the base int value
  109. * @return the canonical NumberProperty
  110. */
  111. public static NumberProperty getInstance(int num) {
  112. return (NumberProperty)cache.fetch(
  113. new NumberProperty(num));
  114. }
  115. /**
  116. * Plain number always has a dimension of 0.
  117. * @return a dimension of 0.
  118. */
  119. public int getDimension() {
  120. return 0;
  121. }
  122. /**
  123. * Return the value of this Numeric.
  124. * @return The value as a double.
  125. */
  126. public double getNumericValue() {
  127. return number.doubleValue();
  128. }
  129. /**
  130. * Return the value of this Numeric.
  131. * @param context Evaluation context
  132. * @return The value as a double.
  133. */
  134. public double getNumericValue(PercentBaseContext context) {
  135. return getNumericValue();
  136. }
  137. /** {@inheritDoc} */
  138. public int getValue() {
  139. return number.intValue();
  140. }
  141. /**
  142. * Return the value
  143. * @param context Evaluation context
  144. * @return The value as an int.
  145. */
  146. public int getValue(PercentBaseContext context) {
  147. return getValue();
  148. }
  149. /**
  150. * Return true because all numbers are absolute.
  151. * @return true.
  152. */
  153. public boolean isAbsolute() {
  154. return true;
  155. }
  156. /**
  157. * @return this.number cast as a Number
  158. */
  159. public Number getNumber() {
  160. return this.number;
  161. }
  162. /**
  163. * @return this.number cast as an Object
  164. */
  165. public Object getObject() {
  166. return this.number;
  167. }
  168. /**
  169. * Convert NumberProperty to Numeric object
  170. * @return Numeric object corresponding to this
  171. */
  172. public Numeric getNumeric() {
  173. return this;
  174. }
  175. /** {@inheritDoc} */
  176. public Length getLength() {
  177. //Assume pixels (like in HTML) when there's no unit
  178. return FixedLength.getInstance(getNumericValue(), "px");
  179. }
  180. /**
  181. * Convert NumberProperty to a Color. Not sure why this is needed.
  182. * @param foUserAgent FOUserAgent
  183. * @return Color that corresponds to black
  184. */
  185. public Color getColor(FOUserAgent foUserAgent) {
  186. // TODO: Implement somehow
  187. // Convert numeric value to color ???
  188. // Convert to hexadecimal and then try to make it into a color?
  189. return Color.black;
  190. }
  191. /** {@inheritDoc} */
  192. public int hashCode() {
  193. return number.hashCode();
  194. }
  195. /** {@inheritDoc} */
  196. public boolean equals(Object o) {
  197. if (o != null && o instanceof NumberProperty) {
  198. NumberProperty np = (NumberProperty) o;
  199. return (np.number == this.number
  200. || (this.number != null
  201. && this.number.equals(np.number)));
  202. } else {
  203. return false;
  204. }
  205. }
  206. }