MATSIM
PopulationImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.* *
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2008 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 package org.matsim.core.population;
20 
21 import java.util.LinkedHashMap;
22 import java.util.Map;
23 
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.matsim.api.core.v01.Id;
33 
38 /* deliberately package */ class PopulationImpl implements Population, Lockable {
39  private static final Logger log = LogManager.getLogger(PopulationImpl.class);
40 
41  private final Attributes attributes = new AttributesImpl();
42  private String name;
43  private Map<Id<Person>, Person> persons = new LinkedHashMap<>();
44  private final PopulationFactory populationFactory;
45  private long counter = 0;
46  private long nextMsg = 1;
47 
48  PopulationImpl(PopulationFactory populationFactory2) {
49  this.populationFactory = populationFactory2 ;
50  }
51 
52  @Override
53  public void addPerson(final Person p) {
54  // validation
55  if (this.getPersons().containsKey(p.getId())) {
56  throw new IllegalArgumentException("Person with id = " + p.getId() + " already exists.");
57  }
58  if ( p instanceof Lockable ) {
59  ((Lockable) p).setLocked();
60  }
61 
62  // show counter
63  this.counter++;
64  if (this.counter % this.nextMsg == 0) {
65  this.nextMsg *= 4;
66  printPlansCount();
67  }
68 
69  this.persons.put( p.getId(), p ) ;
70  }
71 
72  @Override
73  public Person removePerson(Id<Person> personId) {
74  return this.persons.remove(personId) ;
75  }
76 
77  @Override
78  public final Map<Id<Person>, ? extends Person> getPersons() {
79  return persons ;
80  }
81 
82  @Override
83  public PopulationFactory getFactory() {
84  return this.populationFactory;
85  }
86 
87  @Override
88  public String getName() {
89  return this.name ;
90  }
91 
92  @Override
93  public void setName(String name) {
94  this.name = name ;
95  }
96 
97  @Override
98  public final void setLocked() {
99  for ( Person person : this.persons.values() ) {
100  if ( person instanceof Lockable ) {
101  ((Lockable)person).setLocked() ;
102  }
103  }
104  }
105 
106  public void printPlansCount() {
107  log.info(" person # " + this.counter);
108  }
109 
115  @Override
116  public Attributes getAttributes() {
117  return attributes;
118  }
119 }