Skip to content

Instantly share code, notes, and snippets.

@tkroman
Created September 5, 2014 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkroman/349f95557159240e9ea0 to your computer and use it in GitHub Desktop.
Save tkroman/349f95557159240e9ea0 to your computer and use it in GitHub Desktop.
public UnsignedLong incrementProperty(final UnsignedLong id, final String propKey, final UnsignedLong incrementAmount) {
return changePropValue(id, propKey, oldProp -> oldProp.plus(incrementAmount));
}
public UnsignedLong decrementProperty(final UnsignedLong id, final String propKey, final UnsignedLong decrementAmount) {
return changePropValue(id, propKey, oldProp -> oldProp.minus(decrementAmount));
}
public UnsignedLong getProperty(final UnsignedLong id, final String propKey) {
return changePropValue(id, propKey, Function.identity());
}
public UnsignedLong setProperty(final UnsignedLong id, final String propKey, final UnsignedLong newValue) {
return changePropValue(id, propKey, oldProp -> newValue);
}
private UnsignedLong changePropValue(final UnsignedLong id, final String propKey, final Function<UnsignedLong, UnsignedLong> updater) {
try (final Transaction tx = namespaces.db().beginTx()) {
final BushNode nsNode = namespaces.getNodeById(id.longValue());
final UnsignedLong propValue = updater.apply(UnsignedLong.valueOf(nsNode.<Long>getProperty(propKey).orElse(0l)));
if (!updater.equals(Function.identity())) {
nsNode.setProperty(propKey, propValue);
}
tx.success();
return propValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment