All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.protobuf.TextFormatParseInfoTree Maven / Gradle / Ivy

Go to download

Core Protocol Buffers library. Protocol Buffers are a way of encoding structured data in an efficient yet extensible format.

The newest version!
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

package com.google.protobuf;

import com.google.protobuf.Descriptors.FieldDescriptor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Data structure which is populated with the locations of each field value parsed from the text.
 *
 * 

The locations of primary fields values are retrieved by {@code getLocation} or {@code * getLocations}. The locations of sub message values are within nested {@code * TextFormatParseInfoTree}s and are retrieve by {@code getNestedTree} or {@code getNestedTrees}. * *

The {@code TextFormatParseInfoTree} is created by a Builder. */ public class TextFormatParseInfoTree { // Defines a mapping between each field's descriptor to the list of locations where // its value(s) were was encountered. private Map> locationsFromField; // Defines a mapping between a field's descriptor to a list of TextFormatParseInfoTrees for // sub message location information. Map> subtreesFromField; /** * Construct a {@code TextFormatParseInfoTree}. * * @param locationsFromField a map of fields to location in the source code * @param subtreeBuildersFromField a map of fields to parse tree location information builders */ private TextFormatParseInfoTree( Map> locationsFromField, Map> subtreeBuildersFromField) { // The maps are unmodifiable. The values in the maps are unmodifiable. Map> locs = new HashMap>(); for (Entry> kv : locationsFromField.entrySet()) { locs.put(kv.getKey(), Collections.unmodifiableList(kv.getValue())); } this.locationsFromField = Collections.unmodifiableMap(locs); Map> subs = new HashMap>(); for (Entry> kv : subtreeBuildersFromField.entrySet()) { List submessagesOfField = new ArrayList(); for (Builder subBuilder : kv.getValue()) { submessagesOfField.add(subBuilder.build()); } subs.put(kv.getKey(), Collections.unmodifiableList(submessagesOfField)); } this.subtreesFromField = Collections.unmodifiableMap(subs); } /** * Retrieve all the locations of a field. * * @param fieldDescriptor the {@link FieldDescriptor} of the desired field * @return a list of the locations of values of the field. If there are not values or the field * doesn't exist, an empty list is returned. */ public List getLocations(final FieldDescriptor fieldDescriptor) { List result = locationsFromField.get(fieldDescriptor); return (result == null) ? Collections.emptyList() : result; } /** * Get the location in the source of a field's value. * *

Returns the {@link TextFormatParseLocation} for index-th value of the field in the parsed * text. * * @param fieldDescriptor the {@link FieldDescriptor} of the desired field * @param index the index of the value. * @return the {@link TextFormatParseLocation} of the value * @throws IllegalArgumentException index is out of range */ public TextFormatParseLocation getLocation(final FieldDescriptor fieldDescriptor, int index) { return getFromList(getLocations(fieldDescriptor), index, fieldDescriptor); } /** * Retrieve a list of all the location information trees for a sub message field. * * @param fieldDescriptor the {@link FieldDescriptor} of the desired field * @return A list of {@link TextFormatParseInfoTree} */ public List getNestedTrees(final FieldDescriptor fieldDescriptor) { List result = subtreesFromField.get(fieldDescriptor); return result == null ? Collections.emptyList() : result; } /** * Returns the parse info tree for the given field, which must be a message type. * * @param fieldDescriptor the {@link FieldDescriptor} of the desired sub message * @param index the index of message value. * @return the {@code ParseInfoTree} of the message value. {@code null} is returned if the field * doesn't exist or the index is out of range. * @throws IllegalArgumentException if index is out of range */ public TextFormatParseInfoTree getNestedTree(final FieldDescriptor fieldDescriptor, int index) { return getFromList(getNestedTrees(fieldDescriptor), index, fieldDescriptor); } /** * Create a builder for a {@code ParseInfoTree}. * * @return the builder */ public static Builder builder() { return new Builder(); } private static T getFromList(List list, int index, FieldDescriptor fieldDescriptor) { if (index >= list.size() || index < 0) { throw new IllegalArgumentException( String.format( "Illegal index field: %s, index %d", fieldDescriptor == null ? "" : fieldDescriptor.getName(), index)); } return list.get(index); } /** Builder for a {@link TextFormatParseInfoTree}. */ public static class Builder { private Map> locationsFromField; // Defines a mapping between a field's descriptor to a list of ParseInfoTrees builders for // sub message location information. private Map> subtreeBuildersFromField; /** Create a root level {@ParseInfoTree} builder. */ private Builder() { locationsFromField = new HashMap>(); subtreeBuildersFromField = new HashMap>(); } /** * Record the starting location of a single value for a field. * * @param fieldDescriptor the field * @param location source code location information */ public Builder setLocation( final FieldDescriptor fieldDescriptor, TextFormatParseLocation location) { List fieldLocations = locationsFromField.get(fieldDescriptor); if (fieldLocations == null) { fieldLocations = new ArrayList(); locationsFromField.put(fieldDescriptor, fieldLocations); } fieldLocations.add(location); return this; } /** * Set for a sub message. * *

A new builder is created for a sub message. The builder that is returned is a new builder. * The return is not the invoked {@code builder.getBuilderForSubMessageField}. * * @param fieldDescriptor the field whose value is the submessage * @return a new Builder for the sub message */ public Builder getBuilderForSubMessageField(final FieldDescriptor fieldDescriptor) { List submessageBuilders = subtreeBuildersFromField.get(fieldDescriptor); if (submessageBuilders == null) { submessageBuilders = new ArrayList(); subtreeBuildersFromField.put(fieldDescriptor, submessageBuilders); } Builder subtreeBuilder = new Builder(); submessageBuilders.add(subtreeBuilder); return subtreeBuilder; } /** * Build the {@code TextFormatParseInfoTree}. * * @return the {@code TextFormatParseInfoTree} */ public TextFormatParseInfoTree build() { return new TextFormatParseInfoTree(locationsFromField, subtreeBuildersFromField); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy