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