001 package org.hackystat.sensorbase.resource.projects; 002 003 import java.util.ArrayList; 004 import java.util.Collection; 005 import java.util.HashMap; 006 import java.util.List; 007 import java.util.Map; 008 import java.util.Set; 009 010 import org.hackystat.sensorbase.resource.projects.jaxb.Project; 011 012 /** 013 * Helper class to support Project to String mappings. 014 * We need this because I don't know how to define JAXB Project instances 015 * with a custom equals() and hashCode() method such that equal Project instances are 016 * those with the same name and owner. 017 * 018 * @author Philip Johnson 019 */ 020 public class ProjectStringMap { 021 022 /** The internal map. */ 023 private Map<Project, String> project2string = new HashMap<Project, String>(); 024 025 /** 026 * Puts [project, info] into the map, after removing any current project instance from 027 * the map with the same name and owner. 028 * @param project The Project to be added. 029 * @param info The associated String. 030 */ 031 public void put(Project project, String info) { 032 this.remove(project); 033 project2string.put(project, info); 034 } 035 036 /** 037 * Returns the string associated with Project, or null if not found. 038 * @param project The project whose string is to be retrieved. 039 * @return The string, or null if Project is not found in the map. 040 */ 041 public String get(Project project) { 042 String name = project.getName(); 043 String owner = project.getOwner(); 044 for (Project oldProject : this.project2string.keySet()) { 045 if (oldProject.getName().equals(name) && oldProject.getOwner().equals(owner)) { 046 return this.project2string.get(oldProject); 047 } 048 } 049 throw new RuntimeException("Did not find project: " + name + " " + owner); 050 } 051 052 /** 053 * Removes any projects with the same name and owner as Project from this data structure. 054 * @param project A project specifying the projects to be removed by name and owner. 055 */ 056 public void remove(Project project) { 057 String owner = project.getOwner(); 058 String name = project.getName(); 059 List<Project> projectsToRemove = new ArrayList<Project>(); 060 // First see if a Project with this name and owner exists in the internal map. 061 Set<Project> oldProjects = this.project2string.keySet(); 062 for (Project oldProject : oldProjects) { 063 if (oldProject.getName().equals(name) && oldProject.getOwner().equals(owner)) { 064 projectsToRemove.add(oldProject); 065 } 066 } 067 // If we found any, get rid of them. 068 for (Project projectToRemove : projectsToRemove) { 069 project2string.remove(projectToRemove); 070 } 071 } 072 073 /** 074 * Returns the strings in this map as a Collection. 075 * @return The strings as a collection. 076 */ 077 public Collection<String> values() { 078 return this.project2string.values(); 079 } 080 }