package com.perforce.p4java_ext; import java.io.IOException; import java.io.StringReader; import java.util.*; /** * TODO remove: it's not needed */ public class SpecUtils { public static void appendValues(Map<String, String> map, StringBuilder sb) { for (Map.Entry<String, String> ent : map.entrySet()) { if (!ent.getValue().contains("\n")) { // Single line sb.append(ent.getKey()).append(": ").append(ent.getValue()).append("\n\n"); } else { // Multi line sb.append(ent.getKey()).append(":\n"); for (String line : ent.getValue().split("\n")) { sb.append("\t").append(line).append("\n"); } sb.append("\n"); } } } /** * Split out possibly double-quote strings. Things are often whitespace * delimited in specs, with the option to double-quote strings. * * @param line * @return null if there's an issue, otheriwse, a list of delimited strings */ public static List<String> splitLine(String line) { List<String> tokens = new ArrayList<String>(); try { StringReader reader = new StringReader(line); String tok; while (!(tok = nextToken(reader)).isEmpty()) { tokens.add(tok); } } catch (IOException io) { tokens = null; } return tokens; } private static String nextToken(StringReader reader) throws IOException { StringBuilder tok = new StringBuilder(); boolean inSpace = true; // we skip until this is false boolean inQuote = false; int ch; while ((ch = reader.read()) != -1) { if (!inQuote && Character.isWhitespace(ch)) { if (inSpace) { continue; } else { break; } } if (inSpace) { inSpace = false; } if (ch == '"') { inQuote = !inQuote; } else { tok.append((char)ch); } } return tok.toString(); } /** * Several items need to be encoded as whitespace delimited strings... but * if any part has whitespace, well that too needs to be joined. */ public static String joinAndEnquote(List<String> components) { StringBuilder joined = new StringBuilder(); for (int idx = 0; idx < components.size(); ++idx) { String part = components.get(idx); if (part.contains(" ") || part.contains("\t") || part.contains("\n")) { joined.append('"').append(part).append('"'); } else { joined.append(part); } if (idx < (components.size() - 1)) { joined.append(" "); } } return joined.toString(); } /** * Get all keys that start with a particular prefix, ordered by the "rank" * suffix of each key. * * Many "lists" returned in maps from P4Java use a prefix plus a rank as * the key name. * * @param prefix * @param results * @return */ public static List<String> keys(String prefix, Map<String, Object> results) { SortedSet<KeyRank> ranks = new TreeSet<KeyRank>(new KeyRank.KeyRankComparator()); for (String key : results.keySet()) { if (key.startsWith(prefix)) { String rankStr = key.substring(prefix.length()); int rank = Integer.valueOf(rankStr, 10); ranks.add(new KeyRank(key, rank)); } } List<String> keyList = new ArrayList<String>(ranks.size()); for (KeyRank rank : ranks) { keyList.add(rank.getKey()); } return keyList; } public static class KeyRank { private String key; private int rank; public KeyRank(String key, int rank) { this.key = key; this.rank = rank; } public String getKey() { return key; } public int getRank() { return rank; } public static class KeyRankComparator implements Comparator<KeyRank> { @Override public int compare(KeyRank keyRank, KeyRank keyRank2) { Integer r1 = keyRank.getRank(); Integer r2 = keyRank2.getRank(); return r1.compareTo(r2); } } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; KeyRank keyRank = (KeyRank) o; if (rank != keyRank.rank) return false; if (key != null ? !key.equals(keyRank.key) : keyRank.key != null) return false; return true; } @Override public int hashCode() { int result = key != null ? key.hashCode() : 0; result = 31 * result + rank; return result; } } }
# | 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. |