1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

add and test removeActivity(act) and hasActivity(act) in TourActivities

This commit is contained in:
oblonski 2014-10-06 16:47:15 +02:00
parent 818cf7aaba
commit f733587b2b
3 changed files with 315 additions and 146 deletions

View file

@ -1,16 +1,16 @@
/*******************************************************************************
* Copyright (C) 2013 Stefan Schroeder
*
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
@ -34,8 +34,8 @@ public class TourActivities {
public static TourActivities copyOf(TourActivities tourActivities){
return new TourActivities(tourActivities);
}
public static class ReverseActivityIterator implements Iterator<TourActivity> {
public static class ReverseActivityIterator implements Iterator<TourActivity> {
private List<TourActivity> acts;
private int currentIndex;
@ -78,19 +78,28 @@ public class TourActivities {
private final Set<Job> jobs = new HashSet<Job>();
private final Map<Job,List<Integer>> job2activityIndeces = new HashMap<Job,List<Integer>>();
private final Set<TourActivity> activities = new HashSet<TourActivity>();
private ReverseActivityIterator backward;
private TourActivities(TourActivities tour2copy) {
for (TourActivity tourAct : tour2copy.getActivities()) {
TourActivity newAct = tourAct.duplicate();
this.tourActivities.add(newAct);
addJob(newAct);
addToActivitySet(newAct);
addJob(newAct);
}
}
public TourActivities(){
private void addToActivitySet(TourActivity newAct) {
activities.add(newAct);
}
private void removeFromActivitySet(TourActivity act) {
activities.remove(act);
}
public TourActivities(){
}
@ -111,10 +120,8 @@ public class TourActivities {
}
/**
* Returns true if job is in jobList, otherwise false.
*
* @param job
* @return
* @param job that needs to be looked up
* @return true if job is in jobList, otherwise false.
*/
public boolean servesJob(Job job) {
return jobs.contains(job);
@ -126,10 +133,10 @@ public class TourActivities {
}
/**
* Removes job AND belonging activity from tour and returns true if job has been removed, otherwise false.
* Removes job AND belonging activity from tour.
*
* @param job
* @return
* @param job to be removed
* @return true if job has been removed, otherwise false.
*/
public boolean removeJob(Job job){
boolean jobRemoved = false;
@ -145,7 +152,8 @@ public class TourActivities {
if(c instanceof JobActivity){
if(job.equals(((JobActivity) c).getJob())){
tourActivities.remove(c);
activityRemoved = true;
removeFromActivitySet(c);
activityRemoved = true;
}
}
}
@ -153,14 +161,61 @@ public class TourActivities {
return activityRemoved;
}
/**
/**
* Returns true if this contains specified activity.
*
* @param activity to be looked up
* @return true if this contains specified activity, false otherwise
*/
public boolean hasActivity(TourActivity activity){
return activities.contains(activity);
}
/**
* Removes activity from this activity sequence. Removes its corresponding job as well, if there are no other activities
* related to this job.
*
* @param activity to be removed
* @return true if activity has been removed, false otherwise
*/
public boolean removeActivity(TourActivity activity) {
Job job = null;
if(activity instanceof JobActivity){
job = ((JobActivity) activity).getJob();
}
boolean jobIsAlsoAssociateToOtherActs = false;
boolean actRemoved = false;
List<TourActivity> acts = new ArrayList<TourActivity>(tourActivities);
for(TourActivity act : acts){
if(act == activity){
tourActivities.remove(act);
removeFromActivitySet(act);
actRemoved = true;
}
else{
if(act instanceof JobActivity && job != null){
if(((JobActivity) act).getJob().equals(job)){
jobIsAlsoAssociateToOtherActs = true;
}
}
}
}
if(!jobIsAlsoAssociateToOtherActs && actRemoved){
jobs.remove(job);
}
return actRemoved;
}
/**
* Inserts the specified activity add the specified insertionIndex. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
* <p>If specified activity instanceof JobActivity, it adds job to jobList.
* <p>If insertionIndex > tourActivitiies.size(), it just adds the specified act at the end.
*
* @param insertionIndex
* @param act
* @param insertionIndex index where activity needs to be inserted
* @param act activity to be inserted
* @throws IndexOutOfBoundsException if insertionIndex < 0;
*/
public void addActivity(int insertionIndex, TourActivity act) {
@ -180,19 +235,21 @@ public class TourActivities {
else if(insertionIndex >= tourActivities.size()) {
tourActivities.add(act);
}
addJob(act);
addToActivitySet(act);
addJob(act);
}
/**
* Adds specified activity at the end of activity-list.
* <p>If act instanceof JobActivity, it adds underlying job also.
* @throws IllegalStateException if activity-list already contains act.
* @param act
* @param act to be added
*/
public void addActivity(TourActivity act){
if(tourActivities.contains(act)) throw new IllegalStateException("act " + act + " already in tour. cannot add act twice.");
tourActivities.add(act);
addJob(act);
addToActivitySet(act);
addJob(act);
}
private void addJob(TourActivity act) {
@ -203,9 +260,9 @@ public class TourActivities {
}
/**
* Returns number of jobs.
* Returns number of jobs assiciated to activities in this activity sequence.
*
* @return
* @return no. of jobs
*/
public int jobSize() {
return jobs.size();