Reputation: 311
I'm taking in all the information from an MySQL database table using a resultset and adding all the values into an array
public void populateQueueFromDB() {
// create priority queue
try {
String url = "jdbc:mysql://localhost:3306/project";
Connection conn = DriverManager.getConnection(url, "root", "nbuser");
PreparedStatement stmt = conn.prepareStatement("SELECT user_id,s_date,e_date,d_date,department,projectname,projectapplication,priority,cores,disk_space,analysis FROM booking");
ResultSet rs;
rs = stmt.executeQuery();
//List<JobRequest> jobList = new ArrayList<JobRequest>();
while (rs.next()) {
JobRequest job = new JobRequest();
User user = new User();
user.setUserID(rs.getString("user_id"));
job.setUserID(user.getUserID()); // changes the /user id to the job.setuser id so can call for my queue print.
job.setStartDate(rs.getString("s_date"));
job.setEndDate(rs.getString("e_date"));
job.setDeadDate(rs.getString("d_date"));
job.setDepartment(rs.getString("department"));
job.setProjectName(rs.getString("projectname"));
job.setProjectApplication(rs.getString("projectapplication"));
job.setPriority(rs.getInt("priority"));
job.setCores(rs.getInt("cores"));
job.setDiskSpace(rs.getInt("disk_space"));
job.setAnalysis(rs.getString("analysis"));
schedulerPriorityQueue.addJob( job );
}
schedulerPriorityQueue.printQueue();
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
from here I go off and call my comparator to order the data, depending on priority of being either 1,2,3 then sort the queue. some other bits of code naming etc but essentially it sends me to the comparator
public class JobQueueComparator implements Comparator<JobRequest> {
@Override
public int compare(JobRequest object1, JobRequest object2) {
if(object1.getPriority() < object2.getPriority()){
return 1;
} else {
return -1;
}
}
}
But the output I'm getting from the comparator is ordering it in priority 3, 1 then 2. I've adapted this from example online but I don't understand the returns on comparator examples I've seen.
How would I be able to change that comparator to sort my priorities, 1 being the most important, 3 being the least. I made sure I'm printing out the output after adding all the result sets into the array so I know it's working as it's changed my ordering around, just dont know how to order it how I want.
Thanks
EDIT: schedulerPriorityQueue
public class Queue {
private Comparator<JobRequest> comparator = new JobQueueComparator(); //calls my comparator
private PriorityQueue< JobRequest> scheduledJobs = new PriorityQueue<JobRequest>(100, comparator);
public void addJob(JobRequest job) {
// now add job to priority queue
scheduledJobs.add(job); // add jobs from the resultset into queue
}
Upvotes: 0
Views: 165
Reputation: 2610
Make it easy on yourself and use Integer
and its compareTo
method.
Your comparator method would look like this
@Override
public int compare(JobRequest object1, JobRequest object2) {
Integer iO1 = Integer.valueOf(object1.getPriority());
Integer iO2 = Integer.valueOf(object2.getPriority());
return -(i01.compareTo(iO2));
}
Assuming getPriority
returns a int
or String
.
Upvotes: 2
Reputation: 5067
You are missing the case where the priorities are equal. I would use something like this:
public class JobQueueComparator implements Comparator<JobRequest> {
@Override
public int compare(JobRequest object1, JobRequest object2) {
return -(object1.getPriority() - object2.getPriority());
}
}
This would work if you take into consideration the priority order like this: 1, 2, 3.
Upvotes: 0
Reputation: 3241
You should return 0 from the comparator if the two objects are equal, an integer less than 0 if object 1 < object 2, and an integer greater than 0 if object 1 > object 2. Currently the JobQueueComparator never returns 0. Try the following:
public class JobQueueComparator implements Comparator<JobRequest> {
@Override
public int compare(JobRequest object1, JobRequest object2) {
return object1.getPriority() - object2.getPriority();
}
}
See the Comparator documentation for more details.
Upvotes: 0
Reputation: 5686
Simply swap the signs on the compare method. This will reverse the order.
public int compare(JobRequest object1, JobRequest object2) {
if(object1.getPriority() < object2.getPriority()){
return -1;
} else {
return +1;
}
}
Upvotes: 0