001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.client.event;
020
021import java.util.*;
022
023import org.jppf.client.*;
024import org.jppf.client.balancer.queue.JPPFPriorityQueue;
025
026/**
027 * Instances of this class represent events emitted by the JPPF client job queue whenever a job is added to or removed from the queue.
028 * @author Laurent Cohen
029 * @since 4.1
030 */
031public class ClientQueueEvent extends EventObject {
032  /**
033   * The job this event is for.
034   */
035  private final JPPFJob job;
036  /**
037   * The job queue from which this event originates.
038   */
039  private final JPPFPriorityQueue queue;
040
041  /**
042   * Initialize this event with the specified source JPPF client and Job.
043   * @param client the client whose queue emitted the event.
044   * @param job the job this event is for.
045   * @param queue the job queue which emitted this event.
046   */
047  public ClientQueueEvent(final JPPFClient client, final JPPFJob job, final JPPFPriorityQueue queue) {
048    super(client);
049    this.job = job;
050    this.queue = queue;
051  }
052
053  /**
054   * Get the JPPF client source of this event.
055   * @return a {@link JPPFClient} instance.
056   */
057  public JPPFClient getClient() {
058    return (JPPFClient) getSource();
059  }
060
061  /**
062   * Get the job that was added or removed.
063   * @return a {@link JPPFJob} instance.
064   */
065  public JPPFJob getJob() {
066    return job;
067  }
068
069  /**
070   * Get all the {@link JPPFJob}s currently in the queue.
071   * <p>This method should be used with caution, as its cost is in O(n), with n being the number of jobs in the queue.
072   * @return a list of {@link JPPFJob} instances ordered by their priority.
073   */
074  public List<JPPFJob> getQueuedJobs() {
075    return queue.getJPPFJobs();
076  }
077
078  /**
079   * Get the size of this job queue.
080   * <p>This method should be used with caution, as its cost is in O(n), with n being the number of jobs in the queue.
081   * @return the number of jobs currently in the queue.
082   */
083  public int getQueueSize() {
084    return queue.getQueueSize();
085  }
086}