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

org.jclouds.openstack.domain.Resource 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.openstack.domain;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Collections;
import java.util.Set;

import org.jclouds.javax.annotation.Nullable;

import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.ImmutableSet;

/**
 * Resource found in a paginated collection
 * 
 * @author AdrianCole
 * @see 
 */
public class Resource implements Comparable {

   public static Builder builder() {
      return new ConcreteBuilder();
   }

   public Builder toBuilder() {
      return new ConcreteBuilder().fromResource(this);
   }

   public abstract static class Builder>  {
      protected abstract T self();

      private String id;
      private String name;
      private Set links = ImmutableSet.of();

      /**
       * @see Resource#getId()
       */
      public T id(String id) {
         this.id = id;
         return self();
      }

      /**
       * @see Resource#getName()
       */
      public T name(String name) {
         this.name = name;
         return self();
      }

      /**
       * @see Resource#getLinks()
       */
      public T links(Link... links) {
         return links(ImmutableSet.copyOf(checkNotNull(links, "links")));
      }

      /**
       * @see Resource#getLinks()
       */
      public T links(Set links) {
         this.links = links;
         return self();
      }

      public Resource build() {
         return new Resource(id, name, links);
      }

      public T fromResource(Resource in) {
         return id(in.getId()).name(in.getName()).links(in.getLinks());
      }
   }

   private static class ConcreteBuilder extends Builder {
      @Override
      protected ConcreteBuilder self() {
         return this;
      }
   }

   private String id;
   private String name;
   private Set links = ImmutableSet.of();

   protected Resource(String id, @Nullable String name, @Nullable Set links) {
      this.id = checkNotNull(id);
      this.name = name;
      this.links = links == null ? ImmutableSet.of() : ImmutableSet.copyOf(links);
   }
   
   /**
    * When providing an ID, it is assumed that the resource exists in the current OpenStack
    * deployment
    *
    * @return the id of the resource in the current OpenStack deployment
    */
   public String getId() {
      return id;
   }

   /**
    * @return the name of the resource
    */
   @Nullable
   public String getName() {
      return name;
   }

   /**
    * @return the links of the id address allocated to the new server
    */
   public Set getLinks() {
      return Collections.unmodifiableSet(this.links);
   }

   @Override
   public int hashCode() {
      return Objects.hashCode(id, name, links);
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj) return true;
      if (obj == null || getClass() != obj.getClass()) return false;
      Resource that = Resource.class.cast(obj);
      return Objects.equal(this.getId(), that.getId())
            && Objects.equal(this.name, that.name)
            && Objects.equal(this.links, that.links);
   }

   protected ToStringHelper string() {
      return Objects.toStringHelper("")
            .add("id", getId())
            .add("name", name)
            .add("links", links);
   }

   @Override
   public String toString() {
      return string().toString();
   }
   @Override
   public int compareTo(Resource that) {
      if (that == null)
         return 1;
      if (this == that)
         return 0;
      return this.getId().compareTo(that.getId());
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy