| 1 | /** | 
| 2 |  * | 
| 3 |  * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> | 
| 4 |  * | 
| 5 |  * ==================================================================== | 
| 6 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
| 7 |  * you may not use this file except in compliance with the License. | 
| 8 |  * You may obtain a copy of the License at | 
| 9 |  * | 
| 10 |  * http://www.apache.org/licenses/LICENSE-2.0 | 
| 11 |  * | 
| 12 |  * Unless required by applicable law or agreed to in writing, software | 
| 13 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
| 14 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 15 |  * See the License for the specific language governing permissions and | 
| 16 |  * limitations under the License. | 
| 17 |  * ==================================================================== | 
| 18 |  */ | 
| 19 | package org.jclouds.compute.domain.internal; | 
| 20 |   | 
| 21 | import static com.google.common.base.Preconditions.checkNotNull; | 
| 22 | import static org.jclouds.compute.util.ComputeServiceUtils.getCoresAndSpeed; | 
| 23 | import static org.jclouds.compute.util.ComputeServiceUtils.getSpace; | 
| 24 |   | 
| 25 | import java.net.URI; | 
| 26 | import java.util.List; | 
| 27 | import java.util.Map; | 
| 28 |   | 
| 29 | import javax.annotation.Nullable; | 
| 30 |   | 
| 31 | import org.jclouds.compute.domain.ComputeType; | 
| 32 | import org.jclouds.compute.domain.Hardware; | 
| 33 | import org.jclouds.compute.domain.Image; | 
| 34 | import org.jclouds.compute.domain.Processor; | 
| 35 | import org.jclouds.compute.domain.Volume; | 
| 36 | import org.jclouds.domain.Location; | 
| 37 | import org.jclouds.domain.ResourceMetadata; | 
| 38 |   | 
| 39 | import com.google.common.base.Predicate; | 
| 40 | import com.google.common.collect.ComparisonChain; | 
| 41 | import com.google.common.collect.ImmutableList; | 
| 42 |   | 
| 43 | /** | 
| 44 |  * @author Adrian Cole | 
| 45 |  */ | 
| 46 | public class HardwareImpl extends ComputeMetadataImpl implements Hardware { | 
| 47 |   | 
| 48 |    /** The serialVersionUID */ | 
| 49 |    private static final long serialVersionUID = 8994255275911717567L; | 
| 50 |    private final List<Processor> processors; | 
| 51 |    private final int ram; | 
| 52 |    private final List<Volume> volumes; | 
| 53 |    private final Predicate<Image> supportsImage; | 
| 54 |   | 
| 55 |    public HardwareImpl(String providerId, String name, String id, @Nullable Location location, URI uri, | 
| 56 |          Map<String, String> userMetadata, Iterable<? extends Processor> processors, int ram, | 
| 57 |          Iterable<? extends Volume> volumes, Predicate<Image> supportsImage) { | 
| 58 |       super(ComputeType.HARDWARE, providerId, name, id, location, uri, userMetadata); | 
| 59 |       this.processors = ImmutableList.copyOf(checkNotNull(processors, "processors")); | 
| 60 |       this.ram = ram; | 
| 61 |       this.volumes = ImmutableList.copyOf(checkNotNull(volumes, "volumes")); | 
| 62 |       this.supportsImage = supportsImage; | 
| 63 |    } | 
| 64 |   | 
| 65 |    /** | 
| 66 |     * {@inheritDoc} | 
| 67 |     */ | 
| 68 |    @Override | 
| 69 |    public List<? extends Processor> getProcessors() { | 
| 70 |       return processors; | 
| 71 |    } | 
| 72 |   | 
| 73 |    /** | 
| 74 |     * {@inheritDoc} | 
| 75 |     */ | 
| 76 |    @Override | 
| 77 |    public int getRam() { | 
| 78 |       return ram; | 
| 79 |    } | 
| 80 |   | 
| 81 |    /** | 
| 82 |     * {@inheritDoc} | 
| 83 |     */ | 
| 84 |    @Override | 
| 85 |    public List<? extends Volume> getVolumes() { | 
| 86 |       return volumes; | 
| 87 |    } | 
| 88 |   | 
| 89 |    /** | 
| 90 |     * {@inheritDoc} | 
| 91 |     */ | 
| 92 |    @Override | 
| 93 |    public int compareTo(ResourceMetadata<ComputeType> that) { | 
| 94 |       if (that instanceof Hardware) { | 
| 95 |          Hardware thatHardware = Hardware.class.cast(that); | 
| 96 |          return ComparisonChain.start().compare(getCoresAndSpeed(this), getCoresAndSpeed(thatHardware)) | 
| 97 |                .compare(this.getRam(), thatHardware.getRam()).compare(getSpace(this), getSpace(thatHardware)).result(); | 
| 98 |       } else { | 
| 99 |          return super.compareTo(that); | 
| 100 |       } | 
| 101 |    } | 
| 102 |   | 
| 103 |    /** | 
| 104 |     * {@inheritDoc} | 
| 105 |     */ | 
| 106 |    @Override | 
| 107 |    public String toString() { | 
| 108 |       return "[id=" + getId() + ", providerId=" + getProviderId() + ", name=" + getName() + ", processors=" | 
| 109 |             + processors + ", ram=" + ram + ", volumes=" + volumes + ", supportsImage=" + supportsImage + "]"; | 
| 110 |    } | 
| 111 |   | 
| 112 |    /** | 
| 113 |     * {@inheritDoc} | 
| 114 |     */ | 
| 115 |    @Override | 
| 116 |    public Predicate<Image> supportsImage() { | 
| 117 |       return supportsImage; | 
| 118 |    } | 
| 119 |   | 
| 120 |    @Override | 
| 121 |    public int hashCode() { | 
| 122 |       final int prime = 31; | 
| 123 |       int result = super.hashCode(); | 
| 124 |       result = prime * result + ((processors == null) ? 0 : processors.hashCode()); | 
| 125 |       result = prime * result + ram; | 
| 126 |       result = prime * result + ((supportsImage == null) ? 0 : supportsImage.hashCode()); | 
| 127 |       result = prime * result + ((volumes == null) ? 0 : volumes.hashCode()); | 
| 128 |       return result; | 
| 129 |    } | 
| 130 |   | 
| 131 |    @Override | 
| 132 |    public boolean equals(Object obj) { | 
| 133 |       if (this == obj) | 
| 134 |          return true; | 
| 135 |       if (!super.equals(obj)) | 
| 136 |          return false; | 
| 137 |       if (getClass() != obj.getClass()) | 
| 138 |          return false; | 
| 139 |       HardwareImpl other = (HardwareImpl) obj; | 
| 140 |       if (processors == null) { | 
| 141 |          if (other.processors != null) | 
| 142 |             return false; | 
| 143 |       } else if (!processors.equals(other.processors)) | 
| 144 |          return false; | 
| 145 |       if (ram != other.ram) | 
| 146 |          return false; | 
| 147 |       if (supportsImage == null) { | 
| 148 |          if (other.supportsImage != null) | 
| 149 |             return false; | 
| 150 |       } else if (!supportsImage.equals(other.supportsImage)) | 
| 151 |          return false; | 
| 152 |       if (volumes == null) { | 
| 153 |          if (other.volumes != null) | 
| 154 |             return false; | 
| 155 |       } else if (!volumes.equals(other.volumes)) | 
| 156 |          return false; | 
| 157 |       return true; | 
| 158 |    } | 
| 159 | } |