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.

CondLengthProperty.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 org.apache.fop.datatypes.CompoundDatatype;
  20. import org.apache.fop.datatypes.Length;
  21. import org.apache.fop.datatypes.PercentBaseContext;
  22. import org.apache.fop.fo.Constants;
  23. import org.apache.fop.fo.FObj;
  24. import org.apache.fop.fo.PropertyList;
  25. import org.apache.fop.fo.expr.PropertyException;
  26. import org.apache.fop.util.CompareUtil;
  27. /**
  28. * Superclass for properties that have conditional lengths
  29. */
  30. public class CondLengthProperty extends Property implements CompoundDatatype {
  31. /** cache holding canonical instances (for absolute conditional lengths) */
  32. private static final PropertyCache<CondLengthProperty> CACHE
  33. = new PropertyCache<CondLengthProperty>();
  34. /** components */
  35. private Property length;
  36. private EnumProperty conditionality;
  37. private boolean isCached = false;
  38. private int hash = -1;
  39. /**
  40. * Inner class for creating instances of CondLengthProperty
  41. */
  42. public static class Maker extends CompoundPropertyMaker {
  43. /**
  44. * @param propId the id of the property for which a Maker should be created
  45. */
  46. public Maker(int propId) {
  47. super(propId);
  48. }
  49. /**
  50. * Create a new empty instance of CondLengthProperty.
  51. * @return the new instance.
  52. */
  53. public Property makeNewProperty() {
  54. return new CondLengthProperty();
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public Property convertProperty(Property p, PropertyList propertyList, FObj fo)
  60. throws PropertyException {
  61. if (p instanceof KeepProperty) {
  62. return p;
  63. }
  64. return super.convertProperty(p, propertyList, fo);
  65. }
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public void setComponent(int cmpId, Property cmpnValue,
  71. boolean bIsDefault) {
  72. if (isCached) {
  73. throw new IllegalStateException(
  74. "CondLengthProperty.setComponent() called on a cached value!");
  75. }
  76. if (cmpId == CP_LENGTH) {
  77. length = cmpnValue;
  78. } else if (cmpId == CP_CONDITIONALITY) {
  79. conditionality = (EnumProperty)cmpnValue;
  80. }
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. public Property getComponent(int cmpId) {
  86. if (cmpId == CP_LENGTH) {
  87. return length;
  88. } else if (cmpId == CP_CONDITIONALITY) {
  89. return conditionality;
  90. } else {
  91. return null;
  92. }
  93. }
  94. /**
  95. * Returns the conditionality.
  96. * @return the conditionality
  97. */
  98. public Property getConditionality() {
  99. return this.conditionality;
  100. }
  101. /**
  102. * Returns the length.
  103. * @return the length
  104. */
  105. public Property getLengthComponent() {
  106. return this.length;
  107. }
  108. /**
  109. * Indicates if the length can be discarded on certain conditions.
  110. * @return true if the length can be discarded.
  111. */
  112. public boolean isDiscard() {
  113. return this.conditionality.getEnum() == Constants.EN_DISCARD;
  114. }
  115. /**
  116. * Returns the computed length value.
  117. * @return the length in millipoints
  118. */
  119. public int getLengthValue() {
  120. return this.length.getLength().getValue();
  121. }
  122. /**
  123. * Returns the computed length value.
  124. * @param context The context for the length calculation (for percentage based lengths)
  125. * @return the length in millipoints
  126. */
  127. public int getLengthValue(PercentBaseContext context) {
  128. return this.length.getLength().getValue(context);
  129. }
  130. /** {@inheritDoc} */
  131. public String toString() {
  132. return "CondLength[" + length.getObject().toString()
  133. + ", " + (isDiscard()
  134. ? conditionality.toString().toLowerCase()
  135. : conditionality.toString()) + "]";
  136. }
  137. /**
  138. * @return this.condLength
  139. */
  140. public CondLengthProperty getCondLength() {
  141. if (this.length.getLength().isAbsolute()) {
  142. CondLengthProperty clp = CACHE.fetch(this);
  143. if (clp == this) {
  144. isCached = true;
  145. }
  146. return clp;
  147. } else {
  148. return this;
  149. }
  150. }
  151. /**
  152. * TODO: Should we allow this?
  153. * @return this.condLength cast as a Length
  154. */
  155. public Length getLength() {
  156. return length.getLength();
  157. }
  158. /**
  159. * @return this.condLength cast as an Object
  160. */
  161. public Object getObject() {
  162. return this;
  163. }
  164. /** {@inheritDoc} */
  165. public boolean equals(Object obj) {
  166. if (this == obj) {
  167. return true;
  168. }
  169. if (obj instanceof CondLengthProperty) {
  170. CondLengthProperty clp = (CondLengthProperty)obj;
  171. return (CompareUtil.equal(this.length, clp.length)
  172. && CompareUtil.equal(this.conditionality, clp.conditionality));
  173. }
  174. return false;
  175. }
  176. /** {@inheritDoc} */
  177. public int hashCode() {
  178. if (this.hash == -1) {
  179. int hash = 17;
  180. hash = 37 * hash + (length == null ? 0 : length.hashCode());
  181. hash = 37 * hash + (conditionality == null ? 0 : conditionality.hashCode());
  182. this.hash = hash;
  183. }
  184. return this.hash;
  185. }
  186. }