public Transaction.Result doTransaction(MutableData mutableData) { Post post = mutableData.getValue(Post.class); if (post != null) { // Update the starCount to be the same as the number of members in the stars map. if (post.stars != null) { post.starCount = post.stars.size(); } else { post.starCount = 0; } mutableData.setValue(post); return Transaction.success(mutableData); } else { return Transaction.success(mutableData); } }
/** For Repo to create MutableData objects. */ public static MutableData createMutableData(Node node) { return new MutableData(node); }
/** * @param resultData The desired data at the location * @return A {@link Result} indicating the new data to be stored at the location */ public static Result success(MutableData resultData) { return new Result(true, resultData.getNode()); }
private MutableData(SnapshotHolder holder, Path path) { this.holder = holder; prefixPath = path; ValidationPath.validateWithObject(prefixPath, getValue()); }
/** * @return The number of immediate children at this location */ public long getChildrenCount() { return getNode().getChildCount(); }
/** * Used to obtain a MutableData instance that encapsulates the data and priority at the given * relative path. * * @param path A relative path * @return An instance encapsulating the data and priority at the given path */ public MutableData child(String path) { Validation.validatePathString(path); return new MutableData(holder, prefixPath.child(new Path(path))); }
/** Returns true if the data at this location has children, and false otherwise. */ public boolean hasChildren() { Node node = getNode(); return !node.isLeafNode() && !node.isEmpty(); }
/** * Gets the current priority at this location. The possible return types are: * * <ul> * <li>Double * <li>String * </ul> * * <p>Note that null is allowed. * * @return The priority at this location as a native type */ public Object getPriority() { return getNode().getPriority().getValue(); }
/** * getValue() returns the data contained in this instance as native types. The possible types * returned are: * * <ul> * <li>Boolean * <li>String * <li>Long * <li>Double * <li>Map<String, Object> * <li>List<Object> * </ul> * * <p>This list is recursive; the possible types for <code>Object</code> in the above list is * given by the same list. These types correspond to the types available in JSON. * * @return The data contained in this instance as native types, or null if there is no data at * this location. */ @Nullable public Object getValue() { return getNode().getValue(); }
/** * Sets the priority at this location * * @param priority The desired priority */ public void setPriority(Object priority) { holder.update(prefixPath, getNode().updatePriority( PriorityUtilities.parsePriority(prefixPath, priority))); }
/** * @param path A relative path * @return True if data exists at the given path, otherwise false */ public boolean hasChild(String path) { return !getNode().getChild(new Path(path)).isEmpty(); }
/** * Due to the way that Java implements generics, it takes an extra step to get back a * properly-typed Collection. So, in the case where you want a <code>List</code> of Message * instances, you will need to do something like the following: * * <pre><code> * GenericTypeIndicator<List<Message>> t = * new GenericTypeIndicator<List<Message>>() {}; * List<Message> messages = mutableData.getValue(t); * </code></pre> * * <p>It is important to use a subclass of {@link GenericTypeIndicator}. See {@link * GenericTypeIndicator} for more details * * @param t A subclass of {@link GenericTypeIndicator} indicating the type of generic collection * to be returned. * @param <T> The type to return. Implicitly defined from the {@link GenericTypeIndicator} passed * in * @return A properly typed collection, populated with the data from this instance, or null if * there is no data at this location. */ @Nullable public <T> T getValue(GenericTypeIndicator<T> t) { Object value = getNode().getValue(); return CustomClassMapper.convertToCustomClass(value, t); }
Object value = getNode().getValue(); return CustomClassMapper.convertToCustomClass(value, valueType);
Node node = getNode(); if (node.isEmpty() || node.isLeafNode()) { return new Iterable<MutableData>() {