public FluentIterable apply()

in rackspace-autoscale/src/main/java/org/jclouds/rackspace/autoscale/v1/functions/ParseScalingPoliciesResponse.java [55:94]


   public FluentIterable<ScalingPolicy> apply(HttpResponse from) {
      // This needs to be refactored when the service is in a more final state and changing less often
      // A lot of the complexity is expected to go away

      Map<String, List<Map<String, Object>>> singleMap = (Map<String, List<Map<String, Object>>>) json.apply(from);
      List<Map<String, Object>> result = singleMap.get("policies");
      ImmutableList.Builder<ScalingPolicy> scalingPoliciesList = ImmutableList.builder();

      for (Map<String, Object> scalingPolicyMap : result) {
         ScalingPolicyTargetType targetType = null;
         for (String key : scalingPolicyMap.keySet()) {
            if (ScalingPolicyTargetType.getByValue(key).isPresent()) {
               targetType = ScalingPolicyTargetType.getByValue(key).get();
               break;
            }  
         }

         ImmutableList.Builder<Link> links = ImmutableList.builder();
         for (Map<String, String> linkMap : (List<Map<String, String>>) scalingPolicyMap.get("links")) {
            Link link = Link.builder().href(URI.create(linkMap.get("href"))).relation(Relation.fromValue(linkMap.get("rel"))).build();
            links.add(link);
         }

         Double d = (Double)scalingPolicyMap.get(targetType.toString()); // GSON only knows double now
         ScalingPolicy scalingPolicyResponse = 
               new ScalingPolicy(
                     (String)scalingPolicyMap.get("name"),
                     ScalingPolicyType.getByValue((String)scalingPolicyMap.get("type")).get(),
                     ((Double)scalingPolicyMap.get("cooldown")).intValue(),
                     DoubleMath.isMathematicalInteger(d) ? Integer.toString(d.intValue()) : Double.toString(d),
                           targetType,
                           (Map<String, String>) scalingPolicyMap.get("args"),
                           ImmutableList.copyOf(links.build()),
                           (String) scalingPolicyMap.get("id")
                     );
         scalingPoliciesList.add(scalingPolicyResponse);
      }

      return FluentIterable.from(scalingPoliciesList.build());
   }