Java Collections.asLifoQueue Examples

Java Collections.asLifoQueue - 3 examples found. These are the top rated real world Java examples of java.util.Collections.asLifoQueue extracted from open source projects. You can rate examples to help us improve the quality of examples.
  public static void main(String args[]) {

    Deque<String> dq = new ArrayDeque<String>(5);
    dq.add("java");
    dq.add("c");
    dq.add("c++");
    dq.add("unix");
    dq.add("perl");
    System.out.println("queue is: " + dq);
    Queue<String> q = Collections.asLifoQueue(dq);
    System.out.println("returned queue is: " + q);
    q.remove();
    System.out.println("queue is: " + q);
  }
    public void load() throws IOException {
      this.propertiesLoader = new PropertySourcesLoader();
      this.profiles = Collections.asLifoQueue(new LinkedList<String>());
      this.activatedProfiles = false;

      if (this.environment.containsProperty(ACTIVE_PROFILES_PROPERTY)) {
        // Any pre-existing active profiles set via property sources (e.g. System
        // properties) take precedence over those added in config files.
        maybeActivateProfiles(this.environment.getProperty(ACTIVE_PROFILES_PROPERTY));
      } else {
        // Pre-existing active profiles set via Environment.setActiveProfiles()
        // are additional profiles and config files are allowed to add more if
        // they want to, so don't call addActiveProfiles() here.
        this.profiles.addAll(Arrays.asList(this.environment.getActiveProfiles()));
      }

      // The default profile for these purposes is represented as null. We add it
      // last so that it is first out of the queue (active profiles will then
      // override any settings in the defaults when the list is reversed later).
      this.profiles.add(null);

      while (!this.profiles.isEmpty()) {
        String profile = this.profiles.poll();
        for (String location : getSearchLocations()) {
          if (!location.endsWith("/")) {
            // location is a filename already, so don't search for more
            // filenames
            load(location, null, profile);
          } else {
            for (String name : getSearchNames()) {
              load(location, name, profile);
            }
          }
        }
      }

      addConfigurationProperties(this.propertiesLoader.getPropertySources());
    }
Example #3
0
/**
 * Request Context Performance object for a given Request (i.e. RequestContext). This is created at
 * the beginning of every request and hold the duration for each timer.
 *
 * <p>So, it keeps a very simplistic name/duration map for the request, and also use the
 * PerformanceManager to track the time across request (which uses Metrics).
 *
 * <p>Single Thread: This is supposed to be used in conjunction of the RequestContext, and therefore
 * is assumed to be used in a single thread.
 */
public class RcPerf {
  private static final Logger logger = org.slf4j.LoggerFactory.getLogger(RcPerf.class);

  // this is for the application wide request metrics (using Metrics).
  private final MetricRegistry requestsMetrics;
  private final String pathInfo;
  private Context requestMetricsStartContext;

  // single request metrics (not using Metrics)
  Long requestOffset = null;
  Map<String, RcTimer> rootRcTimers = new HashMap<String, RcTimer>();
  final Queue<RcTimer> rcTimerStack = Collections.asLifoQueue(new ArrayDeque<RcTimer>());

  public PerfContext rootRequestPerfCtx;

  public RcPerf(String pathInfo, MetricRegistry requestsMetrics) {
    this.pathInfo = pathInfo;
    this.requestsMetrics = requestsMetrics;
  }

  public void startRequest() {
    rootRequestPerfCtx = start("req");
    Timer metricsTimer = requestsMetrics.timer(pathInfo);
    requestMetricsStartContext = metricsTimer.time();
  }

  public Map getRcPerfInfo() {
    return rootRcTimers;
  }

  public void endRequest() {
    if (rootRequestPerfCtx != null) {
      rootRequestPerfCtx.end();
    }
    if (requestMetricsStartContext != null) {
      requestMetricsStartContext.stop();
    }
  }

  public PerfContext start(String name) {
    RcTimer rcTimer;
    Map<String, RcTimer> rcTimerMap = getRcTimerMap();

    // We check if we have an already existing RcTimer for this name in the rcTimerMap
    rcTimer = rcTimerMap.get(name);

    // if we do not have, we create a new one
    if (rcTimer == null) {
      rcTimer = newRcTimer(name);
      rcTimerMap.put(name, rcTimer);
    }

    rcTimerStack.add(rcTimer);

    rcTimer.start();

    final RcTimer finalRcTimer = rcTimer;

    return new PerfContext() {
      @Override
      public void end() {
        finalRcTimer.end();
      }
    };
  }

  /**
   * Get the RcTimerMap (name/rcTimer) from the current RcTimer in the stack or from the
   * rootRcTimers;
   *
   * @return
   */
  private Map<String, RcTimer> getRcTimerMap() {
    Map<String, RcTimer> rcTimerMap;
    // get the current rcTimerMap for this level
    // First, look in the stack if we are already running a timer
    RcTimer currentRcTimer = rcTimerStack.peek();
    if (currentRcTimer != null) {
      // if we have a RcTimer in the stack, we take it subs as the map
      rcTimerMap = currentRcTimer.getSubs();
    } else {
      // otherwise, we take the rootRcTimers
      rcTimerMap = rootRcTimers;
    }
    return rcTimerMap;
  }

  private RcTimer newRcTimer(String name) {
    //		RcTimer rcTimer;
    Long timerStart;
    if (requestOffset == null) {
      requestOffset = System.currentTimeMillis();
      timerStart = requestOffset;
    } else {
      timerStart = System.currentTimeMillis();
    }
    return new RcTimer(name, timerStart);
  }

  public class RcTimer {

    private String name;
    private int count = 0;
    private Long firstTimerStart = null;
    private Long timerStart = null;
    private Long timerEnd = null;
    private Long duration = 0L;
    private Map<String, RcTimer> subs = new HashMap<String, RcTimer>();

    private RcTimer(String name, Long timerStart) {
      this.name = name;
      this.timerStart = timerStart;
      this.firstTimerStart = timerStart;
    }

    RcTimer start() {
      count++;
      timerStart = (timerStart != null) ? timerStart : System.currentTimeMillis();
      return this;
    }

    RcTimer end() {
      timerEnd = System.currentTimeMillis();
      duration += timerEnd - timerStart;
      timerStart = null;
      RcTimer fromStack = rcTimerStack.peek();
      if (fromStack == this) {
        rcTimerStack.poll();
      } else {
        logger.warn(
            "RcTimer.end does not match with the latest one in the stack.\n"
                + "\tfrom rcTimer: "
                + this
                + "\n"
                + "\tfrom stack: "
                + fromStack
                + "\n");
      }
      return this;
    }

    public Integer getCount() {
      return count;
    }

    public Map<String, RcTimer> getSubs() {
      return subs;
    }

    public RcTimer addSub(String name, RcTimer rcTimer) {
      subs.put(name, rcTimer);
      return this;
    }

    public RcTimer getSub(String name) {
      return subs.get(name);
    }

    /**
     * Return the relative start from the requestOffset
     *
     * @return
     */
    public Long getStart() {
      return firstTimerStart - requestOffset;
    }

    public Long getDuration() {
      return duration;
    }

    public String toString() {
      return "RcPref[" + name + "]";
    }
  }
}