| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 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 | import java.util.Set; |
| 29 | |
| 30 | import org.jclouds.javax.annotation.Nullable; |
| 31 | |
| 32 | import org.jclouds.compute.domain.ComputeType; |
| 33 | import org.jclouds.compute.domain.Hardware; |
| 34 | import org.jclouds.compute.domain.Image; |
| 35 | import org.jclouds.compute.domain.Processor; |
| 36 | import org.jclouds.compute.domain.Volume; |
| 37 | import org.jclouds.domain.Location; |
| 38 | import org.jclouds.domain.ResourceMetadata; |
| 39 | |
| 40 | import com.google.common.base.Predicate; |
| 41 | import com.google.common.collect.ComparisonChain; |
| 42 | import com.google.common.collect.ImmutableList; |
| 43 | |
| 44 | /** |
| 45 | * @author Adrian Cole |
| 46 | */ |
| 47 | public class HardwareImpl extends ComputeMetadataImpl implements Hardware { |
| 48 | |
| 49 | /** The serialVersionUID */ |
| 50 | private static final long serialVersionUID = 8994255275911717567L; |
| 51 | private final List<Processor> processors; |
| 52 | private final int ram; |
| 53 | private final List<Volume> volumes; |
| 54 | private final Predicate<Image> supportsImage; |
| 55 | |
| 56 | public HardwareImpl(String providerId, String name, String id, @Nullable Location location, URI uri, |
| 57 | Map<String, String> userMetadata, Set<String> tags, Iterable<? extends Processor> processors, int ram, |
| 58 | Iterable<? extends Volume> volumes, Predicate<Image> supportsImage) { |
| 59 | super(ComputeType.HARDWARE, providerId, name, id, location, uri, userMetadata, tags); |
| 60 | this.processors = ImmutableList.copyOf(checkNotNull(processors, "processors")); |
| 61 | this.ram = ram; |
| 62 | this.volumes = ImmutableList.copyOf(checkNotNull(volumes, "volumes")); |
| 63 | this.supportsImage = supportsImage; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * {@inheritDoc} |
| 68 | */ |
| 69 | @Override |
| 70 | public List<? extends Processor> getProcessors() { |
| 71 | return processors; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * {@inheritDoc} |
| 76 | */ |
| 77 | @Override |
| 78 | public int getRam() { |
| 79 | return ram; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * {@inheritDoc} |
| 84 | */ |
| 85 | @Override |
| 86 | public List<? extends Volume> getVolumes() { |
| 87 | return volumes; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * {@inheritDoc} |
| 92 | */ |
| 93 | @Override |
| 94 | public int compareTo(ResourceMetadata<ComputeType> that) { |
| 95 | if (that instanceof Hardware) { |
| 96 | Hardware thatHardware = Hardware.class.cast(that); |
| 97 | return ComparisonChain.start().compare(getCoresAndSpeed(this), getCoresAndSpeed(thatHardware)) |
| 98 | .compare(this.getRam(), thatHardware.getRam()).compare(getSpace(this), getSpace(thatHardware)).result(); |
| 99 | } else { |
| 100 | return super.compareTo(that); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * {@inheritDoc} |
| 106 | */ |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | return "[id=" + getId() + ", providerId=" + getProviderId() + ", name=" + getName() + ", processors=" |
| 110 | + processors + ", ram=" + ram + ", volumes=" + volumes + ", supportsImage=" + supportsImage + ", tags=" + tags + "]"; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * {@inheritDoc} |
| 115 | */ |
| 116 | @Override |
| 117 | public Predicate<Image> supportsImage() { |
| 118 | return supportsImage; |
| 119 | } |
| 120 | |
| 121 | } |