All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openehr.terminology.SimpleTerminologyAccess Maven / Gradle / Ivy

The newest version!
/*
 * component:   "openEHR Reference Implementation"
 * description: "Class SimpleTerminologyAccess"
 * keywords:    "terminology"
 *
 * author:      "Rong Chen "
 * copyright:   "Copyright (c) 2007 Rong Chen"
 * license:     "See notice at bottom of class"
 *
 * file:        "$URL$"
 * revision:    "$LastChangedRevision$"
 * last_change: "$LastChangedDate$"
 */
package org.openehr.terminology;

import java.util.*;

import org.openehr.rm.datatypes.text.CodePhrase;
import org.openehr.rm.support.terminology.TerminologyAccess;

/**
 * Simple in-memory implementation of a terminology access
 * 
 * @author Rong Chen
 */
public class SimpleTerminologyAccess implements TerminologyAccess {
	
	/**
	 * Creates an simple terminology access
	 * 
	 * @param id
	 */
	SimpleTerminologyAccess(String id) {
		this.id = id;
		this.groups = new HashMap>();
		this.groupLangNameToId = new HashMap>();
		this.codeRubrics = new HashMap>();
	}
	
	static SimpleTerminologyAccess getInstance(String id) {
		return new SimpleTerminologyAccess(id);
	}
	
	/**
	 * Adds a group of codes with language dependent names
	 * 
	 * @param groupId	not null  
	 * @param codes     not null
	 * @param names     not null
	 */
	void addGroup(String groupId, Collection codes, 
			Map names) {
		
		Set group = new HashSet();
		for(String c : codes) {
			CodePhrase code = new CodePhrase(id, c);
			group.add(code);
		}
		groups.put(groupId, group);
		for(Map.Entry entry : names.entrySet()) {
			String lang = entry.getKey();
			Map nameToId = groupLangNameToId.get(lang);
			if(nameToId == null) {
				nameToId = new HashMap();
			}
			String name = entry.getValue();
			
			nameToId.put(name, groupId);
			groupLangNameToId.put(lang, nameToId);
		}	
	}	
	
	/**
	 * Adds a rubric for given language and code
	 * 
	 * @param lang
	 * @param code
	 * @param rubric
	 */
	void addRubric(String lang, String code, String rubric) {
		Map map = codeRubrics.get(lang);
		if(map == null) {
			map = new HashMap();
			codeRubrics.put(lang, map);
		}
		map.put(code, rubric);
	}
	
	/**
	 * @returns "openehr"
	 */
	public String id() {
		return id;
	}

	public Set allCodes() {
		Set allCodes = new HashSet();
		for(Set codes : groups.values()) {
			allCodes.addAll(codes);
		}
		return allCodes;
	}

	public Set codesForGroupId(String groupID) {
		return groups.get(groupID); 
	}

	public Set codesForGroupName(String name, String language) {
		Map map = groupLangNameToId.get(language);
		if(map == null) {
			return null;
		}
		String groupId = map.get(name);
		return groups.get(groupId);
	}

	public String rubricForCode(String code, String language) {
		Map map = codeRubrics.get(language);
		if(map == null) {
			return null;			
		}
		return map.get(code);
	}
	
	public boolean hasCodeForGroupId(String groupId, CodePhrase code) {
		Set group = groups.get(groupId);
		if(group == null) {
			return false;
		}
		return group.contains(code);
	}

	/*
	 * Id of this terminology
	 */	
	private final String id;
	
	/*
	 * Groups indexed by group id
	 * 
	 */
	private final Map> groups;
	
	/**
	 * GroupIds indexed by language and group name
	 * >
	 */
	private final Map> groupLangNameToId; 
	
	/**
	 * Code rubrics indexed by lang, code
	 */
	private final Map> codeRubrics;	
 }
/*
 *  ***** BEGIN LICENSE BLOCK *****
 *  Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 *  The contents of this file are subject to the Mozilla Public License Version
 *  1.1 (the 'License'); you may not use this file except in compliance with
 *  the License. You may obtain a copy of the License at
 *  http://www.mozilla.org/MPL/
 *
 *  Software distributed under the License is distributed on an 'AS IS' basis,
 *  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 *  for the specific language governing rights and limitations under the
 *  License.
 *
 *  The Original Code is SimpleTerminologyAccess.java
 *
 *  The Initial Developer of the Original Code is Rong Chen.
 *  Portions created by the Initial Developer are Copyright (C) 2007
 *  the Initial Developer. All Rights Reserved.
 *
 *  Contributor(s):
 *
 * Software distributed under the License is distributed on an 'AS IS' basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 *  ***** END LICENSE BLOCK *****
 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy