Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ JAVA_SRCS = [
TEST_SRCS = [
"//src/test/java/com/google/api/generator/engine:engine_files",
"//src/test/java/com/google/api/generator/gapic:gapic_files",
"//src/test/java/com/google/api/generator/util:util_files",
"//src/test/java/com/google/api/generator/test/framework:framework_files",
]

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/api/generator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ java_library(
"//src/main/java/com/google/api/generator/engine/ast",
"//src/main/java/com/google/api/generator/gapic",
"//src/main/java/com/google/api/generator/gapic/model",
"//src/main/java/com/google/api/generator/util",
"@com_google_googleapis//google/api:api_java_proto",
"@com_google_googleapis//google/longrunning:longrunning_java_proto",
"@com_google_guava_guava//jar",
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/google/api/generator/gapic/model/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.api.generator.engine.ast.TypeNode;
import com.google.auto.value.AutoValue;
import java.util.Objects;
import javax.annotation.Nullable;

@AutoValue
Expand Down Expand Up @@ -48,6 +49,37 @@ public boolean hasResourceReference() {
return type().equals(TypeNode.STRING) && resourceReference() != null;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof Field)) {
return false;
}

Field other = (Field) o;
return name().equals(other.name())
&& type().equals(other.type())
&& isMessage() == other.isMessage()
&& isEnum() == other.isEnum()
&& isRepeated() == other.isRepeated()
&& isMap() == other.isMap()
&& isContainedInOneof() == other.isContainedInOneof()
&& Objects.equals(resourceReference(), other.resourceReference())
&& Objects.equals(description(), other.description());
}

@Override
public int hashCode() {
return 17 * name().hashCode()
+ 19 * type().hashCode()
+ (isMessage() ? 1 : 0) * 23
+ (isEnum() ? 1 : 0) * 29
+ (isRepeated() ? 1 : 0) * 31
+ (isMap() ? 1 : 0) * 37
+ (isContainedInOneof() ? 1 : 0) * 41
+ (resourceReference() == null ? 0 : resourceReference().hashCode())
+ (description() == null ? 0 : description().hashCode());
}

public abstract Builder toBuilder();

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ public int compareTo(MethodArgument other) {
return compareVal;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof MethodArgument)) {
return false;
}

MethodArgument other = (MethodArgument) o;
return name().equals(other.name())
&& type().equals(other.type())
&& field().equals(other.field()) & nestedFields().equals(other.nestedFields())
&& isResourceNameHelper() == other.isResourceNameHelper();
}

@Override
public int hashCode() {
return 17 * name().hashCode()
+ 19 * type().hashCode()
+ 23 * field().hashCode()
+ 29 * nestedFields().hashCode()
+ (isResourceNameHelper() ? 1 : 0) * 31;
}

public static Builder builder() {
return new AutoValue_MethodArgument.Builder()
.setNestedFields(ImmutableList.of())
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/google/api/generator/util/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@rules_java//java:defs.bzl", "java_library")

package(default_visibility = ["//visibility:public"])

filegroup(
name = "util_files",
srcs = glob(["*.java"]),
)

java_library(
name = "util",
srcs = [
":util_files",
],
)
88 changes: 88 additions & 0 deletions src/main/java/com/google/api/generator/util/Trie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2020 Google LLC
//
// 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.api.generator.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* A common-prefix trie. T represents the type of each "char" in a word (which is a T-typed list).
*/
public class Trie<T> {
private class Node<T> {
final T chr;
Map<T, Node> children = new HashMap<>();
boolean isLeaf;

Node() {
chr = null;
}

Node(T chr) {
this.chr = chr;
}
}

private Node<T> root;

public Trie() {
root = new Node();
}

public void insert(List<T> word) {
Map<T, Node> children = root.children;
for (int i = 0; i < word.size(); i++) {
T chr = word.get(i);
Node t;
if (children.containsKey(chr)) {
t = children.get(chr);
} else {
t = new Node(chr);
children.put(chr, t);
}
children = t.children;
if (i == word.size() - 1) {
t.isLeaf = true;
}
}
}

/** Returns true if the word is in the trie. */
public boolean search(List<T> word) {
Node node = searchNode(word);
return node != null && node.isLeaf;
}

/** Returns true if some word in the trie begins with the given prefix. */
public boolean hasPrefix(List<T> prefix) {
return searchNode(prefix) != null;
}

private Node searchNode(List<T> word) {
Map<T, Node> children = root.children;
Node t = null;
for (int i = 0; i < word.size(); i++) {
T chr = word.get(i);
if (children.containsKey(chr)) {
t = children.get(chr);
children = t.children;
} else {
return null;
}
}
return t;
}
}
24 changes: 24 additions & 0 deletions src/test/java/com/google/api/generator/util/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@rules_java//java:defs.bzl", "java_test")

package(default_visibility = ["//visibility:public"])

TESTS = [
"TrieTest",
]

filegroup(
name = "util_files",
srcs = ["{0}.java".format(f) for f in TESTS],
)

[java_test(
name = test_name,
srcs = ["{0}.java".format(test_name)],
test_class = "com.google.api.generator.util.{0}".format(test_name),
deps = [
"//src/main/java/com/google/api/generator/util",
"@com_google_guava_guava//jar",
"@com_google_truth_truth//jar",
"@junit_junit//jar",
],
) for test_name in TESTS]
65 changes: 65 additions & 0 deletions src/test/java/com/google/api/generator/util/TrieTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2020 Google LLC
//
// 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.api.generator.util;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import org.junit.Test;

public class TrieTest {
@Test
public void stringTrie() {
Trie<String> trie = new Trie<>();

Function<String, List<String>> wordToCharListFn = w -> Arrays.asList(w.split("(?!^)"));
List<String> wordCat = wordToCharListFn.apply("cat");
assertFalse(trie.search(wordCat));

trie.insert(wordCat);
assertTrue(trie.search(wordCat));
assertFalse(trie.search(wordToCharListFn.apply("car")));
assertTrue(trie.hasPrefix(wordToCharListFn.apply("ca")));

trie.insert(wordToCharListFn.apply("car"));
trie.insert(wordToCharListFn.apply("dog"));
assertTrue(trie.search(wordToCharListFn.apply("car")));
assertTrue(trie.search(wordToCharListFn.apply("dog")));
}

@Test
public void multiStringTrie() {
Trie<String> trie = new Trie<>();
assertFalse(trie.search(Arrays.asList("user", "identity", "name")));

trie.insert(Arrays.asList("user", "identity", "name"));
trie.insert(Arrays.asList("user", "identity", "age"));
trie.insert(Arrays.asList("user", "contact", "email"));

assertTrue(trie.search(Arrays.asList("user", "identity", "name")));
assertTrue(trie.search(Arrays.asList("user", "identity", "age")));
assertFalse(trie.search(Arrays.asList("user", "identity", "eyeColor")));
assertTrue(trie.search(Arrays.asList("user", "contact", "email")));
assertTrue(trie.hasPrefix(Arrays.asList("user", "identity")));
assertTrue(trie.hasPrefix(Arrays.asList("user", "contact")));
assertTrue(trie.hasPrefix(Arrays.asList("user")));

assertFalse(trie.hasPrefix(Arrays.asList("identity")));
assertFalse(trie.hasPrefix(Arrays.asList("contact")));
}
}