1 /*
2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.awt.X11;
27 
28 import java.util.*;
29 
30 /**
31  * Implements abstract X window property caching mechanism.  The
32  * caching is performed using storeCache method, the cached data can
33  * be retrieved using getCacheEntry method.
34  *
35  * NOTE: current caching is disabled because of the big variate of
36  * uncovered access to properties/changes of properties.  Once the
37  * access to properites is rewritten using general mechanisms, caching
38  * will be enabled.
39  */
40 public class XPropertyCache {
41 
42     static class PropertyCacheEntry {
43         private final int format;
44         private final int numberOfItems;
45         private final long bytesAfter;
46         private final long data;
47         private final int dataLength;
PropertyCacheEntry(int format, int numberOfItems, long bytesAfter, long data, int dataLength)48         public PropertyCacheEntry(int format, int numberOfItems, long bytesAfter, long data, int dataLength) {
49             this.format = format;
50             this.numberOfItems = numberOfItems;
51             this.bytesAfter = bytesAfter;
52             this.data = XlibWrapper.unsafe.allocateMemory(dataLength);
53             this.dataLength = dataLength;
54             XlibWrapper.memcpy(this.data, data, dataLength);
55         }
56 
getFormat()57         public int getFormat() {
58             return format;
59         }
60 
getNumberOfItems()61         public int getNumberOfItems() {
62             return numberOfItems;
63         }
64 
getBytesAfter()65         public long getBytesAfter() {
66             return bytesAfter;
67         }
68 
getData()69         public long getData() {
70             return data;
71         }
72 
getDataLength()73         public int getDataLength() {
74             return dataLength;
75         }
76     }
77 
78     private static Map<Long, Map<XAtom, PropertyCacheEntry>> windowToMap = new HashMap<Long, Map<XAtom, PropertyCacheEntry>>();
79 
isCached(long window, XAtom property)80     public static boolean isCached(long window, XAtom property) {
81         Map<XAtom, PropertyCacheEntry> entryMap = windowToMap.get(window);
82         if (entryMap != null) {
83             return entryMap.containsKey(property);
84         } else {
85             return false;
86         }
87     }
88 
getCacheEntry(long window, XAtom property)89     public static PropertyCacheEntry getCacheEntry(long window, XAtom property) {
90         Map<XAtom, PropertyCacheEntry> entryMap = windowToMap.get(window);
91         if (entryMap != null) {
92             return entryMap.get(property);
93         } else {
94             return null;
95         }
96     }
97 
storeCache(PropertyCacheEntry entry, long window, XAtom property)98     public static void storeCache(PropertyCacheEntry entry, long window, XAtom property) {
99         Map<XAtom, PropertyCacheEntry> entryMap = windowToMap.get(window);
100         if (entryMap == null) {
101             entryMap = new HashMap<XAtom, PropertyCacheEntry>();
102             windowToMap.put(window, entryMap);
103         }
104         entryMap.put(property, entry);
105     }
106 
clearCache(long window)107     public static void clearCache(long window) {
108         windowToMap.remove(window);
109     }
110 
clearCache(long window, XAtom property)111     public static void clearCache(long window, XAtom property) {
112         Map<XAtom, PropertyCacheEntry> entryMap = windowToMap.get(window);
113         if (entryMap != null) {
114             entryMap.remove(property);
115         }
116     }
117 
isCachingSupported()118     public static boolean isCachingSupported() {
119         // Currently - unsupported
120         return false;
121     }
122 }
123