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.

PercentLength.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.PercentBase;
  20. import org.apache.fop.datatypes.PercentBaseContext;
  21. import org.apache.fop.fo.expr.PropertyException;
  22. import org.apache.fop.util.CompareUtil;
  23. /**
  24. * a percent specified length quantity in XSL
  25. */
  26. public class PercentLength extends LengthProperty {
  27. /**
  28. * The percentage itself, expressed as a decimal value, e.g. for 95%, set
  29. * the value to .95
  30. */
  31. private double factor;
  32. /**
  33. * A PercentBase implementation that contains the base length to which the
  34. * {@link #factor} should be applied to compute the actual length
  35. */
  36. private PercentBase lbase;
  37. /**
  38. * Main constructor. Construct an object based on a factor (the percent,
  39. * as a factor) and an object which has a method to return the Length which
  40. * provides the "base" for the actual length that is modeled.
  41. * @param factor the percentage factor, expressed as a decimal (e.g. use
  42. * .95 to represent 95%)
  43. * @param lbase base property to which the factor should be applied
  44. */
  45. public PercentLength(double factor, PercentBase lbase) {
  46. this.factor = factor;
  47. this.lbase = lbase;
  48. }
  49. /**
  50. * @return the base
  51. */
  52. public PercentBase getBaseLength() {
  53. return this.lbase;
  54. }
  55. /**
  56. * Used during property resolution to check for
  57. * negative percentages
  58. *
  59. * @return the percentage value
  60. */
  61. protected double getPercentage() {
  62. return factor * 100;
  63. }
  64. /**
  65. * Return false because percent-length are always relative.
  66. * {@inheritDoc}
  67. */
  68. public boolean isAbsolute() {
  69. return false;
  70. }
  71. /** {@inheritDoc} */
  72. public double getNumericValue() {
  73. return getNumericValue(null);
  74. }
  75. /** {@inheritDoc} */
  76. public double getNumericValue(PercentBaseContext context) {
  77. try {
  78. return factor * lbase.getBaseLength(context);
  79. } catch (PropertyException exc) {
  80. log.error(exc);
  81. return 0;
  82. }
  83. }
  84. /** {@inheritDoc} */
  85. public String getString() {
  86. return (factor * 100.0) + "%";
  87. }
  88. /**
  89. * Return the length of this PercentLength.
  90. * {@inheritDoc}
  91. */
  92. public int getValue() {
  93. return (int) getNumericValue();
  94. }
  95. /** {@inheritDoc} */
  96. public int getValue(PercentBaseContext context) {
  97. return (int) getNumericValue(context);
  98. }
  99. /**
  100. * @return the String equivalent of this
  101. */
  102. public String toString() {
  103. StringBuffer sb = new StringBuffer(PercentLength.class.getName())
  104. .append("[factor=").append(factor)
  105. .append(",lbase=").append(lbase).append("]");
  106. return sb.toString();
  107. }
  108. @Override
  109. public int hashCode() {
  110. final int prime = 31;
  111. int result = 1;
  112. result = prime * result + CompareUtil.getHashCode(factor);
  113. result = prime * result + CompareUtil.getHashCode(lbase);
  114. return result;
  115. }
  116. @Override
  117. public boolean equals(Object obj) {
  118. if (this == obj) {
  119. return true;
  120. }
  121. if (!(obj instanceof PercentLength)) {
  122. return false;
  123. }
  124. PercentLength other = (PercentLength) obj;
  125. return CompareUtil.equal(factor, other.factor)
  126. && CompareUtil.equal(lbase, other.lbase);
  127. }
  128. }