1 package com.fasterxml.jackson.annotation; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import com.fasterxml.jackson.annotation.ObjectIdGenerator.IdKey; 7 8 /** 9 * Simple implementation of {@link ObjectIdResolver} 10 * 11 * @author Pascal Gélinas 12 */ 13 public class SimpleObjectIdResolver implements ObjectIdResolver { 14 protected Map<IdKey,Object> _items; 15 SimpleObjectIdResolver()16 public SimpleObjectIdResolver() { } 17 18 @Override bindItem(IdKey id, Object ob)19 public void bindItem(IdKey id, Object ob) 20 { 21 if (_items == null) { 22 _items = new HashMap<ObjectIdGenerator.IdKey,Object>(); 23 } else if (_items.containsKey(id)) { 24 throw new IllegalStateException("Already had POJO for id (" + id.key.getClass().getName() + ") [" + id 25 + "]"); 26 } 27 _items.put(id, ob); 28 } 29 30 @Override resolveId(IdKey id)31 public Object resolveId(IdKey id) { 32 return (_items == null) ? null : _items.get(id); 33 } 34 35 @Override canUseFor(ObjectIdResolver resolverType)36 public boolean canUseFor(ObjectIdResolver resolverType) { 37 return resolverType.getClass() == getClass(); 38 } 39 40 @Override newForDeserialization(Object context)41 public ObjectIdResolver newForDeserialization(Object context) { 42 // 19-Dec-2014, tatu: Important: must re-create without existing mapping; otherwise bindings leak 43 // (and worse, cause unnecessary memory retention) 44 return new SimpleObjectIdResolver(); 45 } 46 } 47