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.

OptionalCharacterProperty.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.fo.FObj;
  20. import org.apache.fop.fo.PropertyList;
  21. /**
  22. * Superclass for properties that wrap an optional character value
  23. * TODO convert character value to int in order to denote unicode scalar value
  24. * instead of a single UTF-16 code element
  25. */
  26. public final class OptionalCharacterProperty extends Property {
  27. /**
  28. * Inner class for creating instances of OptionalCharacterProperty
  29. */
  30. public static class Maker extends PropertyMaker {
  31. /**
  32. * @param propId the id of the property for which a Maker should be created
  33. */
  34. public Maker(int propId) {
  35. super(propId);
  36. }
  37. /** {@inheritDoc} */
  38. public Property make(PropertyList propertyList, String value,
  39. FObj fo) {
  40. if (value.isEmpty()) {
  41. return OptionalCharacterProperty.getInstance(null);
  42. } else {
  43. char c = value.charAt(0);
  44. return OptionalCharacterProperty.getInstance(c);
  45. }
  46. }
  47. }
  48. /** cache containing all canonical OptionalCharacterProperty instances */
  49. private static final PropertyCache<OptionalCharacterProperty> CACHE
  50. = new PropertyCache<OptionalCharacterProperty>();
  51. private final Character character;
  52. /**
  53. * @param character character value to be wrapped in this property
  54. */
  55. private OptionalCharacterProperty(Character character) {
  56. this.character = character;
  57. }
  58. /**
  59. * Get character property instance for character.
  60. * @param character the character
  61. * @return the character property instance
  62. */
  63. public static OptionalCharacterProperty getInstance(Character character) {
  64. return CACHE.fetch(new OptionalCharacterProperty(character));
  65. }
  66. /**
  67. * @return this.character cast as an Object
  68. */
  69. public Object getObject() {
  70. return character;
  71. }
  72. /**
  73. * @return this.character
  74. */
  75. public char getCharacter() {
  76. return character == null ? 0 : this.character;
  77. }
  78. /**
  79. * @return this.character cast as a String
  80. */
  81. public String getString() {
  82. return character == null ? "" : character.toString();
  83. }
  84. @Override
  85. public boolean equals(Object obj) {
  86. if (obj instanceof OptionalCharacterProperty) {
  87. OptionalCharacterProperty ocp = (OptionalCharacterProperty) obj;
  88. if (character == null && ocp.character == null) {
  89. return true;
  90. } else {
  91. return (character != null && character.equals(ocp.character));
  92. }
  93. } else {
  94. return false;
  95. }
  96. }
  97. @Override
  98. public int hashCode() {
  99. return character == null ? 0 : character.hashCode();
  100. }
  101. }