package com.perforce.p4java_ext; import java.util.*; /** * Wraps the lines of the output of the trigger table. */ public class TriggerSpec { private Map<String, Object> triggerMap; public TriggerSpec(Map<String, Object> triggerMap) { this.triggerMap = triggerMap; } public Map<String, Object> getTriggerMap() { return triggerMap; } public void setTriggerMap(Map<String, Object> triggerMap) { this.triggerMap = triggerMap; } /** * The underlying trigger map contains several entries that are simply * prefixed with "Trigger" and a number. This just returns all those keys. */ public List<String> getTriggerKeys() { return SpecUtils.keys("Triggers", triggerMap); } /** * The returned object map only returns lines. You likely don't need this, * but just want to call getTriggers() that parses the lines. */ public List<String> getTriggerLines() { List<String> lines = new ArrayList<String>(); for (String key : getTriggerKeys()) { lines.add((String)triggerMap.get(key)); } return lines; } public List<Trigger> getTriggers() { List<Trigger> triggers = new ArrayList<Trigger>(); for (String line : getTriggerLines()) { List<String> parts = SpecUtils.splitLine(line); if (parts == null || parts.size() != 4) { throw new IllegalStateException("trigger not defined in 4 " + "parts for line " + line); } Trigger trigger = new Trigger(); trigger.setName(parts.get(0)); trigger.setType(parts.get(1)); trigger.setPath(parts.get(2)); trigger.setCommand(parts.get(3)); triggers.add(trigger); } return triggers; } public void clearTriggers() { for (String key : getTriggerKeys()) { triggerMap.remove(key); } } public void addTrigger(Trigger trigger) { int nextNum = getTriggerKeys().size(); String key = "Triggers" + Integer.toString(nextNum, 10); String value = trigger.toSpecLine(); triggerMap.put(key, value); } /** * Write out the triggerMap as a spec. * * Please note that we are using an input string here due to a * known issue in p4java: P4JAVA-835 job071702 */ public String triggersMapAsSpec() { StringBuilder sb = new StringBuilder(); sb.append("Triggers:\n"); for (String key : getTriggerKeys()) { sb.append("\t").append((String)triggerMap.get(key)).append("\n"); } if (!getTriggerKeys().isEmpty()) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TriggerSpec that = (TriggerSpec) o; if (triggerMap != null ? !triggerMap.equals(that.triggerMap) : that.triggerMap != null) return false; return true; } @Override public int hashCode() { return triggerMap != null ? triggerMap.hashCode() : 0; } @Override public String toString() { return "TriggerSpec{" + "triggerMap=" + triggerMap + '}'; } public static class Trigger { private String name; private String type; private String path; private String command; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getCommand() { return command; } public void setCommand(String command) { this.command = command; } public String toSpecLine() { List<String> parts = new ArrayList<String>(); parts.add(name); parts.add(type); parts.add(path); parts.add(command); return SpecUtils.joinAndEnquote(parts); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Trigger trigger = (Trigger) o; if (command != null ? !command.equals(trigger.command) : trigger.command != null) return false; if (name != null ? !name.equals(trigger.name) : trigger.name != null) return false; if (path != null ? !path.equals(trigger.path) : trigger.path != null) return false; if (type != null ? !type.equals(trigger.type) : trigger.type != null) return false; return true; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + (type != null ? type.hashCode() : 0); result = 31 * result + (path != null ? path.hashCode() : 0); result = 31 * result + (command != null ? command.hashCode() : 0); return result; } @Override public String toString() { return "Trigger{" + "name='" + name + '\'' + ", type='" + type + '\'' + ", path='" + path + '\'' + ", command='" + command + '\'' + '}'; } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 9086 | tjuricek |
0.1.0 Initial release of p4java_ext Mostly just contains enough of the P4 API to build the test data initializer. About 4 commands have minimal testing, so it works. Kind of. |
||
#1 | 9085 | tjuricek | First import of the project, before package refactoring. |