// ==> if `ctr.gen` = `gen` then they are both equal to G.
// ==> otherwise, we know that either `ctr.gen` > G, `gen` < G,
// or both
- if ((ctr.gen == gen) && ct.nonReadOnly()) {
+ if (ctr.gen == gen && !ct.isReadOnly()) {
// try to commit
if (m.CAS_PREV(prev, null)) {
return m;
private Object cleanReadOnly(final TNode<K, V> tn, final int lev, final INode<K, V> parent,
final TrieMap<K, V> ct, final K k, final int hc) {
- if (ct.nonReadOnly()) {
- // used to be throw RestartException
- clean(parent, ct, lev - 5);
- return RESTART;
- }
+ if (ct.isReadOnly()) {
+ if (tn.hc == hc && ct.equal(tn.k, k)) {
+ return tn.v;
+ }
- if (tn.hc == hc && ct.equal(tn.k, k)) {
- return tn.v;
+ return null;
}
- return null;
+ // used to be throw RestartException
+ clean(parent, ct, lev - 5);
+ return RESTART;
}
/**
*/
package org.opendaylight.yangtools.triemap;
+import com.google.common.base.Preconditions;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
return new INode<>(gen, new CNode<>(gen));
}
- final boolean CAS_ROOT (final Object ov, final Object nv) {
- if (isReadOnly()) {
- throw new IllegalStateException("Attempted to modify a read-only snapshot");
- }
+ final boolean CAS_ROOT(final Object ov, final Object nv) {
+ Preconditions.checkState(!readOnly, "Attempted to modify a read-only snapshot");
return ROOT_UPDATER.compareAndSet (this, ov, nv);
}
* otherwise. Used by Map-type methods for quick check.
*/
private void ensureReadWrite() {
- if (isReadOnly()) {
+ if (readOnly) {
throw new UnsupportedOperationException("Attempted to modify a read-only view");
}
}
return readOnly;
}
- boolean nonReadOnly() {
- return !readOnly;
- }
-
/* public methods */
/**
*/
public TrieMap<K, V> readOnlySnapshot() {
// Is it a snapshot of a read-only snapshot?
- if (isReadOnly()) {
+ if (readOnly) {
return this;
}
*
* @return
*/
- Iterator<Entry<K, V>> iterator () {
- if (!nonReadOnly()) {
- return readOnlySnapshot().readOnlyIterator();
- }
-
- return new TrieMapIterator<> (0, this);
+ Iterator<Entry<K, V>> iterator() {
+ return readOnly ? new TrieMapReadOnlyIterator<>(0, this) : new TrieMapIterator<>(0, this);
}
/***
*
* @return
*/
- Iterator<Entry<K, V>> readOnlyIterator () {
- if (nonReadOnly()) {
- return readOnlySnapshot().readOnlyIterator();
- }
-
- return new TrieMapReadOnlyIterator<>(0, this);
+ Iterator<Entry<K, V>> readOnlyIterator() {
+ return new TrieMapReadOnlyIterator<>(0, readOnly ? this : readOnlySnapshot());
}
private int cachedSize() {
@Override
public int size() {
- if (nonReadOnly()) {
- return readOnlySnapshot().size ();
- }
-
- return cachedSize();
+ return readOnly ? cachedSize() : readOnlySnapshot().size();
}
@Override
outputStream.defaultWriteObject();
final Map<K, V> ro = readOnlySnapshot();
- outputStream.writeBoolean(isReadOnly());
+ outputStream.writeBoolean(readOnly);
outputStream.writeInt(ro.size());
for (Entry<K, V> e : ro.entrySet()) {