Skip to content

Commit

Permalink
Fix minor issues with new "small" BeanPropertyMap. Next stop: rewrite…
Browse files Browse the repository at this point in the history
… "big" variant.
  • Loading branch information
cowtowncoder committed Mar 21, 2015
1 parent 89a5256 commit 62b1d20
Showing 1 changed file with 96 additions and 30 deletions.
Expand Up @@ -183,13 +183,17 @@ public abstract boolean findDeserializeAndSet(JsonParser p, DeserializationConte
* (note: entry MUST exist; otherwise exception is thrown) with * (note: entry MUST exist; otherwise exception is thrown) with
* specified replacement. * specified replacement.
*/ */
public abstract void replace(SettableBeanProperty property); public void replace(SettableBeanProperty property) {
throw new NoSuchElementException("No entry '"+property.getName()+"' found, can't replace");
}


/** /**
* Specialized method for removing specified existing entry. * Specialized method for removing specified existing entry.
* NOTE: entry MUST exist, otherwise an exception is thrown. * NOTE: entry MUST exist, otherwise an exception is thrown.
*/ */
public abstract void remove(SettableBeanProperty property); public void remove(SettableBeanProperty property) {
throw new NoSuchElementException("No entry '"+property.getName()+"' found, can't remove");
}


/* /*
/********************************************************** /**********************************************************
Expand Down Expand Up @@ -276,19 +280,55 @@ public Small(boolean caseInsensitive, SettableBeanProperty p1, SettableBeanPrope


public Small(boolean caseInsensitive, SettableBeanProperty p1, SettableBeanProperty p2, SettableBeanProperty p3) { public Small(boolean caseInsensitive, SettableBeanProperty p1, SettableBeanProperty p2, SettableBeanProperty p3) {
super(caseInsensitive); super(caseInsensitive);
size = 2; size = 3;
key1 = p1.getName(); key1 = p1.getName();
prop1 = p1; prop1 = p1;
key2 = p2.getName(); key2 = p2.getName();
prop2 = p2; prop2 = p2;
key3 = p3.getName(); key3 = p3.getName();
prop3 = p3; prop3 = p3;
} }

@Override @Override
public BeanPropertyMap withProperty(SettableBeanProperty newProperty) { public BeanPropertyMap withProperty(SettableBeanProperty prop)
// !!! TBI {
throw new UnsupportedOperationException(); final String key = prop.getName();
// First: replace existing one?
switch (size) {
case 3:
if (key.equals(key3)) {
prop3 = prop;
return this;
}
case 2:
if (key.equals(key2)) {
prop2 = prop;
return this;
}
case 1:
if (key.equals(key1)) {
prop1 = prop;
return this;
}
}

// If not, append. Easy if we aren't yet full
switch (size) {
case 2:
return new Small(_caseInsensitive, prop1, prop2, prop);
case 1:
return new Small(_caseInsensitive, prop1, prop);
case 0:
return new Small(_caseInsensitive, prop);
}
// But if we have all 3, "upgrade"
prop.assignIndex(3);
List<SettableBeanProperty> props = new ArrayList<SettableBeanProperty>(4);
props.add(prop1);
props.add(prop2);
props.add(prop3);
props.add(prop);
return new Default(props, _caseInsensitive);
} }


@Override @Override
Expand Down Expand Up @@ -363,16 +403,15 @@ public boolean findDeserializeAndSet(JsonParser p,
key = key.toLowerCase(); key = key.toLowerCase();
} }
SettableBeanProperty prop = find(key); SettableBeanProperty prop = find(key);
if (prop == null) { if (prop != null) {
return false; try {
} prop.deserializeAndSet(p, ctxt, bean);
try { } catch (Exception e) {
prop.deserializeAndSet(p, ctxt, bean); wrapAndThrow(e, bean, key, ctxt);
} catch (Exception e) { }
wrapAndThrow(e, bean, key, ctxt); return true;
} }
return true; return false;

} }


@Override @Override
Expand Down Expand Up @@ -408,13 +447,38 @@ public void replace(SettableBeanProperty prop) {
return; return;
} }
} }
throw new NoSuchElementException("No entry '"+key+"' found, can't replace"); super.replace(prop);
} }


@Override @Override
public void remove(SettableBeanProperty property) { public void remove(SettableBeanProperty prop) {
// !!! TBI final String key = prop.getName();
throw new UnsupportedOperationException(); switch (size) {
case 3:
if (key.equals(key3)) {
prop3 = null;
key3 = null;
size = 2;
return;
}
case 2:
if (key.equals(key2)) {
prop2 = prop3;
key2 = key3;
--size;
return;
}
case 1:
if (key.equals(key1)) {
prop1 = prop2;
key1 = key2;
prop2 = prop3;
key2 = key3;
--size;
return;
}
}
super.remove(prop);
} }
} }


Expand Down Expand Up @@ -556,8 +620,8 @@ public BeanPropertyMap withProperty(SettableBeanProperty newProperty)
// and then insert the new property: // and then insert the new property:
int index = propName.hashCode() & _hashMask; int index = propName.hashCode() & _hashMask;
newBuckets[index] = new Bucket(newBuckets[index], newBuckets[index] = new Bucket(newBuckets[index],
propName, newProperty, _nextBucketIndex++); propName, newProperty, _nextBucketIndex);
return new Default(newBuckets, _size+1, _nextBucketIndex, _caseInsensitive); return new Default(newBuckets, _size+1, _nextBucketIndex+1, _caseInsensitive);
} }
// replace: easy, close + replace // replace: easy, close + replace
BeanPropertyMap newMap = new Default(newBuckets, bcount, _nextBucketIndex, _caseInsensitive); BeanPropertyMap newMap = new Default(newBuckets, bcount, _nextBucketIndex, _caseInsensitive);
Expand Down Expand Up @@ -606,9 +670,9 @@ public void remove(SettableBeanProperty property)
} }


@Override @Override
public void replace(SettableBeanProperty property) public void replace(SettableBeanProperty prop)
{ {
String name = getPropertyName(property); String name = getPropertyName(prop);
int index = name.hashCode() & (_buckets.length-1); int index = name.hashCode() & (_buckets.length-1);


/* This is bit tricky just because buckets themselves /* This is bit tricky just because buckets themselves
Expand All @@ -626,14 +690,16 @@ public void replace(SettableBeanProperty property)
} }
} }
// Not finding specified entry is error, so: // Not finding specified entry is error, so:
if (foundIndex < 0) { if (foundIndex >= 0) {
throw new NoSuchElementException("No entry '"+property+"' found, can't replace"); /* So let's attach replacement in front: useful also because
* it allows replacement even when iterating over entries
*/
_buckets[index] = new Bucket(tail, name, prop, foundIndex);
return;
} }
/* So let's attach replacement in front: useful also because super.replace(prop);
* it allows replacement even when iterating over entries
*/
_buckets[index] = new Bucket(tail, name, property, foundIndex);
} }

@Override @Override
public Iterator<SettableBeanProperty> iterator() { public Iterator<SettableBeanProperty> iterator() {
return new IteratorImpl(_buckets); return new IteratorImpl(_buckets);
Expand Down

0 comments on commit 62b1d20

Please sign in to comment.