Skip to content

Commit

Permalink
Renaming nonOverwriting to combining resources.
Browse files Browse the repository at this point in the history
--
MOS_MIGRATED_REVID=121863653
  • Loading branch information
Googler authored and aehlig committed May 10, 2016
1 parent b64150d commit 5a2dd7a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ UnwrittenMergedAndroidData merge(
ParsedAndroidData.of(
ImmutableSet.<MergeConflict>of(),
ImmutableMap.copyOf(overwritableDeps),
direct.mergeNonOverwritable(transitive),
direct.mergeCombining(transitive),
ImmutableMap.copyOf(assets)));
} catch (IOException e) {
throw new MergingException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void readEntriesSegment(
resourceName,
FullyQualifiedName.isOverwritable(resourceName)
? consumers.overwritingConsumer
: consumers.nonOverwritingConsumer);
: consumers.combiningConsumer);
} else {
keys.put(RelativeAssetPath.fromProto(protoKey, currentFileSystem), consumers.assetConsumer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
public class DataResourceXml implements DataResource {

/**
* Parses xml resources from a Path to the provided overwritable and nonOverwritable collections.
* Parses xml resources from a Path to the provided overwritable and combining collections.
*
* This method is a bit tricky in the service of performance -- creating several collections and
* merging them was more expensive than writing to mutable collections directly.
Expand All @@ -65,7 +65,7 @@ public class DataResourceXml implements DataResource {
* @param path The path to the xml resource to be parsed.
* @param fqnFactory Used to create {@link FullyQualifiedName}s from the resource names.
* @param overwritingConsumer A consumer for overwritable {@link DataResourceXml}s.
* @param nonOverwritingConsumer A consumer for nonoverwritable {@link DataResourceXml}s.
* @param combiningConsumer A consumer for combining {@link DataResourceXml}s.
* @throws XMLStreamException Thrown with the resource format is invalid.
* @throws FactoryConfigurationError Thrown with the {@link XMLInputFactory} is misconfigured.
* @throws IOException Thrown when there is an error reading a file.
Expand All @@ -75,7 +75,7 @@ public static void parse(
Path path,
Factory fqnFactory,
KeyValueConsumer<DataKey, DataResource> overwritingConsumer,
KeyValueConsumer<DataKey, DataResource> nonOverwritingConsumer)
KeyValueConsumer<DataKey, DataResource> combiningConsumer)
throws XMLStreamException, FactoryConfigurationError, IOException {
XMLEventReader eventReader =
xmlInputFactory.createXMLEventReader(Files.newBufferedReader(path, StandardCharsets.UTF_8));
Expand All @@ -94,14 +94,14 @@ public static void parse(
path + " contains an unrecognized resource type:" + start, start.getLocation());
}
if (resourceType == DECLARE_STYLEABLE) {
// Styleables are special, as they produce multiple overwrite and non-overwrite values,
// Styleables are special, as they produce multiple overwrite and combining values,
// so we let the value handle the assignments.
XmlResourceValues.parseDeclareStyleable(
fqnFactory, path, overwritingConsumer, nonOverwritingConsumer, eventReader, start);
fqnFactory, path, overwritingConsumer, combiningConsumer, eventReader, start);
} else {
// Of simple resources, only IDs are nonOverwriting.
// Of simple resources, only IDs are combining.
KeyValueConsumer<DataKey, DataResource> consumer =
resourceType == ID ? nonOverwritingConsumer : overwritingConsumer;
resourceType == ID ? combiningConsumer : overwritingConsumer;
FullyQualifiedName key =
fqnFactory.create(resourceType, XmlResourceValues.getElementName(start));
consumer.consume(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@
class KeyValueConsumers {
static KeyValueConsumers of(
KeyValueConsumer<DataKey, DataResource> overwritingConsumer,
KeyValueConsumer<DataKey, DataResource> nonOverwritingConsumer,
KeyValueConsumer<DataKey, DataResource> combiningConsumer,
KeyValueConsumer<DataKey, DataAsset> assetConsumer) {
return new KeyValueConsumers(overwritingConsumer, nonOverwritingConsumer, assetConsumer);
return new KeyValueConsumers(overwritingConsumer, combiningConsumer, assetConsumer);
}

final KeyValueConsumer<DataKey, DataResource> overwritingConsumer;
final KeyValueConsumer<DataKey, DataResource> nonOverwritingConsumer;
final KeyValueConsumer<DataKey, DataResource> combiningConsumer;
final KeyValueConsumer<DataKey, DataAsset> assetConsumer;

private KeyValueConsumers(
KeyValueConsumer<DataKey, DataResource> overwritingConsumer,
KeyValueConsumer<DataKey, DataResource> nonOverwritingConsumer,
KeyValueConsumer<DataKey, DataResource> combiningConsumer,
KeyValueConsumer<DataKey, DataAsset> assetConsumer) {
this.overwritingConsumer = overwritingConsumer;
this.nonOverwritingConsumer = nonOverwritingConsumer;
this.combiningConsumer = combiningConsumer;
this.assetConsumer = assetConsumer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.android.xml.StyleableXmlResourceValue;

import com.android.ide.common.res2.MergingException;

Expand Down Expand Up @@ -54,28 +55,28 @@ public class ParsedAndroidData {

static class Builder {
private final Map<DataKey, DataResource> overwritingResources;
private final Map<DataKey, DataResource> nonOverwritingResources;
private final Map<DataKey, DataResource> combiningResources;
private final Map<DataKey, DataAsset> assets;
private final Set<MergeConflict> conflicts;
private final List<Exception> errors = new ArrayList<>();

public Builder(
Map<DataKey, DataResource> overwritingResources,
Map<DataKey, DataResource> nonOverwritingResources,
Map<DataKey, DataResource> combiningResources,
Map<DataKey, DataAsset> assets,
Set<MergeConflict> conflicts) {
this.overwritingResources = overwritingResources;
this.nonOverwritingResources = nonOverwritingResources;
this.combiningResources = combiningResources;
this.assets = assets;
this.conflicts = conflicts;
}

static Builder newBuilder() {
final Map<DataKey, DataResource> overwritingResources = new LinkedHashMap<>();
final Map<DataKey, DataResource> nonOverwritingResources = new LinkedHashMap<>();
final Map<DataKey, DataResource> combiningResources = new LinkedHashMap<>();
final Map<DataKey, DataAsset> assets = new LinkedHashMap<>();
final Set<MergeConflict> conflicts = new LinkedHashSet<>();
return new Builder(overwritingResources, nonOverwritingResources, assets, conflicts);
return new Builder(overwritingResources, combiningResources, assets, conflicts);
}

private void checkForErrors() throws MergingException {
Expand All @@ -93,14 +94,14 @@ ParsedAndroidData build() throws MergingException {
return ParsedAndroidData.of(
ImmutableSet.copyOf(conflicts),
ImmutableMap.copyOf(overwritingResources),
ImmutableMap.copyOf(nonOverwritingResources),
ImmutableMap.copyOf(combiningResources),
ImmutableMap.copyOf(assets));
}

ResourceFileVisitor resourceVisitor() {
return new ResourceFileVisitor(
new OverwritableConsumer<>(overwritingResources, conflicts),
new CombiningConsumer(nonOverwritingResources),
new CombiningConsumer(combiningResources),
errors);
}

Expand All @@ -112,7 +113,7 @@ AssetFileVisitor assetVisitorFor(Path path) {
public KeyValueConsumers consumers() {
return KeyValueConsumers.of(
new OverwritableConsumer<>(overwritingResources, conflicts),
new CombiningConsumer(nonOverwritingResources),
new CombiningConsumer(combiningResources),
new OverwritableConsumer<>(assets, conflicts));
}
}
Expand Down Expand Up @@ -223,18 +224,18 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IO
*/
private static class ResourceFileVisitor extends SimpleFileVisitor<Path> {
private final KeyValueConsumer<DataKey, DataResource> overwritingConsumer;
private final KeyValueConsumer<DataKey, DataResource> nonOverwritingConsumer;
private final KeyValueConsumer<DataKey, DataResource> combiningResources;
private final List<Exception> errors;
private boolean inValuesSubtree;
private FullyQualifiedName.Factory fqnFactory;
private final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();

ResourceFileVisitor(
KeyValueConsumer<DataKey, DataResource> overwritingConsumer,
KeyValueConsumer<DataKey, DataResource> nonOverwritingConsumer,
KeyValueConsumer<DataKey, DataResource> combiningResources,
List<Exception> errors) {
this.overwritingConsumer = overwritingConsumer;
this.nonOverwritingConsumer = nonOverwritingConsumer;
this.combiningResources = combiningResources;
this.errors = errors;
}

Expand Down Expand Up @@ -270,7 +271,7 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IO
if (!Files.isDirectory(path) && !path.getFileName().toString().startsWith(".")) {
if (inValuesSubtree) {
DataResourceXml.parse(
xmlInputFactory, path, fqnFactory, overwritingConsumer, nonOverwritingConsumer);
xmlInputFactory, path, fqnFactory, overwritingConsumer, combiningResources);
} else {
String rawFqn = deriveRawFullyQualifiedName(path);
FullyQualifiedName key = fqnFactory.parse(rawFqn);
Expand All @@ -284,13 +285,13 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IO
}
}

/** Creates ParsedAndroidData of conflicts, assets overwriting and nonOverwriting resources. */
/** Creates ParsedAndroidData of conflicts, assets overwriting and combining resources. */
public static ParsedAndroidData of(
ImmutableSet<MergeConflict> conflicts,
ImmutableMap<DataKey, DataResource> overwritingResources,
ImmutableMap<DataKey, DataResource> nonOverwritingResources,
ImmutableMap<DataKey, DataResource> combiningResources,
ImmutableMap<DataKey, DataAsset> assets) {
return new ParsedAndroidData(conflicts, overwritingResources, nonOverwritingResources, assets);
return new ParsedAndroidData(conflicts, overwritingResources, combiningResources, assets);
}

/**
Expand Down Expand Up @@ -334,25 +335,25 @@ public static ParsedAndroidData from(List<DependencyAndroidData> dependencyAndro

private final ImmutableSet<MergeConflict> conflicts;
private final ImmutableMap<DataKey, DataResource> overwritingResources;
private final ImmutableMap<DataKey, DataResource> nonOverwritingResources;
private final ImmutableMap<DataKey, DataResource> combiningResources;
private final ImmutableMap<DataKey, DataAsset> assets;

private ParsedAndroidData(
ImmutableSet<MergeConflict> conflicts,
ImmutableMap<DataKey, DataResource> overwritingResources,
ImmutableMap<DataKey, DataResource> nonOverwritingResources,
ImmutableMap<DataKey, DataResource> combiningResources,
ImmutableMap<DataKey, DataAsset> assets) {
this.conflicts = conflicts;
this.overwritingResources = overwritingResources;
this.nonOverwritingResources = nonOverwritingResources;
this.combiningResources = combiningResources;
this.assets = assets;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("overwritingResources", overwritingResources)
.add("nonOverwritingResources", nonOverwritingResources)
.add("combiningResources", combiningResources)
.add("assets", assets)
.toString();
}
Expand All @@ -367,14 +368,14 @@ public boolean equals(Object other) {
}
ParsedAndroidData that = (ParsedAndroidData) other;
return Objects.equals(overwritingResources, that.overwritingResources)
&& Objects.equals(nonOverwritingResources, that.nonOverwritingResources)
&& Objects.equals(combiningResources, that.combiningResources)
&& Objects.equals(conflicts, that.conflicts)
&& Objects.equals(assets, that.assets);
}

@Override
public int hashCode() {
return Objects.hash(conflicts, overwritingResources, nonOverwritingResources, assets);
return Objects.hash(conflicts, overwritingResources, combiningResources, assets);
}

/**
Expand All @@ -393,18 +394,20 @@ Map<DataKey, DataResource> getOverwritingResources() {
}

/**
* Returns a list of resources that would not overwrite other values when defined.
* Returns a list of resources are combined with other values that have the same key.
*
* <p>
* Example:
*
* A id resource (id.Foo) could be redefined at id.Foo with no adverse effects.
* A id resource (id.Foo) combined id.Foo with no adverse effects, whereas two stylable.Bar
* resources would be combined, resulting in a Styleable containing a union of the attributes.
* See {@link StyleableXmlResourceValue} for more information.
*
* @return A map of key -&gt; non-overwriting resources.
* @return A map of key -&gt; combing resources.
*/
@VisibleForTesting
Map<DataKey, DataResource> getNonOverwritingResources() {
return nonOverwritingResources;
Map<DataKey, DataResource> getCombiningResources() {
return combiningResources;
}

/**
Expand Down Expand Up @@ -432,7 +435,7 @@ Iterable<Entry<DataKey, DataResource>> iterateOverwritableEntries() {
}

Iterable<Entry<DataKey, DataResource>> iterateDataResourceEntries() {
return Iterables.concat(overwritingResources.entrySet(), nonOverwritingResources.entrySet());
return Iterables.concat(overwritingResources.entrySet(), combiningResources.entrySet());
}

boolean containsAsset(DataKey name) {
Expand All @@ -451,12 +454,12 @@ MergeConflict foundAssetConflict(DataKey key, DataAsset value) {
return MergeConflict.between(key, assets.get(key), value);
}

ImmutableMap<DataKey, DataResource> mergeNonOverwritable(ParsedAndroidData other) {
ImmutableMap<DataKey, DataResource> mergeCombining(ParsedAndroidData other) {
Map<DataKey, DataResource> merged = new HashMap<>();
CombiningConsumer consumer = new CombiningConsumer(merged);
for (Entry<DataKey, DataResource> entry :
Iterables.concat(
nonOverwritingResources.entrySet(), other.nonOverwritingResources.entrySet())) {
combiningResources.entrySet(), other.combiningResources.entrySet())) {
consumer.consume(entry.getKey(), entry.getValue());
}
return ImmutableMap.copyOf(merged);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static void parseDeclareStyleable(
FullyQualifiedName.Factory fqnFactory,
Path path,
KeyValueConsumer<DataKey, DataResource> overwritingConsumer,
KeyValueConsumer<DataKey, DataResource> nonOverwritingConsumer,
KeyValueConsumer<DataKey, DataResource> combiningConsumer,
XMLEventReader eventReader,
StartElement start)
throws XMLStreamException {
Expand All @@ -134,7 +134,7 @@ static void parseDeclareStyleable(
}
}
}
nonOverwritingConsumer.consume(
combiningConsumer.consume(
fqnFactory.create(ResourceType.STYLEABLE, getElementName(start)),
DataResourceXml.of(path, StyleableXmlResourceValue.of(members)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ public String toString() {
* {@link FullyQualifiedName} results in a single Styleable containing a union of all the
* attribute references.
*
* @param value Another {@link StyleableXmlResourceValue} with the same {@link FullyQualifiedName}
* .
* @param value Another {@link StyleableXmlResourceValue} with the same
* {@link FullyQualifiedName}.
* @return {@link StyleableXmlResourceValue} containing a sorted union of the attribute
* references.
* @throws IllegalArgumentException if value is not an {@link StyleableXmlResourceValue}.
Expand Down

0 comments on commit 5a2dd7a

Please sign in to comment.