TextFormatParseInfoTree.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Map.Entry;
40 
51 
52  // Defines a mapping between each field's descriptor to the list of locations where
53  // its value(s) were was encountered.
54  private Map<FieldDescriptor, List<TextFormatParseLocation>> locationsFromField;
55 
56  // Defines a mapping between a field's descriptor to a list of TextFormatParseInfoTrees for
57  // sub message location information.
58  Map<FieldDescriptor, List<TextFormatParseInfoTree>> subtreesFromField;
59 
67  Map<FieldDescriptor, List<TextFormatParseLocation>> locationsFromField,
68  Map<FieldDescriptor, List<TextFormatParseInfoTree.Builder>> subtreeBuildersFromField) {
69 
70  // The maps are unmodifiable. The values in the maps are unmodifiable.
71  Map<FieldDescriptor, List<TextFormatParseLocation>> locs =
72  new HashMap<FieldDescriptor, List<TextFormatParseLocation>>();
73  for (Entry<FieldDescriptor, List<TextFormatParseLocation>> kv : locationsFromField.entrySet()) {
74  locs.put(kv.getKey(), Collections.unmodifiableList(kv.getValue()));
75  }
76  this.locationsFromField = Collections.unmodifiableMap(locs);
77 
78  Map<FieldDescriptor, List<TextFormatParseInfoTree>> subs =
79  new HashMap<FieldDescriptor, List<TextFormatParseInfoTree>>();
80  for (Entry<FieldDescriptor, List<Builder>> kv : subtreeBuildersFromField.entrySet()) {
81  List<TextFormatParseInfoTree> submessagesOfField = new ArrayList<TextFormatParseInfoTree>();
82  for (Builder subBuilder : kv.getValue()) {
83  submessagesOfField.add(subBuilder.build());
84  }
85  subs.put(kv.getKey(), Collections.unmodifiableList(submessagesOfField));
86  }
87  this.subtreesFromField = Collections.unmodifiableMap(subs);
88  }
89 
97  public List<TextFormatParseLocation> getLocations(final FieldDescriptor fieldDescriptor) {
98  List<TextFormatParseLocation> result = locationsFromField.get(fieldDescriptor);
99  return (result == null) ? Collections.<TextFormatParseLocation>emptyList() : result;
100  }
101 
113  public TextFormatParseLocation getLocation(final FieldDescriptor fieldDescriptor, int index) {
114  return getFromList(getLocations(fieldDescriptor), index, fieldDescriptor);
115  }
116 
123  public List<TextFormatParseInfoTree> getNestedTrees(final FieldDescriptor fieldDescriptor) {
124  List<TextFormatParseInfoTree> result = subtreesFromField.get(fieldDescriptor);
125  return result == null ? Collections.<TextFormatParseInfoTree>emptyList() : result;
126  }
127 
137  public TextFormatParseInfoTree getNestedTree(final FieldDescriptor fieldDescriptor, int index) {
138  return getFromList(getNestedTrees(fieldDescriptor), index, fieldDescriptor);
139  }
140 
146  public static Builder builder() {
147  return new Builder();
148  }
149 
150  private static <T> T getFromList(List<T> list, int index, FieldDescriptor fieldDescriptor) {
151  if (index >= list.size() || index < 0) {
152  throw new IllegalArgumentException(
153  String.format(
154  "Illegal index field: %s, index %d",
155  fieldDescriptor == null ? "<null>" : fieldDescriptor.getName(), index));
156  }
157  return list.get(index);
158  }
159 
161  public static class Builder {
162 
163  private Map<FieldDescriptor, List<TextFormatParseLocation>> locationsFromField;
164 
165  // Defines a mapping between a field's descriptor to a list of ParseInfoTrees builders for
166  // sub message location information.
167  private Map<FieldDescriptor, List<Builder>> subtreeBuildersFromField;
168 
170  private Builder() {
171  locationsFromField = new HashMap<FieldDescriptor, List<TextFormatParseLocation>>();
172  subtreeBuildersFromField = new HashMap<FieldDescriptor, List<Builder>>();
173  }
174 
182  final FieldDescriptor fieldDescriptor, TextFormatParseLocation location) {
183  List<TextFormatParseLocation> fieldLocations = locationsFromField.get(fieldDescriptor);
184  if (fieldLocations == null) {
185  fieldLocations = new ArrayList<TextFormatParseLocation>();
186  locationsFromField.put(fieldDescriptor, fieldLocations);
187  }
188  fieldLocations.add(location);
189  return this;
190  }
191 
201  public Builder getBuilderForSubMessageField(final FieldDescriptor fieldDescriptor) {
202  List<Builder> submessageBuilders = subtreeBuildersFromField.get(fieldDescriptor);
203  if (submessageBuilders == null) {
204  submessageBuilders = new ArrayList<Builder>();
205  subtreeBuildersFromField.put(fieldDescriptor, submessageBuilders);
206  }
207  Builder subtreeBuilder = new Builder();
208  submessageBuilders.add(subtreeBuilder);
209  return subtreeBuilder;
210  }
211 
219  }
220  }
221 }
com.google.protobuf.Descriptors
Definition: Descriptors.java:80
Map
struct Map Map
Definition: php/ext/google/protobuf/protobuf.h:648
com.google.protobuf.TextFormatParseInfoTree.getLocation
TextFormatParseLocation getLocation(final FieldDescriptor fieldDescriptor, int index)
Definition: TextFormatParseInfoTree.java:113
com.google.protobuf.TextFormatParseInfoTree.getNestedTrees
List< TextFormatParseInfoTree > getNestedTrees(final FieldDescriptor fieldDescriptor)
Definition: TextFormatParseInfoTree.java:123
com.google.protobuf.TextFormatParseInfoTree.builder
static Builder builder()
Definition: TextFormatParseInfoTree.java:146
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
com.google.protobuf.TextFormatParseInfoTree.Builder.subtreeBuildersFromField
Map< FieldDescriptor, List< Builder > > subtreeBuildersFromField
Definition: TextFormatParseInfoTree.java:167
T
#define T(upbtypeconst, upbtype, ctype, default_value)
com.google.protobuf.TextFormatParseInfoTree.getFromList
static< T > T getFromList(List< T > list, int index, FieldDescriptor fieldDescriptor)
Definition: TextFormatParseInfoTree.java:150
com.google.protobuf.TextFormatParseInfoTree.getNestedTree
TextFormatParseInfoTree getNestedTree(final FieldDescriptor fieldDescriptor, int index)
Definition: TextFormatParseInfoTree.java:137
com.google.protobuf.TextFormatParseLocation
Definition: TextFormatParseLocation.java:40
location
GLint location
Definition: glcorearb.h:3074
com.google.protobuf.TextFormatParseInfoTree.Builder.getBuilderForSubMessageField
Builder getBuilderForSubMessageField(final FieldDescriptor fieldDescriptor)
Definition: TextFormatParseInfoTree.java:201
java
com.google.protobuf.TextFormatParseInfoTree.locationsFromField
Map< FieldDescriptor, List< TextFormatParseLocation > > locationsFromField
Definition: TextFormatParseInfoTree.java:54
com.google.protobuf.TextFormatParseInfoTree.Builder.Builder
Builder()
Definition: TextFormatParseInfoTree.java:170
com.google.protobuf.TextFormatParseInfoTree
Definition: TextFormatParseInfoTree.java:50
com.google.protobuf.TextFormatParseInfoTree.getLocations
List< TextFormatParseLocation > getLocations(final FieldDescriptor fieldDescriptor)
Definition: TextFormatParseInfoTree.java:97
com.google
com
com.google.protobuf.TextFormatParseInfoTree.Builder.setLocation
Builder setLocation(final FieldDescriptor fieldDescriptor, TextFormatParseLocation location)
Definition: TextFormatParseInfoTree.java:181
com.google.protobuf.Descriptors.FieldDescriptor.getName
String getName()
Definition: Descriptors.java:968
com.google.protobuf.TextFormatParseInfoTree.Builder
Definition: TextFormatParseInfoTree.java:161
Builder
struct Builder Builder
Definition: ruby/ext/google/protobuf_c/protobuf.h:67
index
GLuint index
Definition: glcorearb.h:3055
com.google.protobuf.TextFormatParseInfoTree.TextFormatParseInfoTree
TextFormatParseInfoTree(Map< FieldDescriptor, List< TextFormatParseLocation >> locationsFromField, Map< FieldDescriptor, List< TextFormatParseInfoTree.Builder >> subtreeBuildersFromField)
Definition: TextFormatParseInfoTree.java:66
com.google.protobuf.TextFormatParseInfoTree.Builder.build
TextFormatParseInfoTree build()
Definition: TextFormatParseInfoTree.java:217
com.google.protobuf.Descriptors.FieldDescriptor
Definition: Descriptors.java:949
com.google.protobuf.TextFormatParseInfoTree.Builder.locationsFromField
Map< FieldDescriptor, List< TextFormatParseLocation > > locationsFromField
Definition: TextFormatParseInfoTree.java:163


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:00