All Downloads are FREE. Search and download functionalities are using the official Maven repository.

opennlp.tools.util.Cache Maven / Gradle / Ivy

There is a newer version: 2.5.3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreemnets.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package opennlp.tools.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Provides fixed size, pre-allocated, least recently used replacement cache.
 */
@SuppressWarnings("unchecked")
public class Cache implements Map {

  /** The element in the linked list which was most recently used. **/
  private DoubleLinkedListElement first;
  /** The element in the linked list which was least recently used. **/
  private DoubleLinkedListElement last;
  /** Temporary holder of the key of the least-recently-used element. */
  private Object lastKey;
  /** Temporary value used in swap. */
  private ObjectWrapper temp;
  /** Holds the object wrappers which the keys are mapped to. */
  private ObjectWrapper[] wrappers;
  /** Map which stores the keys and values of the cache. */
  private Map map;
  /** The size of the cache. */
  private int size;

  /**
   * Creates a new cache of the specified size.
   * @param size The size of the cache.
   */
  public Cache(int size) {
    map = new HashMap(size);
    wrappers = new ObjectWrapper[size];
    this.size=size;
    Object o = new Object();
    first = new DoubleLinkedListElement(null, null, o);
    map.put(o, new ObjectWrapper(null, first));
    wrappers[0] = new ObjectWrapper(null, first);

    DoubleLinkedListElement e = first;
    for(int i=1; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy