aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/cmdline
diff options
context:
space:
mode:
authorGravatar janakr <janakr@google.com>2018-03-05 18:21:20 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-05 18:22:55 -0800
commit66c52e37d83371d93cea712c73f6b7355e0a0d0c (patch)
tree9cebbd6ddfec2188b8ac4b63a94541ec6237ba59 /src/main/java/com/google/devtools/build/lib/cmdline
parent3e687ff5988aa6b3fb1582ade1362c80698a86ff (diff)
@AutoCodec RepositoryName.
PiperOrigin-RevId: 187956593
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/cmdline')
-rw-r--r--src/main/java/com/google/devtools/build/lib/cmdline/RepositoryName.java14
-rw-r--r--src/main/java/com/google/devtools/build/lib/cmdline/RepositoryNameCodec.java78
2 files changed, 6 insertions, 86 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryName.java b/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryName.java
index 7cd9102ca0..b637cf6d84 100644
--- a/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryName.java
+++ b/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryName.java
@@ -18,7 +18,7 @@ import com.google.common.base.Throwables;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
-import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
+import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.util.Pair;
import com.google.devtools.build.lib.util.StringCanonicalizer;
import com.google.devtools.build.lib.util.StringUtilities;
@@ -31,13 +31,12 @@ import java.io.Serializable;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
-/**
- * A human-readable name for the repository.
- */
+/** A human-readable name for the repository. */
+@AutoCodec
public final class RepositoryName implements Serializable {
public static final String DEFAULT_REPOSITORY = "";
- public static final RepositoryName DEFAULT;
- public static final RepositoryName MAIN;
+ @AutoCodec public static final RepositoryName DEFAULT;
+ @AutoCodec public static final RepositoryName MAIN;
private static final Pattern VALID_REPO_NAME = Pattern.compile("@[\\w\\-.]*");
/** Helper for serializing {@link RepositoryName}. */
@@ -104,13 +103,12 @@ public final class RepositoryName implements Serializable {
}
}
- public static final ObjectCodec<RepositoryName> CODEC = new RepositoryNameCodec();
-
/**
* Makes sure that name is a valid repository name and creates a new RepositoryName using it.
*
* @throws LabelSyntaxException if the name is invalid
*/
+ @AutoCodec.Instantiator
public static RepositoryName create(String name) throws LabelSyntaxException {
try {
return repositoryNameCache.get(name);
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryNameCodec.java b/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryNameCodec.java
deleted file mode 100644
index 4949b2fd48..0000000000
--- a/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryNameCodec.java
+++ /dev/null
@@ -1,78 +0,0 @@
-// Copyright 2017 The Bazel Authors. All rights reserved.
-//
-// 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.devtools.build.lib.cmdline;
-
-import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
-import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
-import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
-import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
-import com.google.protobuf.ByteString;
-import com.google.protobuf.CodedInputStream;
-import com.google.protobuf.CodedOutputStream;
-import java.io.IOException;
-
-/** Custom serialization for {@link RepositoryName}. */
-public class RepositoryNameCodec implements ObjectCodec<RepositoryName> {
-
- @Override
- public Class<RepositoryName> getEncodedClass() {
- return RepositoryName.class;
- }
-
- @Override
- public void serialize(
- SerializationContext context, RepositoryName repoName, CodedOutputStream codedOut)
- throws IOException {
- boolean isMain = repoName.isMain();
- // Main is by far the most common. Use boolean to short-circuit string encoding on
- // serialization and byte[]/ByteString creation on deserialization.
- codedOut.writeBoolNoTag(isMain);
- if (!isMain) {
- codedOut.writeStringNoTag(repoName.getName());
- }
- }
-
- @Override
- public RepositoryName deserialize(DeserializationContext context, CodedInputStream codedIn)
- throws SerializationException, IOException {
- boolean isMain = codedIn.readBool();
- if (isMain) {
- return RepositoryName.MAIN;
- }
- try {
- // We can read the string we wrote back as bytes to avoid string decoding/copying.
- return deserializeRepoName(codedIn.readBytes());
- } catch (LabelSyntaxException e) {
- throw new SerializationException("Failed to deserialize RepositoryName", e);
- }
- }
-
- private static final ByteString DEFAULT_REPOSITORY =
- ByteString.copyFromUtf8(RepositoryName.DEFAULT.getName());
- private static final ByteString MAIN_REPOSITORY =
- ByteString.copyFromUtf8(RepositoryName.MAIN.getName());
-
- public static RepositoryName deserializeRepoName(ByteString repoNameBytes)
- throws LabelSyntaxException {
- // We expect MAIN_REPOSITORY the vast majority of the time, so check for it first.
- if (repoNameBytes.equals(MAIN_REPOSITORY)) {
- return RepositoryName.MAIN;
- } else if (repoNameBytes.equals(DEFAULT_REPOSITORY)) {
- return RepositoryName.DEFAULT;
- } else {
- return RepositoryName.create(repoNameBytes.toStringUtf8());
- }
- }
-}