com.google.firebase.database.snapshot.ChildrenNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of firebase-admin Show documentation
Show all versions of firebase-admin Show documentation
This is the official Firebase Admin Java SDK. Build extraordinary native JVM apps in
minutes with Firebase. The Firebase platform can power your app’s backend, user
authentication, static hosting, and more.
The newest version!
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.database.snapshot;
import com.google.firebase.database.collection.ImmutableSortedMap;
import com.google.firebase.database.collection.LLRBNode;
import com.google.firebase.database.core.Path;
import com.google.firebase.database.utilities.Utilities;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/** User: greg Date: 5/16/13 Time: 4:47 PM */
public class ChildrenNode implements Node {
public static final Comparator NAME_ONLY_COMPARATOR =
new Comparator() {
@Override
public int compare(ChildKey o1, ChildKey o2) {
return o1.compareTo(o2);
}
};
private final ImmutableSortedMap children;
private final Node priority;
private String lazyHash = null;
protected ChildrenNode() {
this.children = ImmutableSortedMap.Builder.emptyMap(NAME_ONLY_COMPARATOR);
this.priority = PriorityUtilities.NullPriority();
}
protected ChildrenNode(ImmutableSortedMap children, Node priority) {
if (children.isEmpty() && !priority.isEmpty()) {
throw new IllegalArgumentException("Can't create empty ChildrenNode with priority!");
}
this.priority = priority;
this.children = children;
}
private static void addIndentation(StringBuilder builder, int indentation) {
for (int i = 0; i < indentation; i++) {
builder.append(" ");
}
}
@Override
public boolean hasChild(ChildKey name) {
return !this.getImmediateChild(name).isEmpty();
}
@Override
public boolean isEmpty() {
return children.isEmpty();
}
@Override
public int getChildCount() {
return children.size();
}
@Override
public Object getValue() {
return getValue(false);
}
@Override
public Object getValue(boolean useExportFormat) {
if (isEmpty()) {
return null;
}
int numKeys = 0;
int maxKey = 0;
boolean allIntegerKeys = true;
Map result = new HashMap<>();
for (Map.Entry entry : children) {
String key = entry.getKey().asString();
result.put(key, entry.getValue().getValue(useExportFormat));
numKeys++;
// If we already found a string key, don't bother with any of this
if (allIntegerKeys) {
if (key.length() > 1 && key.charAt(0) == '0') {
allIntegerKeys = false;
} else {
Integer keyAsInt = Utilities.tryParseInt(key);
if (keyAsInt != null && keyAsInt >= 0) {
if (keyAsInt > maxKey) {
maxKey = keyAsInt;
}
} else {
allIntegerKeys = false;
}
}
}
}
if (!useExportFormat && allIntegerKeys && maxKey < 2 * numKeys) {
// convert to an array
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy