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