package com.perforce.client.api; import java.util.*; /** * Field/value manipulation. * * The StrDict class provides a dictionary object of StrPtrs with a simple * Get/Put interface. This class contains abstract methods and therefore * cannot be instantiated, but its subclasses adhere to the basic interface * documented here. * * <p> ClientApi is a descendant of StrDict; most notably, the StrDict::SetArgv() * method is used to set the arguments to a Perforce command before executing it * with ClientApi::Run(). * * <p> The ClientUser::OutputStat() method takes a StrDict as an argument; the * StrDict methods are therefore necessary to process data with OutputStat(). * Note that pulling information from a StrDict is typically easier than trying * to parse the text given to OutputInfo(). */ public class StrDict implements Map<String, String> { final long instance; private final boolean javaCreatedTheInstance; StrDict(long instance) { this.instance = instance; javaCreatedTheInstance = false; } protected void finalize() throws Throwable { try { super.finalize(); } finally { if (javaCreatedTheInstance) { nDeleteInstance(this.instance); } } } private static native void nDeleteInstance(long instance); private static native String nGetVar(long instance, String name); private static native String nGetName(long instance, int index); private static native void nRemoveVar(long instance, String name); private static native void nSetVar(long instance, String name, String val); private static final class VarIterator implements Iterator<String> { private int currentIndex = 0; private String nextVar = null; private final long strDict; public VarIterator(StrDict sd) { strDict = sd.instance; nextVar = nGetName(sd.instance, currentIndex++); } public boolean hasNext() { return nextVar != null; } public String next() { try { return nextVar; } finally { nextVar = nGetName(strDict, currentIndex++); } } public void remove() { if (nextVar == null) throw new IllegalStateException(); nRemoveVar(strDict, nextVar); nextVar = null; } } private static final class DictEntry implements Map.Entry<String,String> { private String entryVar; private String entryVal; DictEntry(String var, String val) { entryVar = var; entryVal = val; } public String getKey() { return entryVar;} public String getValue() {return entryVal;} public String setValue(String value) { String old = entryVal; entryVal = value; return old; } } private static final class EntryIterator implements Iterator<DictEntry> { private int currentIndex; private String nextVar; private final long strDict; EntryIterator(StrDict sd) { strDict = sd.instance; nextVar = nGetName(sd.instance, currentIndex++); } public boolean hasNext() { return nextVar != null; } public synchronized DictEntry next() { if (nextVar == null) throw new NoSuchElementException(); try { return new DictEntry(nextVar, nGetVar(strDict, nextVar)); } finally { nextVar = nGetName(strDict, currentIndex++); } } public synchronized void remove() { if (nextVar == null) throw new IllegalStateException(); nRemoveVar(strDict, nextVar); nextVar = null; } } public synchronized void clear() { Iterator<String> vari = new VarIterator(this); while (vari.hasNext()) { String var = vari.next(); remove(var); } } public synchronized boolean containsKey(Object var) { Iterator vari = new VarIterator(this); while (vari.hasNext()) { if (var.equals(vari.next())) return true; } return false; } public synchronized boolean containsValue(Object value) { EntryIterator enti = new EntryIterator(this); while (enti.hasNext()) { if (value.equals(enti.next().getValue())) return true; } return false; } public Set<Map.Entry<String, String>> entrySet() { Set<Map.Entry<String, String>> ents = new HashSet<Entry<String, String>>(); EntryIterator enti = new EntryIterator(this); while (enti.hasNext()) { ents.add(enti.next()); } return ents; } public boolean equals(Object x) { try { return equals((StrDict) x); } catch (ClassCastException e) { return false; } } public final boolean equals(StrDict sd) { return this.instance == sd.instance; } public String get(Object var) { return nGetVar(this.instance, var.toString()); } public int hashCode() { return (int)(this.instance ^ (this.instance >>> 32)); } public synchronized boolean isEmpty() { return !(new VarIterator(this)).hasNext(); } public Set<String> keySet() { Set<String> kset = new HashSet<String>(); Iterator<String> vari = new VarIterator(this); while (vari.hasNext()) { kset.add(vari.next()); } return kset; } public synchronized String put(String var, String value) { nSetVar(this.instance, var, value); return value; } public synchronized void putAll(Map<? extends String, ? extends String> m) { Iterator<? extends Map.Entry<? extends String, ? extends String>> i = m.entrySet().iterator(); while (i.hasNext()) { Map.Entry<? extends String, ? extends String> me = i.next(); nSetVar(this.instance, me.getKey(), me.getValue()); } } public synchronized String remove(Object key) { String var = key.toString(); String old = nGetVar(this.instance, var); nRemoveVar(this.instance, var); return old; } public synchronized int size() { return keySet().size(); } public Collection<String> values() { List<String> values = new LinkedList<String>(); Iterator<String> vari = new VarIterator(this); while (vari.hasNext()) { values.add(nGetVar(this.instance, vari.next())); } return values; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 4181 | Paul Krause |
Rename //guest/paul_krause/perforce/api/java/wcvm/com/... //guest/paul_krause/perforce/api/java/wcvm/javax/... To //guest/paul_krause/perforce/api/java/wcvm/src-15/... |
||
//guest/paul_krause/perforce/api/java/wcvm/com/perforce/client/api/StrDict.java | |||||
#6 | 4177 | Paul Krause | parameterize container types - requires tiger or pizza | ||
#5 | 4130 | Paul Krause | Copy Javadoc from Perforce 2003.2 C/C++ API User's Guide. | ||
#4 | 4120 | Paul Krause | serialization fixes | ||
#3 | 4108 | Paul Krause | implement missing Map methods | ||
#2 | 4100 | Paul Krause | fix type problems | ||
#1 | 4073 | Paul Krause | branch com.perforce.api package from michael_bishop | ||
//guest/michael_bishop/P4APIForJava/java/com/perforce/client/api/StrDict.java | |||||
#1 | 430 | Michael Bishop |
Initial checkin. Seems to work. Not very much testing. Not very much documentation. Some more commenting needs to take place. But, it's there to experiment with. |