MATSIM
PersonImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Person.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007, 2008 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.population;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 
27 import org.apache.logging.log4j.LogManager;
29 import org.matsim.api.core.v01.Id;
37 
41 /* deliberately package */ final class PersonImpl implements Person, Lockable {
42 
43  private List<Plan> plans = new ArrayList<>(6);
44  private Id<Person> id;
45 
46  private Plan selectedPlan = null;
47 
48  private Customizable customizableDelegate;
49  private boolean locked;
50 
51  private final Attributes attributes = new AttributesImpl();
52 
53  /* deliberately package */ PersonImpl(final Id<Person> id) {
54  this.id = id;
55  }
56 
57  @Override
58  public final Plan getSelectedPlan() {
59  return this.selectedPlan;
60  }
61 
62  @Override
63  public boolean addPlan(final Plan plan) {
64  plan.setPerson(this);
65  // Make sure there is a selected plan if there is at least one plan
66  if (this.selectedPlan == null) this.selectedPlan = plan;
67  return this.plans.add(plan);
68  }
69 
70  @Override
71  public final void setSelectedPlan(final Plan selectedPlan) {
72  if (selectedPlan != null && !plans.contains( selectedPlan )) {
73  throw new IllegalStateException("The plan to be set as selected is not null nor stored in the person's plans");
74  }
75  this.selectedPlan = selectedPlan;
76  }
77 
78  @Override
79  public Plan createCopyOfSelectedPlanAndMakeSelected() {
80  Plan oldPlan = this.getSelectedPlan();
81  if (oldPlan == null) {
82  return null;
83  }
84  Plan newPlan = PopulationUtils.createPlan(oldPlan.getPerson());
85  PopulationUtils.copyFromTo(oldPlan, newPlan, true);
86  this.getPlans().add(newPlan);
87  this.setSelectedPlan(newPlan);
88  return newPlan;
89  }
90 
91  @Override
92  public Id<Person> getId() {
93  return this.id;
94  }
95 
96  /* deliberately package */ void changeId(final Id<Person> newId) {
97  // This is deliberately non-public and not on the interface, since the ID should not be changed after the
98  // person is inserted into the population map (since the ID is the map key).
99  // However, there are some situations where changing the ID makes sense while the person is outside
100  // the population ... kai, jun'16
101  try {
102  testForLocked() ;
103  } catch ( Exception ee ) {
104  LogManager.getLogger(getClass()).warn("cannot change oerson id while in population. remove the person, change Id, re-add.");
105  throw ee ;
106  }
107  this.id = newId;
108  }
109 
110  @Override
111  public final String toString() {
112  StringBuilder b = new StringBuilder();
113  b.append("[id=").append(this.getId()).append("]");
114  b.append("[nof_plans=").append(this.getPlans() == null ? "null" : this.getPlans().size()).append("]");
115  return b.toString();
116  }
117 
118  @Override
119  public boolean removePlan(final Plan plan) {
120  boolean result = this.getPlans().remove(plan);
121  if ((this.getSelectedPlan() == plan) && result) {
122  this.setSelectedPlan(new RandomPlanSelector<Plan, Person>().selectPlan(this));
123  }
124  return result;
125  }
126 
127  @Override
128  public List<Plan> getPlans() {
129  return this.plans;
130  }
131 
132 
133  @Override
134  public Map<String, Object> getCustomAttributes() {
135  if (this.customizableDelegate == null) {
136  this.customizableDelegate = CustomizableUtils.createCustomizable();
137  }
138  return this.customizableDelegate.getCustomAttributes();
139  }
140 
141  @Override
142  public Attributes getAttributes() {
143  return attributes;
144  }
145 
146  @Override
147  public final void setLocked() {
148  this.locked = true ;
149  // we are not locking anything in the plans
150  }
151 
152  private void testForLocked() {
153  if ( this.locked ) {
154  throw new RuntimeException("too late to do this") ;
155  }
156  }
157 
158 
159 }