MATSIM
Matrix.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Matrix.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 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.matrices;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.TreeMap;
27 
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30 
31 public final class Matrix {
32 
34  // member variables
36 
37  private final String id;
38  private String desc = null;
39 
40  // double data structure. for fast access via from_location or via to_location
41  private final TreeMap<String,ArrayList<Entry>> fromLocs = new TreeMap<>();
42  private final TreeMap<String,ArrayList<Entry>> toLocs = new TreeMap<>();
43 
44  private static final Logger log = LogManager.getLogger(Matrix.class);
45 
46  private long counter = 0;
47  private long nextMsg = 1;
48 
50  // Constructors
52 
53  public Matrix(final String id, final String desc) {
54  if (id == null) {
55  throw new NullPointerException("id must not be null");
56  }
57  this.id = id;
58  this.desc = desc;
59  }
60 
62  // create methods
64 
65  public final Entry createAndAddEntry(final String fromLocId, final String toLocId, final double value) {
66 
67  // create an entry
68  Entry e = new Entry(fromLocId, toLocId, value);
69 
70  // add it to the from location data structure
71  if (!this.fromLocs.containsKey(fromLocId)) {
72  this.fromLocs.put(fromLocId, new ArrayList<Entry>());
73  }
74  ArrayList<Entry> fe = this.fromLocs.get(fromLocId);
75  fe.add(e);
76 
77  // add it to the to location data structure
78  if (!this.toLocs.containsKey(toLocId)) {
79  this.toLocs.put(toLocId, new ArrayList<Entry>());
80  }
81  ArrayList<Entry> te = this.toLocs.get(toLocId);
82  te.add(e);
83 
84  // show counter
85  this.counter++;
86  if (this.counter % this.nextMsg == 0) {
87  this.nextMsg *= 2;
88  log.info("Matrix id=" + this.id + ": entry # " + this.counter);
89  }
90 
91  // return the new entry
92  return e;
93  }
94 
96  // set/add methods
98 
99  public final Entry setEntry(final String fromLocId, final String toLocId, final double value) {
100  Entry e = getEntry(fromLocId, toLocId);
101  if (e == null) {
102  return createAndAddEntry(fromLocId, toLocId, value);
103  }
104  e.setValue(value);
105  return e;
106  }
107 
108  public final void setDesc(final String desc) {
109  this.desc = desc;
110  }
111 
113  // remove methods
115 
116  public final void removeEntry(final String from_loc, final String to_loc) {
117  Entry entry = getEntry(from_loc, to_loc);
118  ArrayList<Entry> from_loc_entries = this.fromLocs.get(from_loc);
119  ArrayList<Entry> to_loc_entries = this.toLocs.get(to_loc);
120 
121  if ((from_loc_entries == null) || (to_loc_entries == null)) {
122  throw new RuntimeException("entry with from_loc_id=" + from_loc +
123  " and to_loc_id=" + to_loc + " does not exist!" +
124  " Inconsistent data strucutre!!!");
125  }
126 
127  if (!from_loc_entries.remove(entry)) {
128  throw new RuntimeException("entry with from_loc_id=" + from_loc +
129  " and to_loc_id=" + to_loc + " does not exist!" +
130  " Inconsistent data strucutre!!!");
131  }
132  if (from_loc_entries.isEmpty()) {
133  this.fromLocs.remove(from_loc);
134  }
135 
136  if (!to_loc_entries.remove(entry)) {
137  throw new RuntimeException("entry with from_loc_id=" + from_loc +
138  " and to_loc_id=" + to_loc + " does not exist!" +
139  " Inconsistent data strucutre!!!");
140  }
141  if (to_loc_entries.isEmpty()) {
142  this.toLocs.remove(to_loc);
143  }
144  log.info("entry " + entry.toString() + " removed.");
145  }
146 
148  // get methods
150 
151  public final String getId() {
152  return this.id;
153  }
154 
155  public final String getDesc() {
156  return this.desc;
157  }
158 
159  public final Map<String, ArrayList<Entry>> getFromLocations() {
160  return this.fromLocs;
161  }
162 
163  public final Map<String, ArrayList<Entry>> getToLocations() {
164  return this.toLocs;
165  }
166 
167  public final List<Entry> getFromLocEntries(final String fromLocationId) {
168  return this.fromLocs.get(fromLocationId);
169  }
170 
171  public final List<Entry> getToLocEntries(final String toLocationId) {
172  return this.toLocs.get(toLocationId);
173  }
174 
175  public final Entry getEntry(final String from, final String to) {
176  ArrayList<Entry> fe = this.fromLocs.get(from);
177  if (fe == null) return null;
178  for (int i=0; i<fe.size(); i++) {
179  Entry e = fe.get(i);
180  if (e.getToLocation().equals(to)) {
181  return e;
182  }
183  }
184  return null;
185  }
186 
188  // print methods
190 
191  @Override
192  public final String toString() {
193  return "[id=" + this.id + "]" +
194  "[desc=" + this.desc + "]" +
195  "[nof_from_locs=" + this.fromLocs.size() + "]" +
196  "[nof_to_locs=" + this.toLocs.size() + "]";
197  }
198 }
final List< Entry > getFromLocEntries(final String fromLocationId)
Definition: Matrix.java:167
final Entry createAndAddEntry(final String fromLocId, final String toLocId, final double value)
Definition: Matrix.java:65
Definition: Entry.java:23
final TreeMap< String, ArrayList< Entry > > fromLocs
Definition: Matrix.java:41
final String toString()
Definition: Matrix.java:192
final List< Entry > getToLocEntries(final String toLocationId)
Definition: Matrix.java:171
final void removeEntry(final String from_loc, final String to_loc)
Definition: Matrix.java:116
final String toString()
Definition: Entry.java:78
final String getToLocation()
Definition: Entry.java:65
final String getDesc()
Definition: Matrix.java:155
final Map< String, ArrayList< Entry > > getFromLocations()
Definition: Matrix.java:159
static final Logger log
Definition: Matrix.java:44
final TreeMap< String, ArrayList< Entry > > toLocs
Definition: Matrix.java:42
final Entry setEntry(final String fromLocId, final String toLocId, final double value)
Definition: Matrix.java:99
final Map< String, ArrayList< Entry > > getToLocations()
Definition: Matrix.java:163
Matrix(final String id, final String desc)
Definition: Matrix.java:53
final String getId()
Definition: Matrix.java:151
final void setDesc(final String desc)
Definition: Matrix.java:108
final void setValue(final double value)
Definition: Entry.java:53
final Entry getEntry(final String from, final String to)
Definition: Matrix.java:175