1 /* 2 * Copyright 2009 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.openehealth.ipf.commons.ihe.xds.core.ebxml; 17 18 import java.util.Collection; 19 import java.util.Collections; 20 import java.util.HashMap; 21 import java.util.Map; 22 23 /** 24 * A container of objects used for resolving object references and their ids. 25 * @author Jens Riemschneider 26 */ 27 public class EbXMLObjectLibrary { 28 private final Map<String, Object> objLib = new HashMap<>(); 29 private final Map<Object, String> reverseLib = new HashMap<>(); 30 31 /** 32 * Puts an object into the library. 33 * <p> 34 * The object will only be added if both parameters are non-<code>null</code>. 35 * @param id 36 * the id of the object. Can be <code>null</code>. 37 * @param obj 38 * the object. Can be <code>null</code>. 39 */ 40 public void put(String id, Object obj) { 41 if (id != null && obj != null) { 42 objLib.put(id, obj); 43 reverseLib.put(obj, id); 44 } 45 } 46 47 /** 48 * Looks up an object via its ID. 49 * @param id 50 * the id. Can be <code>null</code>. 51 * @return the object or <code>null</code> if the input was <code>null</code> 52 * of if the library does not contain an object with the given id. 53 */ 54 public Object getById(String id) { 55 if (id == null) { 56 return null; 57 } 58 return objLib.get(id); 59 } 60 61 /** 62 * Looks up an ID via its object. 63 * @param obj 64 * the object. Can be <code>null</code>. 65 * @return the ID or <code>null</code> if the input was <code>null</code> 66 * or if the library does not contain the object. 67 */ 68 public String getByObj(Object obj) { 69 if (obj == null) { 70 return null; 71 } 72 return reverseLib.get(obj); 73 } 74 75 /** 76 * @return a read-only collection of all objects in the library. 77 */ 78 public Collection<Object> getObjects() { 79 return Collections.unmodifiableCollection(objLib.values()); 80 } 81 }