File: ThriftField.java

package info (click to toggle)
libthrift-java 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,108 kB
  • sloc: java: 13,229; xml: 66; makefile: 6
file content (193 lines) | stat: -rw-r--r-- 5,564 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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 org.apache.thrift.partial;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Holds name of a thrift field and of its sub-fields recursively.
 *
 * <p>This class is meant to be used in conjunction with {@code TDeserializer}.
 */
public class ThriftField {

  /** Name of this field as it appears in a thrift file. Case sensitive. */
  public final String name;

  /**
   * List of sub-fields of this field.
   *
   * <p>This list should have only those sub-fields that need to be deserialized by the {@code
   * TDeserializer}.
   */
  public final List<ThriftField> fields;

  /**
   * Constructs a {@link ThriftField}.
   *
   * @param name the name of this field as it appears in a thrift file. Case sensitive.
   * @param fields List of sub-fields of this field.
   */
  ThriftField(String name, List<ThriftField> fields) {
    Validate.checkNotNullAndNotEmpty(name, "name");
    Validate.checkNotNull(fields, "fields");

    this.name = name;
    this.fields = Collections.unmodifiableList(fields);
  }

  /** Constructs a {@link ThriftField} that does not have any sub-fields. */
  ThriftField(String name) {
    this(name, Collections.emptyList());
  }

  // Internal-only constructor that does not mark fields as read-only.
  // That allows fromNames() to construct fields from names.
  // The actual value of allowFieldAdds is ignored.
  // It is used only for generating a different function signature.
  ThriftField(String name, List<ThriftField> fields, boolean allowFieldAdds) {
    Validate.checkNotNullAndNotEmpty(name, "name");
    Validate.checkNotNull(fields, "fields");

    this.name = name;
    this.fields = fields;
  }

  private int hashcode = 0;

  @Override
  public int hashCode() {
    if (this.hashcode == 0) {
      int hc = this.name.toLowerCase().hashCode();
      for (ThriftField subField : this.fields) {
        hc ^= subField.hashCode();
      }

      this.hashcode = hc;
    }

    return this.hashcode;
  }

  @Override
  public boolean equals(Object o) {
    if (o == null) {
      return false;
    }

    if (!(o instanceof ThriftField)) {
      return false;
    }

    ThriftField other = (ThriftField) o;

    if (!this.name.equalsIgnoreCase(other.name)) {
      return false;
    }

    if (this.fields.size() != other.fields.size()) {
      return false;
    }

    for (int i = 0; i < this.fields.size(); i++) {
      if (!this.fields.get(i).equals(other.fields.get(i))) {
        return false;
      }
    }

    return true;
  }

  @Override
  public String toString() {
    return String.join(", ", this.getFieldNames());
  }

  public List<String> getFieldNames() {
    List<String> fieldsList = new ArrayList<>();
    if (this.fields.size() == 0) {
      fieldsList.add(this.name);
    } else {
      for (ThriftField f : this.fields) {
        for (String subF : f.getFieldNames()) {
          fieldsList.add(this.name + "." + subF);
        }
      }
    }

    return fieldsList;
  }

  /**
   * Generates and returns n-ary tree of fields and their sub-fields.
   *
   * <p>
   *
   * @param fieldNames collection of fully qualified field names.
   *     <p>for example, In case of PinJoin thrift struct, the following are valid field names --
   *     signature -- pins.user.userId -- textSignal.termSignal.termDataMap
   * @return n-ary tree of fields and their sub-fields.
   */
  public static List<ThriftField> fromNames(Collection<String> fieldNames) {
    Validate.checkNotNullAndNotEmpty(fieldNames, "fieldNames");

    List<String> fieldNamesList = new ArrayList<>(fieldNames);
    Collections.sort(fieldNamesList, String.CASE_INSENSITIVE_ORDER);

    List<ThriftField> fields = new ArrayList<>();

    for (String fieldName : fieldNamesList) {
      List<ThriftField> tfields = fields;
      String[] tokens = fieldName.split("\\.");

      for (String token : tokens) {
        ThriftField field = findField(token, tfields);
        if (field == null) {
          field = new ThriftField(token, new ArrayList<>(), true);
          tfields.add(field);
        }
        tfields = field.fields;
      }
    }

    return makeReadOnly(fields);
  }

  private static ThriftField findField(String name, List<ThriftField> fields) {
    for (ThriftField field : fields) {
      if (field.name.equalsIgnoreCase(name)) {
        return field;
      }
    }
    return null;
  }

  private static List<ThriftField> makeReadOnly(List<ThriftField> fields) {
    List<ThriftField> result = new ArrayList<>(fields.size());
    for (ThriftField field : fields) {
      ThriftField copy = new ThriftField(field.name, makeReadOnly(field.fields));
      result.add(copy);
    }
    return Collections.unmodifiableList(result);
  }
}