MATSIM
ObjectAttributes.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2010 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 
20 package org.matsim.utils.objectattributes;
21 
22 import java.util.IdentityHashMap;
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 
28 
44 public class ObjectAttributes implements MatsimExtensionPoint {
45 
46  /*package*/ Map<String, Map<String, Object>> attributes = new LinkedHashMap<String, Map<String, Object>>(1000);
47 
48  @Override
49  public String toString() {
50  StringBuilder stb = new StringBuilder() ;
51  for ( Entry<String, Map<String,Object>> entry : attributes.entrySet() ) {
52  String key = entry.getKey() ;
53  stb.append("key=").append(key);
54  Map<String,Object> map = entry.getValue() ;
55  for ( Entry<String,Object> ee : map.entrySet() ) {
56  String subkey = ee.getKey();
57  stb.append("; subkey=").append(subkey);
58  stb.append("; object=").append(ee.getValue().toString());
59  }
60  stb.append("\n") ;
61  }
62  return stb.toString() ;
63  }
64 
65  public Object putAttribute(final String objectId, final String attribute, final Object value) {
66  Map<String, Object> attMap = this.attributes.get(objectId);
67  if (attMap == null) {
68  attMap = new IdentityHashMap<String, Object>(5);
69  this.attributes.put(objectId, attMap);
70  }
71  return attMap.put(attribute.intern(), value);
72  }
73 
74  public Object getAttribute(final String objectId, final String attribute) {
75  Map<String, Object> attMap = this.attributes.get(objectId);
76  if (attMap == null) {
77  return null;
78  }
79  return attMap.get(attribute.intern());
80  }
81 
82  public Object removeAttribute(final String objectId, final String attribute) {
83  Map<String, Object> attMap = this.attributes.get(objectId);
84  if (attMap == null) {
85  return null;
86  }
87  return attMap.remove(attribute.intern());
88  }
89 
90  public void removeAllAttributes(final String objectId) {
91  this.attributes.remove(objectId);
92  }
93 
97  public void clear() {
98  this.attributes.clear();
99  }
100 
101 }
Object putAttribute(final String objectId, final String attribute, final Object value)
Object getAttribute(final String objectId, final String attribute)
Object removeAttribute(final String objectId, final String attribute)