21 package org.matsim.api.core.v01;
29 import java.util.ArrayList;
30 import java.util.List;
32 import java.util.concurrent.ConcurrentHashMap;
33 import java.util.concurrent.ConcurrentMap;
46 private final static ConcurrentMap<Class<?>, ConcurrentMap<String, Id<?>>>
cacheId =
new ConcurrentHashMap<>();
47 private final static ConcurrentMap<Class<?>, List<Id<?>>>
cacheIndex =
new ConcurrentHashMap<>();
53 StackTraceElement[] elements = Thread.currentThread().getStackTrace();
54 boolean fromJUnit =
false;
55 for (StackTraceElement element : elements) {
56 if (element.getClassName().contains(
"junit.")) {
62 throw new RuntimeException(
"This method can only be called from JUnit-Tests, but not in normal code!");
68 public static <T>
Id<T> create(
final long key,
final Class<T> type) {
69 return create(Long.toString(key), type);
76 return create(
id.toString(), type);
82 public static <T>
Id<T> create(
final String key,
final Class<T> type) {
85 ConcurrentMap<String, Id<?>> mapId = cacheId.computeIfAbsent(type, k ->
new ConcurrentHashMap<>(1000));
90 synchronized (mapId) {
98 List<Id<?>> mapIndex = mapId.isEmpty() ?
new ArrayList<>(1000) : cacheIndex.get(type);
99 int index = mapIndex.size();
100 id =
new IdImpl<T>(key,
index);
102 cacheIndex.put(type, mapIndex);
110 public abstract int index();
112 public static <T>
Id<T> get(
int index,
final Class<T> type) {
113 List<Id<?>> mapIndex = cacheIndex.
get(type);
114 return mapIndex == null ? null : (
Id<T>)mapIndex.get(index);
117 public static <T>
Id<T> get(String id,
final Class<T> type) {
118 Map<String, Id<?>> mapId = cacheId.
get(type);
119 return mapId == null ? null : (
Id<T>)mapId.get(
id);
123 Map<String, Id<?>> mapId = cacheId.get(type);
124 return mapId == null ? 0 : mapId.size();
134 return this.toString().compareTo(o.toString());
140 if (obj instanceof
Id) {
160 private final String
id;
163 IdImpl(
final String
id,
final int index) {
175 return this.
id.hashCode();
189 return id.toString() ;
static Id< Vehicle > createVehicleId(final String str)
static< T > Id< T > get(int index, final Class< T > type)
static< T > int getNumberOfIds(final Class< T > type)
static final ConcurrentMap< Class<?>, ConcurrentMap< String, Id<?> > > cacheId
static Id< Link > createLinkId(final String str)
static void resetCaches()
static Id< Vehicle > createVehicleId(final long key)
static Id< Link > createLinkId(final long key)
static Id< Person > createPersonId(final String str)
static< T > String writeId(Id< T > id)
static< T > Id< T > create(final long key, final Class< T > type)
static final ConcurrentMap< Class<?>, List< Id<?> > > cacheIndex
static void assertNotNull(Object obj)
static Id< Node > createNodeId(final String str)
static Id< Person > createPersonId(final long key)
static< T > Id< T > create(final Id<?> id, final Class< T > type)
static Id< Person > createPersonId(final Id<?> id)
static Id< Node > createNodeId(final Id<?> id)
boolean equals(Object obj)
static< T > Id< T > create(final String key, final Class< T > type)
static Id< Node > createNodeId(final long key)
static Id< Vehicle > createVehicleId(final Id<?> id)
static Id< Link > createLinkId(final Id<?> id)