aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2016-03-17 22:34:52 +0000
committerGravatar Dmitry Lomov <dslomov@google.com>2016-03-18 12:48:29 +0000
commit80665ec28f4fde484c35e2935e8d06aabe902841 (patch)
treebb05d02f898becb9a92e3bfe4af97a4649499884 /src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java
parent652bb6953d2f020322c08c806a1409aae7696c09 (diff)
Part 3 of 5: Merging semantics.
Introduces the AndroidDataMerger, MergeConflict, and UnwrittenMergedAndroidData which is the entry point in the AndroidResourceProcessing *AndroidData lifecycle. Also, refactors the AndroidDataSet parsing of resources, making it functionally immutable. -- MOS_MIGRATED_REVID=117492690
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java')
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java67
1 files changed, 55 insertions, 12 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java b/src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java
index 2f74db5e85..85161bf2ef 100644
--- a/src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java
+++ b/src/tools/android/java/com/google/devtools/build/android/FullyQualifiedName.java
@@ -16,6 +16,8 @@ package com.google.devtools.build.android;
import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Ordering;
import com.android.resources.ResourceType;
@@ -24,16 +26,20 @@ import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import javax.annotation.CheckReturnValue;
+import javax.annotation.concurrent.Immutable;
+
/**
* Represents a fully qualified name for an android resource.
*
* Each resource name consists of the resource package, name, type, and qualifiers.
*/
-public class FullyQualifiedName {
+@Immutable
+public class FullyQualifiedName implements Comparable<FullyQualifiedName> {
public static final String DEFAULT_PACKAGE = "res-auto";
private final String pkg;
- private final List<String> qualifiers;
+ private final ImmutableList<String> qualifiers;
private final ResourceType resourceType;
private final String resourceName;
@@ -98,16 +104,8 @@ public class FullyQualifiedName {
}
}
- private FullyQualifiedName(
- String pkg, List<String> qualifiers, ResourceType resourceType, String resourceName) {
- this.pkg = pkg;
- this.qualifiers = qualifiers;
- this.resourceType = resourceType;
- this.resourceName = resourceName;
- }
-
/**
- * Creates a new FullyQualifiedName.
+ * Creates a new FullyQualifiedName with sorted qualifiers.
* @param pkg The resource package of the name. If unknown the default should be "res-auto"
* @param qualifiers The resource qualifiers of the name, such as "en" or "xhdpi".
* @param resourceType The resource type of the name.
@@ -116,7 +114,30 @@ public class FullyQualifiedName {
*/
public static FullyQualifiedName of(
String pkg, List<String> qualifiers, ResourceType resourceType, String resourceName) {
- return new FullyQualifiedName(pkg, qualifiers, resourceType, resourceName);
+ return new FullyQualifiedName(pkg, Ordering.natural().immutableSortedCopy(qualifiers),
+ resourceType, resourceName);
+ }
+
+ private FullyQualifiedName(
+ String pkg,
+ ImmutableList<String> qualifiers,
+ ResourceType resourceType,
+ String resourceName) {
+ this.pkg = pkg;
+ this.qualifiers = qualifiers;
+ this.resourceType = resourceType;
+ this.resourceName = resourceName;
+ }
+
+ /** Creates a FullyQualifiedName from this one with a different package. */
+ @CheckReturnValue
+ public FullyQualifiedName replacePackage(String newPackage) {
+ if (pkg.equals(newPackage)) {
+ return this;
+ }
+ // Don't use "of" because it ensures the qualifiers are sorted -- we already know
+ // they are sorted here.
+ return new FullyQualifiedName(newPackage, qualifiers, resourceType, resourceName);
}
@Override
@@ -145,4 +166,26 @@ public class FullyQualifiedName {
.add("resourceName", resourceName)
.toString();
}
+
+ @Override
+ public int compareTo(FullyQualifiedName other) {
+ if (!pkg.equals(other.pkg)) {
+ return pkg.compareTo(other.pkg);
+ }
+ if (!resourceType.equals(other.resourceType)) {
+ return resourceType.compareTo(other.resourceType);
+ }
+ if (!resourceName.equals(other.resourceName)) {
+ return resourceName.compareTo(other.resourceName);
+ }
+ // TODO(corysmith): Figure out a more performant stable way to keep a stable order.
+ if (!qualifiers.equals(other.qualifiers)) {
+ if (qualifiers.size() != other.qualifiers.size()) {
+ return qualifiers.size() - other.qualifiers.size();
+ }
+ // This works because the qualifiers are sorted on creation.
+ return qualifiers.toString().compareTo(other.qualifiers.toString());
+ }
+ return 0;
+ }
}