bump product version to 6.4.0.3
[LibreOffice.git] / jurt / com / sun / star / lib / uno / protocols / urp / Cache.java
blob26c2d449d6178a8bbba38356e24a038cfd4b87a2
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
24 /**
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>
30 final class Cache {
31 /**
32 * Create a cache.
34 * @param size the maximum cache size, must be between 0, inclusive, and
35 * NOT_CACHED, exclusive.
37 public Cache(int size) {
38 maxSize = size;
41 public int add(boolean[] found, Object content) {
42 Entry e = map.get(content);
43 found[0] = e != null;
44 if (e == null) {
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);
48 if (first == null) {
49 last = e;
50 } else {
51 first.prev = e;
53 first = e;
54 } else if (last != null) {
55 // Take last entry out and recycle as new front:
56 map.remove(last.content);
57 e = last;
58 e.content = content;
59 if (first != last) {
60 // Reached only if maxSize > 1:
61 last = last.prev;
62 last.next = null;
63 e.prev = null;
64 e.next = first;
65 first.prev = e;
66 first = e;
68 } else {
69 // Reached iff maxSize == 0:
70 return NOT_CACHED;
72 map.put(content, e);
73 } else if (e != first) {
74 // Move to front (reached only if maxSize > 1):
75 e.prev.next = e.next;
76 if (e.next == null) {
77 last = e.prev;
78 } else {
79 e.next.prev = e.prev;
81 e.prev = null;
82 e.next = first;
83 first.prev = e;
84 first = e;
86 return e.index;
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;
94 this.index = index;
95 this.prev = prev;
96 this.next = next;
99 public Object content;
100 public int index;
101 public Entry prev;
102 public Entry next;
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
107 // 0 to maxSize - 1
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: */