aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/fo/properties/PropertyCache.java
diff options
context:
space:
mode:
authorAndreas L. Delmelle <adelmelle@apache.org>2007-07-07 20:13:41 +0000
committerAndreas L. Delmelle <adelmelle@apache.org>2007-07-07 20:13:41 +0000
commit50505cd29cf2acf34df427f9048a36337e8f1304 (patch)
treebd9494ca9caaada23d0954e718e2a76da62e8cd7 /src/java/org/apache/fop/fo/properties/PropertyCache.java
parent7433301271f20e1b5483aee5f9134c69d894d011 (diff)
downloadxmlgraphics-fop-50505cd29cf2acf34df427f9048a36337e8f1304.tar.gz
xmlgraphics-fop-50505cd29cf2acf34df427f9048a36337e8f1304.zip
Partial application of the patch in Bugzilla 41044:
* addition of a generic PropertyCache to be used by all Property types that can be safely canonicalized * modified EnumProperty, StringProperty, NumberProperty, EnumNumber and FixedLength to make use of the cache infrastructure git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@554251 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/fo/properties/PropertyCache.java')
-rw-r--r--src/java/org/apache/fop/fo/properties/PropertyCache.java55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/java/org/apache/fop/fo/properties/PropertyCache.java b/src/java/org/apache/fop/fo/properties/PropertyCache.java
new file mode 100644
index 000000000..176cdd4aa
--- /dev/null
+++ b/src/java/org/apache/fop/fo/properties/PropertyCache.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.fo.properties;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+/**
+ * Thin wrapper around a HashMap to implement the property caching idiom
+ * in which a new Property instance is created then tested against cached
+ * instances created previously. If an existing property is found, this is
+ * retained and the newly created one is instantly eligible for garbage
+ * collection.
+ */
+public class PropertyCache {
+
+ private Map propCache = Collections.synchronizedMap(new WeakHashMap());
+
+
+ /**
+ * Checks if the given property is present in the cache - if so, returns
+ * a reference to the cached value. Otherwise the given object is added
+ * to the cache and returned.
+ * @param obj
+ * @return the cached instance
+ */
+ public Property fetch(Property prop) {
+
+ Property cacheEntry = (Property) propCache.get(prop);
+ if (cacheEntry != null) {
+ return cacheEntry;
+ } else {
+ propCache.put(prop, prop);
+ return prop;
+ }
+ }
+}