All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jclouds.ohai.functions.NestSlashKeys Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
/*
 * 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.> fromJson(value.toString(), mapLiteral);
      Set uniques = Sets.difference(toPut.keySet(), valueContext.keySet());
      for (String k : uniques) {
         valueContext.put(k, toPut.get(k));
      }
      Set conflicts = Sets.difference(toPut.keySet(), uniques);
      for (String k : conflicts) {
         JsonBall v = toPut.get(k);
         if (v.toString().matches("^\\{.*\\}$")) {
            mergeAsPeer(k, v, valueContext);
         } else {
            // replace
            valueContext.put(k, v);
         }
      }
      insertionContext.put(key, new JsonBall(json.toJson(valueContext, mapLiteral)));
   }

   /**
    * @param keyParts
    * @param toInsert
    * @param destination
    * @throws IllegalArgumentException
    *            

* if destination.get(keyParts(0)) is not a map * *

* keyParts is zero length */ void putUnderContext(List keyParts, JsonBall toInsert, Map destination) { checkNotNull(keyParts, "keyParts"); checkArgument(keyParts.size() >= 1, "keyParts must contain at least one element"); checkNotNull(toInsert, "toInsert"); checkNotNull(destination, "destination"); String rootKey = keyParts.remove(0); String rootValue = destination.containsKey(rootKey) ? destination.get(rootKey).toString() : "{}"; checkArgument(rootValue.matches("^\\{.*\\}$"), "value must be a hash: %s", rootValue); Map immutableInsertionContext = json.fromJson(rootValue, mapLiteral); Map insertionContext = Maps.newLinkedHashMap(immutableInsertionContext); if (keyParts.size() == 1) { if (!insertionContext.containsKey(keyParts.get(0))) { insertionContext.put(keyParts.get(0), toInsert); } else { String key = keyParts.get(0); mergeAsPeer(key, toInsert, insertionContext); } } else { putUnderContext(keyParts, toInsert, insertionContext); } destination.put(rootKey, new JsonBall(json.toJson(insertionContext, mapLiteral))); } final Type mapLiteral = new TypeLiteral>() { }.getType(); final Type listLiteral = new TypeLiteral>() { }.getType(); }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy