org.jclouds.ohai.functions.NestSlashKeys Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of chef Show documentation
Show all versions of chef Show documentation
jclouds components to access Chef
/*
* 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.jclouds.ohai.functions;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.domain.JsonBall;
import org.jclouds.json.Json;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Supplier;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.inject.TypeLiteral;
@Singleton
public class NestSlashKeys implements Function>, Map> {
private final Json json;
@Inject
NestSlashKeys(Json json) {
this.json = checkNotNull(json, "json");
}
@Override
public Map apply(Multimap> from) {
Map autoAttrs = mergeSameKeys(from);
Map modifiableFlatMap = Maps.newLinkedHashMap(Maps.filterKeys(autoAttrs,
new Predicate() {
@Override
public boolean apply(String input) {
return input.indexOf('/') == -1;
}
}));
Map withSlashesMap = Maps.difference(autoAttrs, modifiableFlatMap).entriesOnlyOnLeft();
for (Entry entry : withSlashesMap.entrySet()) {
List keyParts = Lists.newArrayList(Splitter.on('/').split(entry.getKey()));
JsonBall toInsert = entry.getValue();
try {
putUnderContext(keyParts, toInsert, modifiableFlatMap);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("error inserting value in entry: " + entry.getKey(), e);
}
}
return modifiableFlatMap;
}
private Map mergeSameKeys(Multimap> from) {
Map merged = Maps.newLinkedHashMap();
for (Entry> entry : from.entries()) {
if (merged.containsKey(entry.getKey())) {
mergeAsPeer(entry.getKey(), entry.getValue().get(), merged);
} else {
merged.put(entry.getKey(), entry.getValue().get());
}
}
return merged;
}
@VisibleForTesting
void mergeAsPeer(String key, JsonBall value, Map insertionContext) {
Map immutableValueContext = json.fromJson(insertionContext.get(key).toString(), mapLiteral);
Map valueContext = Maps.newLinkedHashMap(immutableValueContext);
Map toPut = json.
© 2015 - 2024 Weber Informatics LLC | Privacy Policy