/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

package edu.stanford.smi.protege.server.framestore.background;

import java.util.EnumSet;

public enum CacheRequestReason {
  USER_REQUESTED_FRAME_VALUES,  USER_NAME_REQUEST, USER_CLOSURE_REQUEST, USER_SPECIFIC_FRAMES,
  NEW_FRAME, IMMEDIATE_PRELOAD, PRELOAD, SUBCLASS, DIRECT_INSTANCES, STATE_MACHINE;

  private static int MIN_PRIORITY;
  static {
    int min = STATE_MACHINE.priority();
    for (CacheRequestReason reason : CacheRequestReason.values()) {
      if (reason.priority() < min) {
        min = reason.priority();
      }
      MIN_PRIORITY = min;
    }
  }

  public int priority() {
    switch (this) {
    case USER_REQUESTED_FRAME_VALUES:
      return 8;
    case USER_CLOSURE_REQUEST:
      return 7;
    case NEW_FRAME:
      return 6;
    case STATE_MACHINE:
      return 3;
    case IMMEDIATE_PRELOAD:
      return 3;
    case SUBCLASS:
      return -1;
    case PRELOAD:
      return -1;
    default:
      return 0;
    }
  }

  public static int priority(EnumSet<CacheRequestReason> reasons) {
    int priority = MIN_PRIORITY;
    for (CacheRequestReason reason : reasons) {
      if (reason.priority() > priority) {
        priority = reason.priority();
      }
    }
    return priority;
  }

}
