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

removed constructor from StateManager and its according implications

This commit is contained in:
oblonski 2014-07-17 12:41:47 +02:00
parent 279826f061
commit 00eef79679
6 changed files with 28 additions and 196 deletions

View file

@ -1,83 +0,0 @@
/*******************************************************************************
* 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
* 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/>.
*
* Contributors:
* Stefan Schroeder - initial API and implementation
******************************************************************************/
package jsprit.core.algorithm.state;
import java.util.HashMap;
import java.util.Map;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.solution.route.state.StateFactory;
import jsprit.core.problem.solution.route.state.StateFactory.State;
public class GenericsTest {
// static class State<T> {
// Class<T> type;
// T state;
// public State(Class<T> type, T state) {
// super();
// this.type = type;
// this.state = state;
// }
//
// }
static class States {
private Map<String,Object> states = new HashMap<String,Object>();
public <T> void putState(String id, Class<T> type, T state){
states.put(id, type.cast(state));
}
public <T> T getState(String id, Class<T> type){
T s = type.cast(states.get(id));
return s;
}
}
public static void main(String[] args) {
States states = new States();
states.putState("load", Double.class, 0.1);
states.putState("state", String.class, "foo");
states.putState("job", Job.class, Service.Builder.newInstance("foo").setLocationId("loc").build());
states.putState("cap", Capacity.class, Capacity.Builder.newInstance().addDimension(0, 1).build());
Double load = states.getState("load", Double.class);
String state = states.getState("state", String.class);
Job service = states.getState("job", Job.class);
Capacity capacity = states.getState("cap", Capacity.class);
states.putState("st", State.class, StateFactory.createState(10.));
System.out.println(load);
System.out.println(state);
System.out.println(service);
System.out.println(capacity);
System.out.println(states.getState("st", State.class).toDouble());
}
}

View file

@ -24,7 +24,6 @@ import jsprit.core.problem.job.Service;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.solution.route.state.StateFactory;
import jsprit.core.problem.solution.route.state.StateFactory.State;
import jsprit.core.problem.solution.route.state.StateFactory.StateId;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
@ -34,20 +33,9 @@ import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class StateManagerTest {
@Test
public void whenRouteStateIsSetWithGenericMethod_itMustBeSetCorrectly(){
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().build();
VehicleRoute route = getRoute(mock(Vehicle.class));
StateManager stateManager = new StateManager(vrp);
StateId id = stateManager.createStateId("myState");
State state = StateFactory.createState(1.);
stateManager.putRouteState(route, id, state);
assertEquals(1.,stateManager.getRouteState(route, id, State.class).toDouble(),0.01);
}
private VehicleRoute getRoute(Vehicle vehicle) {
return VehicleRoute.Builder.newInstance(vehicle).addService(Service.Builder.newInstance("s").setLocationId("loc").build()).build();
@ -84,16 +72,7 @@ public class StateManagerTest {
assertEquals(500, getCap.get(0));
}
@Test
public void whenActivityStateIsSetWithGenericMethod_itMustBeSetCorrectly(){
TourActivity activity = mock(TourActivity.class);
when(activity.getIndex()).thenReturn(0);
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = stateManager.createStateId("myState");
State state = StateFactory.createState(1.);
stateManager.putActivityState(activity, id, state);
assertEquals(1.,stateManager.getActivityState(activity, id, State.class).toDouble(),0.01);
}
@Test
public void whenActivityStateIsSetWithGenericMethodAndBoolean_itMustBeSetCorrectly(){