Skip to content

Instantly share code, notes, and snippets.

@hakusaro
Created May 12, 2018 02:32
Show Gist options
  • Save hakusaro/b244f06cc49b3b2ab8b3b5ff7c222af0 to your computer and use it in GitHub Desktop.
Save hakusaro/b244f06cc49b3b2ab8b3b5ff7c222af0 to your computer and use it in GitHub Desktop.
This is the worst data structure ever
package uwyo.cs.acm2013.dao.model;
import java.util.HashMap;
import java.util.Map;
// import com.fasterxml.jackson.annotation.JsonAutoDetect;
// @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Compliance {
public static final String TIER1 = "tier1";
public static final String TIER2 = "tier2";
public Map<String, Map<String, Object>> compliance_map;
public Compliance(Map<String, Map<String, Object>> compliance_map) {
super();
this.compliance_map = compliance_map;
}
public Compliance() {
this(new HashMap<String, Map<String,Object>> ());
}
private void _setCompliance(String unitCode, String outcomeCode, Object level) {
Map<String,Object> map = compliance_map.get(unitCode);
if (map == null) {
map = new HashMap<String,Object> ();
compliance_map.put(unitCode, map);
}
map.put(outcomeCode, level);
}
private Object _getCompliance(String unitCode, String outcomeCode) {
Map<String,Object> map = compliance_map.get(unitCode);
if (map == null)
return null;
return map.get(outcomeCode);
}
private void _removeCompliance(String unitCode, String outcomeCode) {
Map<String,Object> map = compliance_map.get(unitCode);
if (map == null)
return;
if (!map.containsKey(unitCode))
return;
map.remove(outcomeCode);
if (map.isEmpty())
compliance_map.remove(unitCode);
}
public void setComplianceTier(String unitCode, int tier, int hours) {
assert tier == 1 || tier == 2;
String key = (tier == 1 ? TIER1 : TIER2);
if (hours > 0)
_setCompliance(unitCode, key, hours);
else
_removeCompliance(unitCode, key);
}
public void setComplianceOutcome(String unitCode, String outcomeCode, String level) {
if (!LearningOutcome.NONE.equals(level))
_setCompliance(unitCode, outcomeCode, level);
else
_removeCompliance(unitCode, outcomeCode);
}
public int getComplianceTier(String unitCode, int tier) {
assert tier == 1 || tier == 2;
String key = (tier == 1 ? TIER1 : TIER2);
Object level = _getCompliance(unitCode, key);
if (level == null)
return 0;
return (int) level;
}
public String getComplianceOutcome(String unitCode, String outcomeCode) {
Object level = _getCompliance(unitCode, outcomeCode);
if (level == null)
return LearningOutcome.NONE;
return (String) level;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment