Created
September 5, 2014 16:29
-
-
Save tkroman/349f95557159240e9ea0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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