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

org.jclouds.openstack.marconi.v1.functions.ParseClaim Maven / Gradle / Ivy

There is a newer version: 2.4.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.openstack.marconi.v1.functions;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ParseJson;
import org.jclouds.openstack.marconi.v1.domain.Claim;
import org.jclouds.openstack.marconi.v1.domain.Message;

import javax.inject.Inject;
import java.beans.ConstructorProperties;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.openstack.marconi.v1.functions.ParseMessagesToStream.MessageWithHref;
import static org.jclouds.openstack.marconi.v1.functions.ParseMessagesToStream.TO_ID_FROM_HREF;
import static org.jclouds.openstack.marconi.v1.functions.ParseMessagesToStream.TO_MESSAGE;

public class ParseClaim implements Function {

   protected static final Function TO_CLAIM = new Function() {
      @Override
      public Claim apply(ClaimWithHref claimWithHref) {
         List messages = ImmutableList.copyOf(transform(claimWithHref.messagesWithHref, TO_MESSAGE));
         String claimId = TO_ID_FROM_HREF.apply(claimWithHref.getId());

         return claimWithHref.toBuilder()
               .id(claimId)
               .messages(messages)
               .build();
      }
   };
   private final ParseJson json;

   @Inject
   ParseClaim(ParseJson json) {
      this.json = checkNotNull(json, "json");
   }

   @Override
   public Claim apply(HttpResponse response) {
      ClaimWithHref claimWithHref = json.apply(response);
      Claim claim = TO_CLAIM.apply(claimWithHref);

      return claim;
   }

   private static class ClaimWithHref extends Claim {
      private final List messagesWithHref;

      @ConstructorProperties({"href", "ttl", "age", "messages"})
      protected ClaimWithHref(String href, int ttl, int age, List messagesWithHref) {
         super(href, ttl, age, null);
         this.messagesWithHref = messagesWithHref;
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy