| 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; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Predicates.not; |
| 23 | import static org.jclouds.compute.predicates.ImagePredicates.any; |
| 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.compute.domain.internal.HardwareImpl; |
| 31 | import org.jclouds.compute.predicates.ImagePredicates; |
| 32 | import org.jclouds.domain.Location; |
| 33 | |
| 34 | import com.google.common.base.Predicate; |
| 35 | import com.google.common.collect.ImmutableList; |
| 36 | import com.google.common.collect.Lists; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | public class HardwareBuilder extends ComputeMetadataBuilder { |
| 43 | protected List<Processor> processors = Lists.newArrayList(); |
| 44 | protected int ram; |
| 45 | protected List<Volume> volumes = Lists.newArrayList(); |
| 46 | protected Predicate<Image> supportsImage = any(); |
| 47 | |
| 48 | public HardwareBuilder() { |
| 49 | super(ComputeType.HARDWARE); |
| 50 | } |
| 51 | |
| 52 | public HardwareBuilder processor(Processor processor) { |
| 53 | this.processors.add(checkNotNull(processor, "processor")); |
| 54 | return this; |
| 55 | } |
| 56 | |
| 57 | public HardwareBuilder processors(Iterable<Processor> processors) { |
| 58 | this.processors = ImmutableList.copyOf(checkNotNull(processors, "processors")); |
| 59 | return this; |
| 60 | } |
| 61 | |
| 62 | public HardwareBuilder ram(int ram) { |
| 63 | this.ram = ram; |
| 64 | return this; |
| 65 | } |
| 66 | |
| 67 | public HardwareBuilder volume(Volume volume) { |
| 68 | this.volumes.add(checkNotNull(volume, "volume")); |
| 69 | return this; |
| 70 | } |
| 71 | |
| 72 | public HardwareBuilder volumes(Iterable<Volume> volumes) { |
| 73 | this.volumes = ImmutableList.copyOf(checkNotNull(volumes, "volumes")); |
| 74 | return this; |
| 75 | } |
| 76 | |
| 77 | public HardwareBuilder supportsImage(Predicate<Image> supportsImage) { |
| 78 | this.supportsImage = checkNotNull(supportsImage, "supportsImage"); |
| 79 | return this; |
| 80 | } |
| 81 | |
| 82 | public HardwareBuilder is64Bit(boolean is64Bit) { |
| 83 | supportsImage(is64Bit ? ImagePredicates.is64Bit() : not(ImagePredicates.is64Bit())); |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public HardwareBuilder id(String id) { |
| 89 | return HardwareBuilder.class.cast(super.id(id)); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public HardwareBuilder tags(Set<String> tags) { |
| 94 | return HardwareBuilder.class.cast(super.tags(tags)); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public HardwareBuilder ids(String id) { |
| 99 | return HardwareBuilder.class.cast(super.ids(id)); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public HardwareBuilder providerId(String providerId) { |
| 104 | return HardwareBuilder.class.cast(super.providerId(providerId)); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public HardwareBuilder name(String name) { |
| 109 | return HardwareBuilder.class.cast(super.name(name)); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public HardwareBuilder location(Location location) { |
| 114 | return HardwareBuilder.class.cast(super.location(location)); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public HardwareBuilder uri(URI uri) { |
| 119 | return HardwareBuilder.class.cast(super.uri(uri)); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public HardwareBuilder userMetadata(Map<String, String> userMetadata) { |
| 124 | return HardwareBuilder.class.cast(super.userMetadata(userMetadata)); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Hardware build() { |
| 129 | return new HardwareImpl(providerId, name, id, location, uri, userMetadata, tags, processors, ram, volumes, |
| 130 | supportsImage); |
| 131 | } |
| 132 | |
| 133 | @SuppressWarnings("unchecked") |
| 134 | public static HardwareBuilder fromHardware(Hardware in) { |
| 135 | return new HardwareBuilder().id(in.getId()).providerId(in.getProviderId()).location(in.getLocation()).name( |
| 136 | in.getName()).uri(in.getUri()).userMetadata(in.getUserMetadata()).tags(in.getTags()).processors( |
| 137 | List.class.cast(in.getProcessors())).ram(in.getRam()).volumes(List.class.cast(in.getVolumes())) |
| 138 | .supportsImage(in.supportsImage()); |
| 139 | } |
| 140 | } |