Package com.intellij.openapi.util

Source Code of com.intellij.openapi.util.FieldCache

/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed 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 com.intellij.openapi.util;

import com.intellij.util.concurrency.JBLock;
import com.intellij.util.concurrency.JBReentrantReadWriteLock;
import com.intellij.util.concurrency.LockFactory;

public abstract class FieldCache<T, Owner,AccessorParameter,Parameter> {
  private static final RecursionGuard ourGuard = RecursionManager.createGuard("fieldCache");
  private final JBLock r;
  private final JBLock w;

  protected FieldCache() {
    JBReentrantReadWriteLock ourLock = LockFactory.createReadWriteLock();
    r = ourLock.readLock();
    w = ourLock.writeLock();
  }

  public T get(AccessorParameter a, Owner owner, Parameter p) {
    r.lock();
    T result;
    try {
      result = getValue(owner, a);
    }
    finally {
      r.unlock();
    }

    if (result == null) {
      w.lock();

      try {
        result = getValue(owner, a);
        if (result == null) {
          RecursionGuard.StackStamp stamp = ourGuard.markStack();
          result = compute(owner, p);
          if (stamp.mayCacheNow()) {
            putValue(result, owner, a);
          }
        }
      }
      finally {
        w.unlock();
      }
    }
    return result;
  }

  public final T getCached(AccessorParameter a, Owner owner) {
    r.lock();

    try {
      return getValue(owner, a);
    }
    finally {
      r.unlock();
    }
  }

  public void clear(AccessorParameter a, Owner owner) {
    w.lock();
    try {
      putValue(null, owner, a);
    }
    finally{
      w.unlock();
    }
  }

  protected abstract T compute(Owner owner, Parameter p);
  protected abstract T getValue(Owner owner, AccessorParameter p);
  protected abstract void putValue(T t, Owner owner, AccessorParameter p);
}
TOP

Related Classes of com.intellij.openapi.util.FieldCache

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.