1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 package com
.sun
.star
.lib
.uno
.protocols
.urp
;
22 import java
.util
.HashMap
;
25 * An LRU cache for arbitrary objects.
27 * <p>This class is not synchronized, as any necessary synchronization will already
28 * take place in the client.</p>
34 * @param size the maximum cache size, must be between 0, inclusive, and
35 * NOT_CACHED, exclusive.
37 public Cache(int size
) {
41 public int add(boolean[] found
, Object content
) {
42 Entry e
= map
.get(content
);
45 if (map
.size() < maxSize
) {
46 // There is still room for a new entry at the front:
47 e
= new Entry(content
, map
.size(), null, first
);
54 } else if (last
!= null) {
55 // Take last entry out and recycle as new front:
56 map
.remove(last
.content
);
60 // Reached only if maxSize > 1:
69 // Reached iff maxSize == 0:
73 } else if (e
!= first
) {
74 // Move to front (reached only if maxSize > 1):
89 public static final int NOT_CACHED
= 0xFFFF;
91 private static final class Entry
{
92 public Entry(Object content
, int index
, Entry prev
, Entry next
) {
93 this.content
= content
;
99 public Object content
;
105 // first/last form a list of 0 to maxSize entries, most recently used first;
106 // map contains the same entries; each entry has a unique index in the range
108 private final int maxSize
;
109 private final HashMap
<Object
, Entry
> map
= new HashMap
<Object
, Entry
>(); // from Object to Entry
110 private Entry first
= null;
111 private Entry last
= null;
114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */