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
;
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>
33 * @param size the maximum cache size, must be between 0, inclusive, and
34 * NOT_CACHED, exclusive.
36 public Cache(int size
) {
40 public int add(boolean[] found
, Object content
) {
41 Entry e
= map
.get(content
);
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
);
53 } else if (last
!= null) {
54 // Take last entry out and recycle as new front:
55 map
.remove(last
.content
);
59 // Reached only if maxSize > 1:
68 // Reached iff maxSize == 0:
72 } else if (e
!= first
) {
73 // Move to front (reached only if maxSize > 1):
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
;
98 public Object content
;
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
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;