bump product version to 5.0.4.1
[LibreOffice.git] / jurt / com / sun / star / lib / uno / protocols / urp / Cache.java
blob544a06406648170e4601d291aa3517608fc684af
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com.sun.star.lib.uno.protocols.urp;
21 import java.util.HashMap;
23 /**
24 * An LRU cache for arbitrary objects.
26 * <p>This class is not synchronized, as any necessary synchronization will already
27 * take place in the client.</p>
29 final class Cache {
30 /**
31 * Create a cache.
33 * @param size the maximum cache size, must be between 0, inclusive, and
34 * NOT_CACHED, exclusive.
36 public Cache(int size) {
37 maxSize = size;
40 public int add(boolean[] found, Object content) {
41 Entry e = map.get(content);
42 found[0] = e != null;
43 if (e == null) {
44 if (map.size() < maxSize) {
45 // There is still room for a new entry at the front:
46 e = new Entry(content, map.size(), null, first);
47 if (first == null) {
48 last = e;
49 } else {
50 first.prev = e;
52 first = e;
53 } else if (last != null) {
54 // Take last entry out and recycle as new front:
55 map.remove(last.content);
56 e = last;
57 e.content = content;
58 if (first != last) {
59 // Reached only if maxSize > 1:
60 last = last.prev;
61 last.next = null;
62 e.prev = null;
63 e.next = first;
64 first.prev = e;
65 first = e;
67 } else {
68 // Reached iff maxSize == 0:
69 return NOT_CACHED;
71 map.put(content, e);
72 } else if (e != first) {
73 // Move to front (reached only if maxSize > 1):
74 e.prev.next = e.next;
75 if (e.next == null) {
76 last = e.prev;
77 } else {
78 e.next.prev = e.prev;
80 e.prev = null;
81 e.next = first;
82 first.prev = e;
83 first = e;
85 return e.index;
88 public static final int NOT_CACHED = 0xFFFF;
90 private static final class Entry {
91 public Entry(Object content, int index, Entry prev, Entry next) {
92 this.content = content;
93 this.index = index;
94 this.prev = prev;
95 this.next = next;
98 public Object content;
99 public int index;
100 public Entry prev;
101 public Entry next;
104 // first/last form a list of 0 to maxSize entries, most recently used first;
105 // map contains the same entries; each entry has a unique index in the range
106 // 0 to maxSize - 1
107 private final int maxSize;
108 private final HashMap<Object, Entry> map = new HashMap<Object, Entry>(); // from Object to Entry
109 private Entry first = null;
110 private Entry last = null;